scratch – Blame information for rev 115

Subversion Repositories:
Rev:
Rev Author Line No. Line
115 office 1 <?php
2  
3 /*
4 * This file is part of Alchemy\BinaryDriver.
5 *
6 * (c) Alchemy <info@alchemy.fr>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11  
12 namespace Alchemy\Tests\BinaryDriver;
13  
14 use Alchemy\BinaryDriver\ProcessRunner;
15 use Alchemy\BinaryDriver\BinaryDriverTestCase;
16 use Alchemy\BinaryDriver\Exception\ExecutionFailureException;
17 use Alchemy\BinaryDriver\Listeners\ListenerInterface;
18 use Evenement\EventEmitter;
19 use Symfony\Component\Process\Exception\RuntimeException as ProcessRuntimeException;
20  
21 class ProcessRunnerTest extends BinaryDriverTestCase
22 {
23 public function getProcessRunner($logger)
24 {
25 return new ProcessRunner($logger, 'test-runner');
26 }
27  
28 public function testRunSuccessFullProcess()
29 {
30 $logger = $this->createLoggerMock();
31 $runner = $this->getProcessRunner($logger);
32  
33 $process = $this->createProcessMock(1, true, '--helloworld--', "Kikoo Romain", null, true);
34  
35 $logger
36 ->expects($this->never())
37 ->method('error');
38 $logger
39 ->expects($this->exactly(2))
40 ->method('info');
41  
42 $this->assertEquals('Kikoo Romain', $runner->run($process, new \SplObjectStorage(), false));
43 }
44  
45 public function testRunSuccessFullProcessBypassingErrors()
46 {
47 $logger = $this->createLoggerMock();
48 $runner = $this->getProcessRunner($logger);
49  
50 $process = $this->createProcessMock(1, true, '--helloworld--', "Kikoo Romain", null, true);
51  
52 $logger
53 ->expects($this->never())
54 ->method('error');
55 $logger
56 ->expects($this->exactly(2))
57 ->method('info');
58  
59 $this->assertEquals('Kikoo Romain', $runner->run($process, new \SplObjectStorage(), true));
60 }
61  
62 public function testRunFailingProcess()
63 {
64 $logger = $this->createLoggerMock();
65 $runner = $this->getProcessRunner($logger);
66  
67 $process = $this->createProcessMock(1, false, '--helloworld--', null, null, true);
68  
69 $logger
70 ->expects($this->once())
71 ->method('error');
72 $logger
73 ->expects($this->once())
74 ->method('info');
75  
76 try {
77 $runner->run($process, new \SplObjectStorage(), false);
78 $this->fail('An exception should have been raised');
79 } catch (ExecutionFailureException $e) {
80  
81 }
82 }
83  
84 public function testRunFailingProcessWithException()
85 {
86 $logger = $this->createLoggerMock();
87 $runner = $this->getProcessRunner($logger);
88  
89 $exception = new ProcessRuntimeException('Process Failed');
90 $process = $this->getMockBuilder('Symfony\Component\Process\Process')
91 ->disableOriginalConstructor()
92 ->getMock();
93 $process->expects($this->once())
94 ->method('run')
95 ->will($this->throwException($exception));
96  
97 $logger
98 ->expects($this->once())
99 ->method('error');
100 $logger
101 ->expects($this->once())
102 ->method('info');
103  
104 try {
105 $runner->run($process, new \SplObjectStorage(), false);
106 $this->fail('An exception should have been raised');
107 } catch (ExecutionFailureException $e) {
108 $this->assertEquals($exception, $e->getPrevious());
109 }
110 }
111  
112 public function testRunfailingProcessBypassingErrors()
113 {
114 $logger = $this->createLoggerMock();
115 $runner = $this->getProcessRunner($logger);
116  
117 $process = $this->createProcessMock(1, false, '--helloworld--', 'Hello output', null, true);
118  
119 $logger
120 ->expects($this->once())
121 ->method('error');
122 $logger
123 ->expects($this->once())
124 ->method('info');
125  
126 $this->assertNull($runner->run($process, new \SplObjectStorage(), true));
127 }
128  
129 public function testRunFailingProcessWithExceptionBypassingErrors()
130 {
131 $logger = $this->createLoggerMock();
132 $runner = $this->getProcessRunner($logger);
133  
134 $exception = new ProcessRuntimeException('Process Failed');
135 $process = $this->getMockBuilder('Symfony\Component\Process\Process')
136 ->disableOriginalConstructor()
137 ->getMock();
138 $process->expects($this->once())
139 ->method('run')
140 ->will($this->throwException($exception));
141  
142 $logger
143 ->expects($this->once())
144 ->method('error');
145 $logger
146 ->expects($this->once())
147 ->method('info');
148  
149 $this->assertNull($runner->run($process, new \SplObjectStorage(), true));
150 }
151  
152 public function testRunSuccessFullProcessWithHandlers()
153 {
154 $logger = $this->createLoggerMock();
155 $runner = $this->getProcessRunner($logger);
156  
157 $capturedCallback = null;
158  
159 $process = $this->createProcessMock(1, true, '--helloworld--', "Kikoo Romain", null, true);
160 $process->expects($this->once())
161 ->method('run')
162 ->with($this->isInstanceOf('Closure'))
163 ->will($this->returnCallback(function ($callback) use (&$capturedCallback) {
164 $capturedCallback = $callback;
165 }));
166  
167 $logger
168 ->expects($this->never())
169 ->method('error');
170 $logger
171 ->expects($this->exactly(2))
172 ->method('info');
173  
174 $listener = new TestListener();
175 $storage = new \SplObjectStorage();
176 $storage->attach($listener);
177  
178 $capturedType = $capturedData = null;
179  
180 $listener->on('received', function ($type, $data) use (&$capturedType, &$capturedData) {
181 $capturedData = $data;
182 $capturedType = $type;
183 });
184  
185 $this->assertEquals('Kikoo Romain', $runner->run($process, $storage, false));
186  
187 $type = 'err';
188 $data = 'data';
189  
190 $capturedCallback($type, $data);
191  
192 $this->assertEquals($data, $capturedData);
193 $this->assertEquals($type, $capturedType);
194 }
195 }
196  
197 class TestListener extends EventEmitter implements ListenerInterface
198 {
199 public function handle($type, $data)
200 {
201 return $this->emit('received', array($type, $data));
202 }
203  
204 public function forwardedEvents()
205 {
206 return array();
207 }
208 }