1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverLeague\Console\Tests\Command; |
4
|
|
|
|
5
|
|
|
use SilverLeague\Console\Command\Dev\Tasks\AbstractTaskCommand; |
6
|
|
|
use SilverLeague\Console\Command\Factory; |
7
|
|
|
use SilverLeague\Console\Framework\Scaffold; |
8
|
|
|
use SilverStripe\Control\HTTPRequest; |
9
|
|
|
use SilverStripe\Core\Config\Config; |
10
|
|
|
use SilverStripe\Dev\Tasks\CleanupTestDatabasesTask; |
11
|
|
|
use Symfony\Component\Console\Application; |
12
|
|
|
use Symfony\Component\Console\Tester\CommandTester; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @coversDefaultClass \SilverLeague\Console\Command\Factory |
16
|
|
|
* @package silverstripe-console |
17
|
|
|
* @author Robbie Averill <[email protected]> |
18
|
|
|
*/ |
19
|
|
|
class FactoryTest extends \PHPUnit_Framework_TestCase |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Test that a Symfony command is bootstrapped and returned when a SilverStripe BuildTask is given |
23
|
|
|
*/ |
24
|
|
|
public function testGetCommandFromTask() |
25
|
|
|
{ |
26
|
|
|
$task = new CleanupTestDatabasesTask; |
27
|
|
|
|
28
|
|
|
$command = $this->getFactory()->getCommandFromTask($task); |
29
|
|
|
$this->assertInstanceOf(AbstractTaskCommand::class, $command); |
30
|
|
|
$this->assertInstanceOf(Application::class, $command->getApplication()); |
31
|
|
|
$this->assertSame($task, $command->getTask()); |
32
|
|
|
$this->assertSame($task->getTitle(), $command->getDescription()); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Test that a SilverStrip task name is converted to a Symfony friendly command name |
37
|
|
|
* |
38
|
|
|
* @param string $input |
39
|
|
|
* @param string $expected |
40
|
|
|
* @dataProvider commandNameProvider |
41
|
|
|
*/ |
42
|
|
|
public function testGetCommandName($input, $expected) |
43
|
|
|
{ |
44
|
|
|
Config::nest(); |
45
|
|
|
|
46
|
|
|
$fakeTask = new CleanupTestDatabasesTask; |
47
|
|
|
Config::modify()->set(get_class($fakeTask), 'segment', $input); |
48
|
|
|
$this->assertSame($expected, $this->getFactory()->getCommandName($fakeTask)); |
49
|
|
|
|
50
|
|
|
Config::unnest(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @return array[] |
55
|
|
|
*/ |
56
|
|
|
public function commandNameProvider() |
57
|
|
|
{ |
58
|
|
|
return [ |
59
|
|
|
['CleanupTestDatabasesTask', 'dev:tasks:cleanup-test-databases'], |
60
|
|
|
['FooBar', 'dev:tasks:foo-bar'], |
61
|
|
|
['FooBarTask', 'dev:tasks:foo-bar'] |
62
|
|
|
]; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Ensure that the BuildTask functionality can be returned as a closure for the Command |
67
|
|
|
*/ |
68
|
|
|
public function testGetTaskAsClosure() |
69
|
|
|
{ |
70
|
|
|
$taskMock = $this |
71
|
|
|
->getMockBuilder(CleanupTestDatabasesTask::class) |
72
|
|
|
->setMethods(['run']) |
73
|
|
|
->getMock(); |
74
|
|
|
|
75
|
|
|
$taskMock |
76
|
|
|
->expects($this->once()) |
77
|
|
|
->method('run') |
78
|
|
|
->with($this->isInstanceOf(HTTPRequest::class)); |
79
|
|
|
|
80
|
|
|
$factory = $this->getFactory(); |
81
|
|
|
$command = $factory->getCommandFromTask($taskMock); |
82
|
|
|
$this->assertTrue(is_callable($factory->getTaskAsClosure($command))); |
|
|
|
|
83
|
|
|
|
84
|
|
|
$tester = new CommandTester($command); |
|
|
|
|
85
|
|
|
$tester->execute([]); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Ensure that SilverStripe task URL segments are made "friendly" |
90
|
|
|
* |
91
|
|
|
* @param string $input |
92
|
|
|
* @param string $expected |
93
|
|
|
* @dataProvider segmentProvider |
94
|
|
|
*/ |
95
|
|
|
public function testGetFriendlySegment($input, $expected) |
96
|
|
|
{ |
97
|
|
|
$this->assertSame($expected, $this->getFactory()->getFriendlySegment($input)); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @return array[] |
102
|
|
|
*/ |
103
|
|
|
public function segmentProvider() |
104
|
|
|
{ |
105
|
|
|
return [ |
106
|
|
|
['SomeTask', 'some'], |
107
|
|
|
['Some\\Namespaced\\TaskName', 'some-namespaced-task-name'], |
108
|
|
|
['CleanupTestDatabasesTask', 'cleanup-test-databases'], |
109
|
|
|
]; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Get a Factory to test with |
114
|
|
|
* |
115
|
|
|
* @return Factory |
116
|
|
|
*/ |
117
|
|
|
protected function getFactory() |
118
|
|
|
{ |
119
|
|
|
return (new Scaffold)->getSilverStripeLoader()->getCommandFactory(); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
This check looks for type mismatches where the missing type is
false
. This is usually indicative of an error condtion.Consider the follow example
This function either returns a new
DateTime
object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returnedfalse
before passing on the value to another function or method that may not be able to handle afalse
.