scratch – Blame information for rev 120

Subversion Repositories:
Rev:
Rev Author Line No. Line
120 office 1 <?php
2  
3 namespace Alchemy\Tests\BinaryDriver;
4  
5 use Alchemy\BinaryDriver\AbstractBinary;
6 use Alchemy\BinaryDriver\BinaryDriverTestCase;
7 use Alchemy\BinaryDriver\Configuration;
8 use Alchemy\BinaryDriver\Exception\ExecutableNotFoundException;
9 use Alchemy\BinaryDriver\Listeners\ListenerInterface;
10 use Symfony\Component\Process\ExecutableFinder;
11  
12 class AbstractBinaryTest extends BinaryDriverTestCase
13 {
14 protected function getPhpBinary()
15 {
16 $finder = new ExecutableFinder();
17 $php = $finder->find('php');
18  
19 if (null === $php) {
20 $this->markTestSkipped('Unable to find a php binary');
21 }
22  
23 return $php;
24 }
25  
26 public function testSimpleLoadWithBinaryPath()
27 {
28 $php = $this->getPhpBinary();
29 $imp = Implementation::load($php);
30 $this->assertInstanceOf('Alchemy\Tests\BinaryDriver\Implementation', $imp);
31  
32 $this->assertEquals($php, $imp->getProcessBuilderFactory()->getBinary());
33 }
34  
35 public function testMultipleLoadWithBinaryPath()
36 {
37 $php = $this->getPhpBinary();
38 $imp = Implementation::load(array('/zz/path/to/unexisting/command', $php));
39 $this->assertInstanceOf('Alchemy\Tests\BinaryDriver\Implementation', $imp);
40  
41 $this->assertEquals($php, $imp->getProcessBuilderFactory()->getBinary());
42 }
43  
44 public function testSimpleLoadWithBinaryName()
45 {
46 $php = $this->getPhpBinary();
47 $imp = Implementation::load('php');
48 $this->assertInstanceOf('Alchemy\Tests\BinaryDriver\Implementation', $imp);
49  
50 $this->assertEquals($php, $imp->getProcessBuilderFactory()->getBinary());
51 }
52  
53 public function testMultipleLoadWithBinaryName()
54 {
55 $php = $this->getPhpBinary();
56 $imp = Implementation::load(array('bachibouzouk', 'php'));
57 $this->assertInstanceOf('Alchemy\Tests\BinaryDriver\Implementation', $imp);
58  
59 $this->assertEquals($php, $imp->getProcessBuilderFactory()->getBinary());
60 }
61  
62 public function testLoadWithMultiplePathExpectingAFailure()
63 {
64 $this->setExpectedException(ExecutableNotFoundException::class);
65  
66 Implementation::load(array('bachibouzouk', 'moribon'));
67 }
68  
69 public function testLoadWithUniquePathExpectingAFailure()
70 {
71 $this->setExpectedException(ExecutableNotFoundException::class);
72  
73 Implementation::load('bachibouzouk');
74 }
75  
76 public function testLoadWithCustomLogger()
77 {
78 $logger = $this->getMock('Psr\Log\LoggerInterface');
79 $imp = Implementation::load('php', $logger);
80  
81 $this->assertEquals($logger, $imp->getProcessRunner()->getLogger());
82 }
83  
84 public function testLoadWithCustomConfigurationAsArray()
85 {
86 $conf = array('timeout' => 200);
87 $imp = Implementation::load('php', null, $conf);
88  
89 $this->assertEquals($conf, $imp->getConfiguration()->all());
90 }
91  
92 public function testLoadWithCustomConfigurationAsObject()
93 {
94 $conf = $this->getMock('Alchemy\BinaryDriver\ConfigurationInterface');
95 $imp = Implementation::load('php', null, $conf);
96  
97 $this->assertEquals($conf, $imp->getConfiguration());
98 }
99  
100 public function testProcessBuilderFactoryGetterAndSetters()
101 {
102 $imp = Implementation::load('php');
103 $factory = $this->getMock('Alchemy\BinaryDriver\ProcessBuilderFactoryInterface');
104  
105 $imp->setProcessBuilderFactory($factory);
106 $this->assertEquals($factory, $imp->getProcessBuilderFactory());
107 }
108  
109 public function testConfigurationGetterAndSetters()
110 {
111 $imp = Implementation::load('php');
112 $conf = $this->getMock('Alchemy\BinaryDriver\ConfigurationInterface');
113  
114 $imp->setConfiguration($conf);
115 $this->assertEquals($conf, $imp->getConfiguration());
116 }
117  
118 public function testTimeoutIsSetOnConstruction()
119 {
120 $imp = Implementation::load('php', null, array('timeout' => 42));
121 $this->assertEquals(42, $imp->getProcessBuilderFactory()->getTimeout());
122 }
123  
124 public function testTimeoutIsSetOnConfigurationSetting()
125 {
126 $imp = Implementation::load('php', null);
127 $imp->setConfiguration(new Configuration(array('timeout' => 42)));
128 $this->assertEquals(42, $imp->getProcessBuilderFactory()->getTimeout());
129 }
130  
131 public function testTimeoutIsSetOnProcessBuilderSetting()
132 {
133 $imp = Implementation::load('php', null, array('timeout' => 42));
134  
135 $factory = $this->getMock('Alchemy\BinaryDriver\ProcessBuilderFactoryInterface');
136 $factory->expects($this->once())
137 ->method('setTimeout')
138 ->with(42);
139  
140 $imp->setProcessBuilderFactory($factory);
141 }
142  
143 public function testListenRegistersAListener()
144 {
145 $imp = Implementation::load('php');
146  
147 $listeners = $this->getMockBuilder('Alchemy\BinaryDriver\Listeners\Listeners')
148 ->disableOriginalConstructor()
149 ->getMock();
150  
151 $listener = $this->getMock('Alchemy\BinaryDriver\Listeners\ListenerInterface');
152  
153 $listeners->expects($this->once())
154 ->method('register')
155 ->with($this->equalTo($listener), $this->equalTo($imp));
156  
157 $reflexion = new \ReflectionClass('Alchemy\BinaryDriver\AbstractBinary');
158 $prop = $reflexion->getProperty('listenersManager');
159 $prop->setAccessible(true);
160 $prop->setValue($imp, $listeners);
161  
162 $imp->listen($listener);
163 }
164  
165 /**
166 * @dataProvider provideCommandParameters
167 */
168 public function testCommandRunsAProcess($parameters, $bypassErrors, $expectedParameters, $output)
169 {
170 $imp = Implementation::load('php');
171 $factory = $this->getMock('Alchemy\BinaryDriver\ProcessBuilderFactoryInterface');
172 $processRunner = $this->getMock('Alchemy\BinaryDriver\ProcessRunnerInterface');
173  
174 $process = $this->getMockBuilder('Symfony\Component\Process\Process')
175 ->disableOriginalConstructor()
176 ->getMock();
177  
178 $processRunner->expects($this->once())
179 ->method('run')
180 ->with($this->equalTo($process), $this->isInstanceOf('SplObjectStorage'), $this->equalTo($bypassErrors))
181 ->will($this->returnValue($output));
182  
183 $factory->expects($this->once())
184 ->method('create')
185 ->with($expectedParameters)
186 ->will($this->returnValue($process));
187  
188 $imp->setProcessBuilderFactory($factory);
189 $imp->setProcessRunner($processRunner);
190  
191 $this->assertEquals($output, $imp->command($parameters, $bypassErrors));
192 }
193  
194 /**
195 * @dataProvider provideCommandWithListenersParameters
196 */
197 public function testCommandWithTemporaryListeners($parameters, $bypassErrors, $expectedParameters, $output, $count, $listeners)
198 {
199 $imp = Implementation::load('php');
200 $factory = $this->getMock('Alchemy\BinaryDriver\ProcessBuilderFactoryInterface');
201 $processRunner = $this->getMock('Alchemy\BinaryDriver\ProcessRunnerInterface');
202  
203 $process = $this->getMockBuilder('Symfony\Component\Process\Process')
204 ->disableOriginalConstructor()
205 ->getMock();
206  
207 $firstStorage = $secondStorage = null;
208  
209 $processRunner->expects($this->exactly(2))
210 ->method('run')
211 ->with($this->equalTo($process), $this->isInstanceOf('SplObjectStorage'), $this->equalTo($bypassErrors))
212 ->will($this->returnCallback(function ($process, $storage, $errors) use ($output, &$firstStorage, &$secondStorage) {
213 if (null === $firstStorage) {
214 $firstStorage = $storage;
215 } else {
216 $secondStorage = $storage;
217 }
218  
219 return $output;
220 }));
221  
222 $factory->expects($this->exactly(2))
223 ->method('create')
224 ->with($expectedParameters)
225 ->will($this->returnValue($process));
226  
227 $imp->setProcessBuilderFactory($factory);
228 $imp->setProcessRunner($processRunner);
229  
230 $this->assertEquals($output, $imp->command($parameters, $bypassErrors, $listeners));
231 $this->assertCount($count, $firstStorage);
232 $this->assertEquals($output, $imp->command($parameters, $bypassErrors));
233 $this->assertCount(0, $secondStorage);
234 }
235  
236 public function provideCommandWithListenersParameters()
237 {
238 return array(
239 array('-a', false, array('-a'), 'loubda', 2, array($this->getMockListener(), $this->getMockListener())),
240 array('-a', false, array('-a'), 'loubda', 1, array($this->getMockListener())),
241 array('-a', false, array('-a'), 'loubda', 1, $this->getMockListener()),
242 array('-a', false, array('-a'), 'loubda', 0, array()),
243 );
244 }
245  
246 public function provideCommandParameters()
247 {
248 return array(
249 array('-a', false, array('-a'), 'loubda'),
250 array('-a', true, array('-a'), 'loubda'),
251 array('-a -b', false, array('-a -b'), 'loubda'),
252 array(array('-a'), false, array('-a'), 'loubda'),
253 array(array('-a'), true, array('-a'), 'loubda'),
254 array(array('-a', '-b'), false, array('-a', '-b'), 'loubda'),
255 );
256 }
257  
258 public function testUnlistenUnregistersAListener()
259 {
260 $imp = Implementation::load('php');
261  
262 $listeners = $this->getMockBuilder('Alchemy\BinaryDriver\Listeners\Listeners')
263 ->disableOriginalConstructor()
264 ->getMock();
265  
266 $listener = $this->getMock('Alchemy\BinaryDriver\Listeners\ListenerInterface');
267  
268 $listeners->expects($this->once())
269 ->method('unregister')
270 ->with($this->equalTo($listener), $this->equalTo($imp));
271  
272 $reflexion = new \ReflectionClass('Alchemy\BinaryDriver\AbstractBinary');
273 $prop = $reflexion->getProperty('listenersManager');
274 $prop->setAccessible(true);
275 $prop->setValue($imp, $listeners);
276  
277 $imp->unlisten($listener);
278 }
279  
280 /**
281 * @return \PHPUnit_Framework_MockObject_MockObject
282 */
283 private function getMockListener()
284 {
285 $listener = $this->getMock(ListenerInterface::class);
286 $listener->expects($this->any())
287 ->method('forwardedEvents')
288 ->willReturn(array());
289  
290 return $listener;
291 }
292 }
293  
294 class Implementation extends AbstractBinary
295 {
296 public function getName()
297 {
298 return 'Implementation';
299 }
300 }