Skip to content

Adds basic test suite for existing functionality #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: 1.0
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 2 additions & 7 deletions src/Spotlight.php
Original file line number Diff line number Diff line change
@@ -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();
}
}

9 changes: 9 additions & 0 deletions src/SpotlightSearchResult.php
Original file line number Diff line number Diff line change
@@ -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,
];
}
}
37 changes: 37 additions & 0 deletions tests/Commands/LogoutTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php declare(strict_types=1);

namespace LivewireUI\Spotlight\Tests\Commands;

use Illuminate\Contracts\Auth\StatefulGuard;
use LivewireUI\Spotlight\Commands\Logout;
use LivewireUI\Spotlight\Spotlight;
use PHPUnit\Framework\TestCase;

class LogoutTest extends TestCase
{
/** @test */
public function it_logs_out_the_current_user(): void
{
$spotlight = $this->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);
}
}
12 changes: 0 additions & 12 deletions tests/ExampleTest.php

This file was deleted.

62 changes: 62 additions & 0 deletions tests/SpotlightCommandDependenciesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php declare(strict_types=1);

namespace LivewireUI\Spotlight\Tests;

use LivewireUI\Spotlight\SpotlightCommandDependencies;
use LivewireUI\Spotlight\SpotlightCommandDependency;

class SpotlightCommandDependenciesTest extends \PHPUnit\Framework\TestCase
{
/** @test */
public function it_is_empty_by_default(): void
{
$dependencies = new SpotlightCommandDependencies();

self::assertEquals([], $dependencies->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());
}
}
53 changes: 53 additions & 0 deletions tests/SpotlightCommandDependencyTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php declare(strict_types=1);

namespace LivewireUI\Spotlight\Tests;

use Generator;
use LivewireUI\Spotlight\SpotlightCommandDependency;

class SpotlightCommandDependencyTest extends \PHPUnit\Framework\TestCase
{
/**
* @test
* @dataProvider dependencyProvider
*/
public function it_can_be_turned_into_an_array(string $identifier, callable $configureDependency, array $expected): void
{
$commandDependency = SpotlightCommandDependency::make($identifier);

$configureDependency($commandDependency);

self::assertEquals($expected, $commandDependency->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,
]
]
];
}
}
43 changes: 43 additions & 0 deletions tests/SpotlightSearchResultTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php declare(strict_types=1);

namespace LivewireUI\Spotlight\Tests;

use LivewireUI\Spotlight\SpotlightSearchResult;

class SpotlightSearchResultTest extends \PHPUnit\Framework\TestCase
{
private SpotlightSearchResult $result;

protected function setUp(): void
{
$this->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());
}
}
158 changes: 158 additions & 0 deletions tests/SpotlightTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
<?php declare(strict_types=1);

namespace LivewireUI\Spotlight\Tests;

use TypeError;
use Generator;
use Livewire\Component;
use LivewireUI\Spotlight\Spotlight;
use LivewireUI\Spotlight\SpotlightCommand;
use LivewireUI\Spotlight\SpotlightSearchResult;

class SpotlightTest extends TestCase
{
protected function setUp(): void
{
parent::setUp();

Spotlight::$commands = [];
$_SERVER['__command.called'] = 0;
}

/** @test */
public function it_can_execute_a_registered_command(): void
{
$command = new TestCommand();
Spotlight::registerCommand(get_class($command));

(new Spotlight())->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::',
)]);
}
}
14 changes: 4 additions & 10 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
@@ -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,
];
}