Skip to content
This repository was archived by the owner on Apr 19, 2021. It is now read-only.

Commit 537e6cd

Browse files
committed
Complete Tests, change protected to public in addHandlers method on laravel locator
1 parent f0d5e3e commit 537e6cd

File tree

6 files changed

+82
-3
lines changed

6 files changed

+82
-3
lines changed

src/Locator/LaravelLocator.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function addHandler($handler, $commandClassName)
3535
*
3636
* @param array $commandClassToHandlerMap
3737
*/
38-
protected function addHandlers(array $commandClassToHandlerMap)
38+
public function addHandlers(array $commandClassToHandlerMap)
3939
{
4040
foreach ($commandClassToHandlerMap as $commandClass => $handler) {
4141
$this->addHandler($handler, $commandClass);

tests/Bus/TestBus.php

+32
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,36 @@ public function test_it_applies_a_middleware()
3535
$this->assertEquals('Handled', $commandHandled->addedPropertyInMiddleware);
3636
}
3737

38+
/**
39+
* Test if the bus is able to map a property in the command
40+
*/
41+
public function test_it_maps_input_to_command()
42+
{
43+
$bus = app('Joselfonseca\LaravelTactician\CommandBusInterface');
44+
$bus->addHandler('Joselfonseca\LaravelTactician\Tests\Stubs\TestCommand',
45+
'Joselfonseca\LaravelTactician\Tests\Stubs\TestCommandHandler');
46+
$commandHandled = $bus->dispatch('Joselfonseca\LaravelTactician\Tests\Stubs\TestCommand', [
47+
'property' => 'Jhon Doe'
48+
], [
49+
'Joselfonseca\LaravelTactician\Tests\Stubs\Middleware'
50+
]);
51+
$this->assertEquals('Jhon Doe', $commandHandled->property);
52+
}
53+
54+
/**
55+
* Test the InvalidArgumentException
56+
* @expectedException InvalidArgumentException
57+
*/
58+
public function test_it_trows_exception_if_input_can_not_be_mapped_to_the_command()
59+
{
60+
$bus = app('Joselfonseca\LaravelTactician\CommandBusInterface');
61+
$bus->addHandler('Joselfonseca\LaravelTactician\Tests\Stubs\TestCommandInput',
62+
'Joselfonseca\LaravelTactician\Tests\Stubs\TestCommandHandler');
63+
$commandHandled = $bus->dispatch('Joselfonseca\LaravelTactician\Tests\Stubs\TestCommandInput', [
64+
65+
], [
66+
'Joselfonseca\LaravelTactician\Tests\Stubs\Middleware'
67+
]);
68+
}
69+
3870
}

tests/Locator/TestLaravelLocator.php

+16
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,20 @@ public function test_it_is_able_to_resolve_handler_from_laravel_container()
5252
$this->assertInstanceOf('Joselfonseca\LaravelTactician\Tests\Stubs\TestCommandHandler', $handler);
5353
}
5454

55+
/**
56+
* Add more than one command => handler to the bus
57+
*/
58+
public function test_it_maps_array_commands()
59+
{
60+
$locator = app('League\Tactician\Handler\Locator\HandlerLocator');
61+
$locator->addHandlers([
62+
'Joselfonseca\LaravelTactician\Tests\Stubs\TestCommand' => 'Joselfonseca\LaravelTactician\Tests\Stubs\TestCommandHandler',
63+
'Joselfonseca\LaravelTactician\Tests\Stubs\TestCommandInput' => 'Joselfonseca\LaravelTactician\Tests\Stubs\TestCommandSeccondHandler'
64+
]);
65+
$handler = $locator->getHandlerForCommand('Joselfonseca\LaravelTactician\Tests\Stubs\TestCommand');
66+
$handler2 = $locator->getHandlerForCommand('Joselfonseca\LaravelTactician\Tests\Stubs\TestCommandInput');
67+
$this->assertInstanceOf('Joselfonseca\LaravelTactician\Tests\Stubs\TestCommandHandler', $handler);
68+
$this->assertInstanceOf('Joselfonseca\LaravelTactician\Tests\Stubs\TestCommandSeccondHandler', $handler2);
69+
}
70+
5571
}

tests/Stubs/TestCommand.php

+7-2
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,14 @@
55

66
class TestCommand {
77

8-
public function __construct()
9-
{
8+
public $property;
9+
10+
public $propertyTwo;
1011

12+
public function __construct($property = null, $propertyTwo = "First Name")
13+
{
14+
$this->property = $property;
15+
$this->propertyTwo = $propertyTwo;
1116
}
1217

1318
}

tests/Stubs/TestCommandInput.php

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Joselfonseca\LaravelTactician\Tests\Stubs;
4+
5+
6+
class TestCommandInput
7+
{
8+
public $property;
9+
10+
public function __construct($property)
11+
{
12+
$this->property = $property;
13+
}
14+
}
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace Joselfonseca\LaravelTactician\Tests\Stubs;
4+
5+
6+
class TestCommandSeccondHandler
7+
{
8+
public function handle($command)
9+
{
10+
return $command;
11+
}
12+
}

0 commit comments

Comments
 (0)