From 6cd6d408b1e7ab337c7fd2be7a0da3f2268dfcf9 Mon Sep 17 00:00:00 2001 From: Kai Sassnowski Date: Fri, 23 Apr 2021 17:09:22 +0200 Subject: [PATCH] Adds basic test suite for existing functionality --- src/Spotlight.php | 9 +- src/SpotlightSearchResult.php | 9 ++ tests/Commands/LogoutTest.php | 37 +++++ tests/ExampleTest.php | 12 -- tests/SpotlightCommandDependenciesTest.php | 62 ++++++++ tests/SpotlightCommandDependencyTest.php | 53 +++++++ tests/SpotlightSearchResultTest.php | 43 ++++++ tests/SpotlightTest.php | 158 +++++++++++++++++++++ tests/TestCase.php | 14 +- 9 files changed, 368 insertions(+), 29 deletions(-) create mode 100644 tests/Commands/LogoutTest.php delete mode 100644 tests/ExampleTest.php create mode 100644 tests/SpotlightCommandDependenciesTest.php create mode 100644 tests/SpotlightCommandDependencyTest.php create mode 100644 tests/SpotlightSearchResultTest.php create mode 100644 tests/SpotlightTest.php diff --git a/src/Spotlight.php b/src/Spotlight.php index f65d481..d20129c 100644 --- a/src/Spotlight.php +++ b/src/Spotlight.php @@ -55,13 +55,8 @@ public function searchDependency(string $commandId, $dependency, $query, $resolv $params = array_merge(['query' => $query], (array) $resolvedDependencies); $this->dependencyQueryResults = collect(ImplicitlyBoundMethod::call(app(), [$command, $method], $params)) - ->map(function (SpotlightSearchResult $result) { - return [ - 'id' => $result->getId(), - 'name' => $result->getName(), - 'description' => $result->getDescription(), - ]; - })->toArray(); + ->map(fn (SpotlightSearchResult $result) => $result->toArray()) + ->toArray(); } } diff --git a/src/SpotlightSearchResult.php b/src/SpotlightSearchResult.php index 421245c..3895ec9 100644 --- a/src/SpotlightSearchResult.php +++ b/src/SpotlightSearchResult.php @@ -29,4 +29,13 @@ public function getDescription(): ?string { return $this->description; } + + public function toArray(): array + { + return [ + 'id' => $this->id, + 'name' => $this->name, + 'description' => $this->description, + ]; + } } diff --git a/tests/Commands/LogoutTest.php b/tests/Commands/LogoutTest.php new file mode 100644 index 0000000..99374b3 --- /dev/null +++ b/tests/Commands/LogoutTest.php @@ -0,0 +1,37 @@ +createMock(Spotlight::class); + $guardMock = $this->createMock(StatefulGuard::class); + $guardMock->expects($this->once())->method('logout'); + + $command = new Logout(); + + $command->execute($spotlight, $guardMock); + } + + /** @test */ + public function it_redirects_after_logout(): void + { + $spotlight = $this->createMock(Spotlight::class); + $spotlight->expects($this->once()) + ->method('redirect') + ->with('/'); + $guardMock = $this->createMock(StatefulGuard::class); + + $command = new Logout(); + + $command->execute($spotlight, $guardMock); + } +} diff --git a/tests/ExampleTest.php b/tests/ExampleTest.php deleted file mode 100644 index 037c36b..0000000 --- a/tests/ExampleTest.php +++ /dev/null @@ -1,12 +0,0 @@ -assertTrue(true); - } -} diff --git a/tests/SpotlightCommandDependenciesTest.php b/tests/SpotlightCommandDependenciesTest.php new file mode 100644 index 0000000..b2ca03e --- /dev/null +++ b/tests/SpotlightCommandDependenciesTest.php @@ -0,0 +1,62 @@ +toArray()); + } + + /** @test */ + public function it_correctly_serializes_for_a_single_dependency(): void + { + $dependencies = SpotlightCommandDependencies::collection() + ->add( + SpotlightCommandDependency::make('::id-1::') + ->setPlaceholder('::placeholder-1::') + ); + + self::assertEquals([ + [ + 'id' => '::id-1::', + 'placeholder' => '::placeholder-1::', + 'type' => 'search', + ] + ], $dependencies->toArray()); + } + + /** @test */ + public function it_returns_the_dependencies_in_reversed_order(): void + { + $dependencies = SpotlightCommandDependencies::collection() + ->add( + SpotlightCommandDependency::make('::id-1::') + ->setPlaceholder('::placeholder-1::') + ) + ->add( + SpotlightCommandDependency::make('::id-2::') + ->setPlaceholder('::placeholder-2::') + ); + + self::assertEquals([ + [ + 'id' => '::id-2::', + 'placeholder' => '::placeholder-2::', + 'type' => 'search', + ], + [ + 'id' => '::id-1::', + 'placeholder' => '::placeholder-1::', + 'type' => 'search', + ], + ], $dependencies->toArray()); + } +} diff --git a/tests/SpotlightCommandDependencyTest.php b/tests/SpotlightCommandDependencyTest.php new file mode 100644 index 0000000..642517a --- /dev/null +++ b/tests/SpotlightCommandDependencyTest.php @@ -0,0 +1,53 @@ +toArray()); + } + + public function dependencyProvider(): Generator + { + yield from [ + 'no explicit type' => [ + '::id::', + function (SpotlightCommandDependency $dependency) { + $dependency->setPlaceholder('::placeholder-1::'); + }, + [ + 'id' => '::id::', + 'placeholder' => '::placeholder-1::', + 'type' => SpotlightCommandDependency::SEARCH, + ] + ], + + 'provide explicit type' => [ + '::id::', + function (SpotlightCommandDependency $dependency) { + $dependency + ->setType(SpotlightCommandDependency::INPUT) + ->setPlaceholder('::placeholder-2::'); + }, + [ + 'id' => '::id::', + 'placeholder' => '::placeholder-2::', + 'type' => SpotlightCommandDependency::INPUT, + ] + ] + ]; + } +} diff --git a/tests/SpotlightSearchResultTest.php b/tests/SpotlightSearchResultTest.php new file mode 100644 index 0000000..bc9ed0e --- /dev/null +++ b/tests/SpotlightSearchResultTest.php @@ -0,0 +1,43 @@ +result = new SpotlightSearchResult('::id::', '::name::', '::description::'); + } + + /** @test */ + public function it_can_be_turned_into_an_array(): void + { + self::assertEquals([ + 'id' => '::id::', + 'name' => '::name::', + 'description' => '::description::', + ], $this->result->toArray()); + } + + /** @test */ + public function it_returns_its_id(): void + { + self::assertEquals('::id::', $this->result->getId()); + } + + /** @test */ + public function it_returns_its_description(): void + { + self::assertEquals('::description::', $this->result->getDescription()); + } + + /** @test */ + public function it_returns_its_name(): void + { + self::assertEquals('::name::', $this->result->getName()); + } +} diff --git a/tests/SpotlightTest.php b/tests/SpotlightTest.php new file mode 100644 index 0000000..ff269c0 --- /dev/null +++ b/tests/SpotlightTest.php @@ -0,0 +1,158 @@ +execute($command->getId()); + + self::assertEquals(1, $_SERVER['__command.called']); + } + + /** + * @test + * @dataProvider conditionalCommandRegistrationProvider + */ + public function it_can_conditionally_register_commands(callable $registerCommand, int $expected): void + { + $command = new TestCommand(); + $registerCommand($command); + + try { + (new Spotlight())->execute($command->getId()); + } catch (TypeError) { + // Trying to execute a command that hasn't been registered + // currently throws an exception. That's not what we want + // to check for, however, so we'll ignore it for now. + } + + self::assertEquals($expected, $_SERVER['__command.called']); + } + + public function conditionalCommandRegistrationProvider(): Generator + { + yield from [ + 'registerCommandIf (true)' => [ + function ($command) { + Spotlight::registerCommandIf(true, get_class($command)); + }, + 1 + ], + + 'registerCommandIf (false)' => [ + function ($command) { + Spotlight::registerCommandIf(false, get_class($command)); + }, + 0 + ], + + 'registerCommandUnless (true)' => [ + function ($command) { + Spotlight::registerCommandUnless(true, get_class($command)); + }, + 0 + ], + + 'registerCommandUnless (false)' => [ + function ($command) { + Spotlight::registerCommandUnless(false, get_class($command)); + }, + 1 + ] + ]; + } + + /** + * @test + * @dataProvider searchDependencyProvider + */ + public function it_resolves_a_commands_dependencies_via_the_corresponding_method(string $dependency, array $expected): void + { + $command = new TestCommand(); + Spotlight::registerCommand(get_class($command)); + $spotlight = new Spotlight(); + + $spotlight->searchDependency($command->getId(), $dependency, '::query::'); + + self::assertEquals($spotlight->dependencyQueryResults, $expected); + } + + public function searchDependencyProvider(): Generator + { + yield from [ + 'searchFoo' => [ + 'foo', + [ + [ + 'id' => '::foo-id::', + 'name' => '::foo-name::', + 'description' => '::foo-description::', + ] + ] + ], + + 'searchBar' => [ + 'bar', + [ + [ + 'id' => '::bar-id::', + 'name' => '::bar-name::', + 'description' => '::bar-description::', + ], + ], + ], + + 'no method exists' => [ + 'baz', + [], + ] + ]; + } +} + +class TestCommand extends SpotlightCommand +{ + public function execute() + { + ++$_SERVER['__command.called']; + } + + public function searchFoo() + { + return collect([new SpotlightSearchResult( + '::foo-id::', + '::foo-name::', + '::foo-description::', + )]); + } + + public function searchBar() + { + return collect([new SpotlightSearchResult( + '::bar-id::', + '::bar-name::', + '::bar-description::', + )]); + } +} diff --git a/tests/TestCase.php b/tests/TestCase.php index d5c76c0..e6fdd76 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -2,23 +2,17 @@ namespace LivewireUI\Spotlight\Tests; -use Illuminate\Database\Eloquent\Factories\Factory; +use Livewire\LivewireServiceProvider; +use LivewireUI\Spotlight\SpotlightServiceProvider; use Orchestra\Testbench\TestCase as Orchestra; class TestCase extends Orchestra { - public function setUp(): void - { - parent::setUp(); - - Factory::guessFactoryNamesUsing( - fn (string $modelName) => 'Spatie\\Spotlight\\Database\\Factories\\'.class_basename($modelName).'Factory' - ); - } - protected function getPackageProviders($app) { return [ + LivewireServiceProvider::class, + SpotlightServiceProvider::class, ]; }