1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Class FindDirectoryCommandTest |
4
|
|
|
* |
5
|
|
|
* @author Mauro Moreno <[email protected]> |
6
|
|
|
*/ |
7
|
|
|
namespace MauroMoreno\FindBundle\Tests\Command; |
8
|
|
|
|
9
|
|
|
use MauroMoreno\FindBundle\Command\FindDirectoryCommand; |
10
|
|
|
use MauroMoreno\FindBundle\Service\FindDirectoryService; |
11
|
|
|
use MauroMoreno\FindBundle\Service\Finder; |
12
|
|
|
use MauroMoreno\FindBundle\Service\Lister; |
13
|
|
|
use Symfony\Component\Console\Application; |
14
|
|
|
use Symfony\Component\Console\Tester\CommandTester; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class FindDirectoryCommandTest |
18
|
|
|
* @package MauroMoreno\FindBundle\Tests\Command |
19
|
|
|
*/ |
20
|
|
|
class FindDirectoryCommandTest extends \PHPUnit_Framework_TestCase |
21
|
|
|
{ |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* {@inheritdoc} |
25
|
|
|
*/ |
26
|
|
|
protected function setUp() |
27
|
|
|
{ |
28
|
|
|
$application = new Application(); |
29
|
|
|
$application->add(new FindDirectoryCommand()); |
30
|
|
|
|
31
|
|
|
$this->command = $application->find('find:dir'); |
|
|
|
|
32
|
|
|
$this->command->setContainer($this->getMockContainer()); |
|
|
|
|
33
|
|
|
$this->commandTester = new CommandTester($this->command); |
|
|
|
|
34
|
|
|
mkdir(__DIR__ . '/empty_directory'); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* {@inheritdoc} |
39
|
|
|
*/ |
40
|
|
|
protected function tearDown() |
41
|
|
|
{ |
42
|
|
|
rmdir(__DIR__ . '/empty_directory'); |
43
|
|
|
parent::tearDown(); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Test Command execute, empty directory |
48
|
|
|
*/ |
49
|
|
View Code Duplication |
public function testExecuteEmptyDirectory() |
|
|
|
|
50
|
|
|
{ |
51
|
|
|
$this->commandTester->execute([ |
|
|
|
|
52
|
|
|
'command' => $this->command->getName(), |
53
|
|
|
'pattern' => 'pattern', |
54
|
|
|
'directory' => __DIR__ . '/empty_directory' |
55
|
|
|
]); |
56
|
|
|
|
57
|
|
|
$this->assertEquals( |
58
|
|
|
"No results where found.\n", |
59
|
|
|
$this->commandTester->getDisplay() |
|
|
|
|
60
|
|
|
); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Test Command execute |
65
|
|
|
*/ |
66
|
|
View Code Duplication |
public function testExecute() |
|
|
|
|
67
|
|
|
{ |
68
|
|
|
$this->commandTester->execute([ |
|
|
|
|
69
|
|
|
'command' => $this->command->getName(), |
70
|
|
|
'pattern' => 'pattern', |
71
|
|
|
'directory' => __DIR__ . '/../Fixtures/directory' |
72
|
|
|
]); |
73
|
|
|
$this->assertRegExp( |
74
|
|
|
'/file_1|file_3/', |
75
|
|
|
$this->commandTester->getDisplay() |
|
|
|
|
76
|
|
|
); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Test Command execute, Extension set |
81
|
|
|
*/ |
82
|
|
View Code Duplication |
public function testExecuteExtensionSet() |
|
|
|
|
83
|
|
|
{ |
84
|
|
|
$this->commandTester->execute([ |
|
|
|
|
85
|
|
|
'command' => $this->command->getName(), |
86
|
|
|
'pattern' => 'pattern', |
87
|
|
|
'directory' => __DIR__ . '/../Fixtures/directory', |
88
|
|
|
'--extension' => 'txt' |
89
|
|
|
]); |
90
|
|
|
$this->assertEquals("file_3.txt\n", $this->commandTester->getDisplay()); |
|
|
|
|
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Get Mock Container |
95
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject |
96
|
|
|
*/ |
97
|
|
|
protected function getMockContainer() |
98
|
|
|
{ |
99
|
|
|
$mockContainer = $this->getMock( |
100
|
|
|
'Symfony\Component\DependencyInjection\Container' |
101
|
|
|
); |
102
|
|
|
$mockContainer |
103
|
|
|
->expects($this->once()) |
104
|
|
|
->method('get') |
105
|
|
|
->with('mauro_moreno_find.find_directory_service') |
106
|
|
|
->willReturn(new FindDirectoryService(new Finder, new Lister)); |
107
|
|
|
return $mockContainer; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
} |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: