From 11fe0a08901f67b3bb89ca8d3ffa9e6a44886b2e Mon Sep 17 00:00:00 2001 From: Aleksei Lebedev <1329824+LastDragon-ru@users.noreply.github.com> Date: Mon, 1 Jan 2024 14:02:24 +0400 Subject: [PATCH] refactor(testing)!: Removed deprecated `CronableAssertions::assertCronableRegistered()`, `CronableAssertions::setQueueableConfig()`, `CronableAssertions::isCronableRegistered()`. --- .../ChunkedChangeSafeIteratorTest.php | 3 +- .../src/Iterators/ChunkedIteratorTest.php | 3 +- .../src/SearchBy/Directives/DirectiveTest.php | 4 +- .../migrator/src/Commands/RawSeederTest.php | 2 + .../queue/src/CronableRegistratorTest.php | 12 ++- .../queue/src/QueueableConfiguratorTest.php | 3 + .../Testing/Package/WithQueueableConfig.php | 26 +++++++ .../Http/Controllers/SpaControllerTest.php | 4 +- .../Application/CronableAssertions.php | 78 ------------------- .../testing/src/Assertions/Assertions.php | 2 - 10 files changed, 49 insertions(+), 88 deletions(-) create mode 100644 packages/queue/src/Testing/Package/WithQueueableConfig.php delete mode 100644 packages/testing/src/Assertions/Application/CronableAssertions.php diff --git a/packages/eloquent/src/Iterators/ChunkedChangeSafeIteratorTest.php b/packages/eloquent/src/Iterators/ChunkedChangeSafeIteratorTest.php index 2bad28b19..af9dc48f0 100644 --- a/packages/eloquent/src/Iterators/ChunkedChangeSafeIteratorTest.php +++ b/packages/eloquent/src/Iterators/ChunkedChangeSafeIteratorTest.php @@ -3,6 +3,7 @@ namespace LastDragon_ru\LaraASP\Eloquent\Iterators; use Closure; +use Illuminate\Container\Container; use Illuminate\Database\ConnectionResolverInterface; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Model; @@ -35,7 +36,7 @@ public function testGetIterator(): void { $spyBefore = Mockery::spy(static fn () => null); $spyAfter = Mockery::spy(static fn () => null); - $db = $this->app->make(ConnectionResolverInterface::class); + $db = Container::getInstance()->make(ConnectionResolverInterface::class); $log = $this->getQueryLog($db); $query = TestObject::query()->orderByDesc('value'); $count = count($log); diff --git a/packages/eloquent/src/Iterators/ChunkedIteratorTest.php b/packages/eloquent/src/Iterators/ChunkedIteratorTest.php index 9563ac0cf..85ec1e215 100644 --- a/packages/eloquent/src/Iterators/ChunkedIteratorTest.php +++ b/packages/eloquent/src/Iterators/ChunkedIteratorTest.php @@ -2,6 +2,7 @@ namespace LastDragon_ru\LaraASP\Eloquent\Iterators; +use Illuminate\Container\Container; use Illuminate\Database\ConnectionResolverInterface; use LastDragon_ru\LaraASP\Eloquent\Testing\Package\Models\TestObject; use LastDragon_ru\LaraASP\Eloquent\Testing\Package\Models\WithTestObject; @@ -28,7 +29,7 @@ public function testGetIterator(): void { $spyBefore = Mockery::spy(static fn() => null); $spyAfter = Mockery::spy(static fn() => null); - $db = $this->app->make(ConnectionResolverInterface::class); + $db = Container::getInstance()->make(ConnectionResolverInterface::class); $log = $this->getQueryLog($db); $query = TestObject::query()->orderByDesc('value'); $expected = (clone $query)->get()->all(); diff --git a/packages/graphql/src/SearchBy/Directives/DirectiveTest.php b/packages/graphql/src/SearchBy/Directives/DirectiveTest.php index db0040b25..3c7c7815f 100644 --- a/packages/graphql/src/SearchBy/Directives/DirectiveTest.php +++ b/packages/graphql/src/SearchBy/Directives/DirectiveTest.php @@ -402,8 +402,8 @@ static function (self $test): GraphQLExpected { )); }, '~custom-complex-operators.graphql', - static function (TestCase $test): void { - $locator = $test->app->make(DirectiveLocator::class); + static function (): void { + $locator = Container::getInstance()->make(DirectiveLocator::class); $directive = new class() extends BaseOperator implements TypeDefinition { #[Override] public static function getName(): string { diff --git a/packages/migrator/src/Commands/RawSeederTest.php b/packages/migrator/src/Commands/RawSeederTest.php index 395b22f59..49372cd66 100644 --- a/packages/migrator/src/Commands/RawSeederTest.php +++ b/packages/migrator/src/Commands/RawSeederTest.php @@ -23,6 +23,8 @@ public function testHandle(): void { self::assertCount(0, $finder->files()); // Redefine path where files will be generated. + self::assertNotNull($this->app); + $this->app->useDatabasePath($path); // Call diff --git a/packages/queue/src/CronableRegistratorTest.php b/packages/queue/src/CronableRegistratorTest.php index 962b410b8..dfe3ce9fb 100644 --- a/packages/queue/src/CronableRegistratorTest.php +++ b/packages/queue/src/CronableRegistratorTest.php @@ -10,6 +10,7 @@ use LastDragon_ru\LaraASP\Queue\Configs\CronableConfig; use LastDragon_ru\LaraASP\Queue\Contracts\Cronable; use LastDragon_ru\LaraASP\Queue\Testing\Package\TestCase; +use LastDragon_ru\LaraASP\Queue\Testing\Package\WithQueueableConfig; use LogicException; use Mockery; use Mockery\MockInterface; @@ -21,6 +22,8 @@ */ #[CoversClass(CronableRegistrator::class)] class CronableRegistratorTest extends TestCase { + use WithQueueableConfig; + // // ========================================================================= /** @@ -75,10 +78,11 @@ public function getQueueConfig(): array { Queue::fake(); + $app = Container::getInstance()->make(Application::class); $schedule = Container::getInstance()->make(Schedule::class); $registrator = Container::getInstance()->make(CronableRegistrator::class); - $registrator->register($this->app, $schedule, $cronable::class); + $registrator->register($app, $schedule, $cronable::class); if ($enabled) { Queue::assertPushed($cronable::class); @@ -133,10 +137,11 @@ public function getQueueConfig(): array { CronableConfig::Enabled => false, ]); + $app = Container::getInstance()->make(Application::class); $schedule = Container::getInstance()->make(Schedule::class); $registrator = Container::getInstance()->make(CronableRegistrator::class); - $registrator->register($this->app, $schedule, $cronable::class); + $registrator->register($app, $schedule, $cronable::class); } public function testRegisterCronIsNull(): void { @@ -161,10 +166,11 @@ public function getQueueConfig(): array { CronableConfig::Enabled => true, ]); + $app = Container::getInstance()->make(Application::class); $schedule = Container::getInstance()->make(Schedule::class); $registrator = Container::getInstance()->make(CronableRegistrator::class); - $registrator->register($this->app, $schedule, $cronable::class); + $registrator->register($app, $schedule, $cronable::class); } // diff --git a/packages/queue/src/QueueableConfiguratorTest.php b/packages/queue/src/QueueableConfiguratorTest.php index bab73347f..ba5939511 100644 --- a/packages/queue/src/QueueableConfiguratorTest.php +++ b/packages/queue/src/QueueableConfiguratorTest.php @@ -8,6 +8,7 @@ use Illuminate\Container\Container; use LastDragon_ru\LaraASP\Queue\Contracts\ConfigurableQueueable; use LastDragon_ru\LaraASP\Queue\Testing\Package\TestCase; +use LastDragon_ru\LaraASP\Queue\Testing\Package\WithQueueableConfig; use Override; use PHPUnit\Framework\Attributes\CoversClass; @@ -16,6 +17,8 @@ */ #[CoversClass(QueueableConfigurator::class)] class QueueableConfiguratorTest extends TestCase { + use WithQueueableConfig; + public function testConfigure(): void { $configurator = Container::getInstance()->make(QueueableConfigurator::class); $queueable = new QueueableConfiguratorTest_ConfigurableQueueable(); diff --git a/packages/queue/src/Testing/Package/WithQueueableConfig.php b/packages/queue/src/Testing/Package/WithQueueableConfig.php new file mode 100644 index 000000000..1e66a7a9f --- /dev/null +++ b/packages/queue/src/Testing/Package/WithQueueableConfig.php @@ -0,0 +1,26 @@ + $queueable + * @param array $settings + */ + private function setQueueableConfig(ConfigurableQueueable|string $queueable, array $settings): void { + $key = sprintf('queue.queueables.%s', is_object($queueable) ? $queueable::class : $queueable); + $target = [ConfigMerger::Strict => false] + (array) config($key); + $merger = new ConfigMerger(); + + config([ + $key => $merger->merge($target, $settings), + ]); + } +} diff --git a/packages/spa/src/Http/Controllers/SpaControllerTest.php b/packages/spa/src/Http/Controllers/SpaControllerTest.php index a7185f731..bf2158e94 100644 --- a/packages/spa/src/Http/Controllers/SpaControllerTest.php +++ b/packages/spa/src/Http/Controllers/SpaControllerTest.php @@ -2,6 +2,8 @@ namespace LastDragon_ru\LaraASP\Spa\Http\Controllers; +use Illuminate\Container\Container; +use Illuminate\Contracts\Foundation\Application; use LastDragon_ru\LaraASP\Spa\Package; use LastDragon_ru\LaraASP\Spa\Provider; use LastDragon_ru\LaraASP\Spa\Testing\Package\TestCase; @@ -145,7 +147,7 @@ protected static function getAcceptDataProvider(): DataProvider { // // ========================================================================= protected function loadRoutes(): void { - (new class($this->app) extends Provider { + (new class(Container::getInstance()->make(Application::class)) extends Provider { #[Override] public function bootRoutes(): void { parent::bootRoutes(); diff --git a/packages/testing/src/Assertions/Application/CronableAssertions.php b/packages/testing/src/Assertions/Application/CronableAssertions.php deleted file mode 100644 index 30438b531..000000000 --- a/packages/testing/src/Assertions/Application/CronableAssertions.php +++ /dev/null @@ -1,78 +0,0 @@ - $cronable - */ - protected function assertCronableRegistered(string $cronable, string $message = ''): void { - $message = $message ?: sprintf('The `%s` is not registered as scheduled job.', $cronable); - $expected = $this->isCronableRegistered($cronable); - - self::assertTrue($expected, $message); - } - - /** - * @deprecated 5.3.1 - * - * @param ConfigurableQueueable|class-string $queueable - * @param array $settings - */ - protected function setQueueableConfig(ConfigurableQueueable|string $queueable, array $settings): void { - $key = sprintf('queue.queueables.%s', is_object($queueable) ? $queueable::class : $queueable); - $target = [ConfigMerger::Strict => false] + (array) config($key); - $merger = new ConfigMerger(); - - config([ - $key => $merger->merge($target, $settings), - ]); - } - - /** - * @deprecated 5.3.1 - * - * @param class-string $cronable - */ - protected function isCronableRegistered(string $cronable): bool { - $schedule = Container::getInstance()->make(Schedule::class); - $expected = method_exists($cronable, 'displayName') - ? Container::getInstance()->make($cronable)->displayName() - : $cronable; - $events = array_filter($schedule->events(), static function (Event $event) use ($expected): bool { - return str_contains("{$event->description}", $expected); - }); - - return count($events) === 1; - } -} diff --git a/packages/testing/src/Assertions/Assertions.php b/packages/testing/src/Assertions/Assertions.php index 10bc43764..6e24f0f1a 100644 --- a/packages/testing/src/Assertions/Assertions.php +++ b/packages/testing/src/Assertions/Assertions.php @@ -2,7 +2,6 @@ namespace LastDragon_ru\LaraASP\Testing\Assertions; -use LastDragon_ru\LaraASP\Testing\Assertions\Application\CronableAssertions; use LastDragon_ru\LaraASP\Testing\Assertions\Application\ScheduleAssertions; trait Assertions { @@ -10,7 +9,6 @@ trait Assertions { use JsonAssertions; use ScoutAssertions; use ScheduleAssertions; - use CronableAssertions; use ResponseAssertions; use DatabaseAssertions; }