@@ -145,4 +145,156 @@ public function itDetectsMultiInstanceInstanceNamesCorrectlyDataProvider()
145145 ],
146146 ];
147147 }
148+
149+ /**
150+ * @test
151+ */
152+ public function itShouldReturnTrueIfServiceEnabledCommandRanSuccessfully ()
153+ {
154+ $ processBuilderStub = $ this ->buildProcessBuilderMock (true , 'enabled ' );
155+ $ processBuilderStub ->setArguments (['is-enabled ' , static ::SERVICE_NAME ,])->willReturn ($ processBuilderStub );
156+
157+ $ unit = new UnitStub (static ::SERVICE_NAME , $ processBuilderStub ->reveal ());
158+
159+ $ this ->assertTrue ($ unit ->isEnabled ());
160+ }
161+
162+ /**
163+ * @test
164+ */
165+ public function itShouldReturnFalseIfServiceEnabledCommandFailed ()
166+ {
167+ $ processBuilderStub = $ this ->buildProcessBuilderMock (false , 'enabled ' );
168+ $ processBuilderStub ->setArguments (['is-enabled ' , static ::SERVICE_NAME ,])->willReturn ($ processBuilderStub );
169+
170+ $ unit = new UnitStub (static ::SERVICE_NAME , $ processBuilderStub ->reveal ());
171+
172+ $ this ->assertFalse ($ unit ->isEnabled ());
173+ }
174+
175+ /**
176+ * @param bool $commandSuccessful
177+ * @param string $commandOutput
178+ *
179+ * @test
180+ * @dataProvider itShouldReturnFalseIfServiceEnabledCommandOutputDoesNotEqualEnabledDataProvider
181+ */
182+ public function itShouldReturnFalseIfServiceEnabledCommandOutputDoesNotEqualEnabled (
183+ $ commandSuccessful ,
184+ $ commandOutput
185+ ) {
186+ $ processBuilderStub = $ this ->buildProcessBuilderMock ($ commandSuccessful , $ commandOutput );
187+ $ processBuilderStub ->setArguments (['is-enabled ' , static ::SERVICE_NAME ,])->willReturn ($ processBuilderStub );
188+
189+ $ unit = new UnitStub (static ::SERVICE_NAME , $ processBuilderStub ->reveal ());
190+
191+ $ this ->assertFalse ($ unit ->isEnabled ());
192+ }
193+
194+ /**
195+ * @return array
196+ */
197+ public function itShouldReturnFalseIfServiceEnabledCommandOutputDoesNotEqualEnabledDataProvider (): array
198+ {
199+ return [
200+ [
201+ 'commandSuccessful ' => true ,
202+ 'commandOutput ' => 'static ' ,
203+ ],
204+ [
205+ 'commandSuccessful ' => false ,
206+ 'commandOutput ' => 'static ' ,
207+ ],
208+ [
209+ 'commandSuccessful ' => true ,
210+ 'commandOutput ' => 'enable ' ,
211+ ],
212+ ];
213+ }
214+
215+ /**
216+ * @test
217+ */
218+ public function itShouldReturnTrueIfServiceActiveCommandRanSuccessfully ()
219+ {
220+ $ processBuilderStub = $ this ->buildProcessBuilderMock (true , 'active ' );
221+ $ processBuilderStub ->setArguments (['is-active ' , static ::SERVICE_NAME ,])->willReturn ($ processBuilderStub );
222+
223+ $ unit = new UnitStub (static ::SERVICE_NAME , $ processBuilderStub ->reveal ());
224+
225+ $ this ->assertTrue ($ unit ->isRunning ());
226+ }
227+
228+ /**
229+ * @test
230+ */
231+ public function itShouldReturnFalseIfServiceActiveCommandFailed ()
232+ {
233+ $ processBuilderStub = $ this ->buildProcessBuilderMock (false , 'active ' );
234+ $ processBuilderStub ->setArguments (['is-active ' , static ::SERVICE_NAME ,])->willReturn ($ processBuilderStub );
235+
236+ $ unit = new UnitStub (static ::SERVICE_NAME , $ processBuilderStub ->reveal ());
237+
238+ $ this ->assertFalse ($ unit ->isRunning ());
239+ }
240+
241+ /**
242+ * @param bool $commandSuccessful
243+ * @param string $commandOutput
244+ *
245+ * @test
246+ * @dataProvider itShouldReturnFalseIfServiceActiveCommandOutputDoesNotEqualActiveDataProvider
247+ */
248+ public function itShouldReturnFalseIfServiceActiveCommandOutputDoesNotEqualActive (
249+ $ commandSuccessful ,
250+ $ commandOutput
251+ ) {
252+ $ processBuilderStub = $ this ->buildProcessBuilderMock ($ commandSuccessful , $ commandOutput );
253+ $ processBuilderStub ->setArguments (['is-active ' , static ::SERVICE_NAME ,])->willReturn ($ processBuilderStub );
254+
255+ $ unit = new UnitStub (static ::SERVICE_NAME , $ processBuilderStub ->reveal ());
256+
257+ $ this ->assertFalse ($ unit ->isRunning ());
258+ }
259+
260+ /**
261+ * @return array
262+ */
263+ public function itShouldReturnFalseIfServiceActiveCommandOutputDoesNotEqualActiveDataProvider (): array
264+ {
265+ return [
266+ [
267+ 'commandSuccessful ' => true ,
268+ 'commandOutput ' => 'static ' ,
269+ ],
270+ [
271+ 'commandSuccessful ' => false ,
272+ 'commandOutput ' => 'static ' ,
273+ ],
274+ [
275+ 'commandSuccessful ' => true ,
276+ 'commandOutput ' => 'enable ' ,
277+ ],
278+ ];
279+ }
280+
281+ /**
282+ * @param bool $processRanSuccessful
283+ * @param string $processOutput
284+ *
285+ * @return \Prophecy\Prophecy\ObjectProphecy
286+ */
287+ private function buildProcessBuilderMock ($ processRanSuccessful = true , $ processOutput = '' ): ObjectProphecy
288+ {
289+ $ processBuilderStub = $ this ->prophesize (ProcessBuilder::class);
290+
291+ $ processStub = $ this ->prophesize (Process::class);
292+ $ processStub ->run ()->willReturn (!$ processRanSuccessful );
293+ $ processStub ->isSuccessful ()->willReturn ($ processRanSuccessful );
294+ $ processStub ->getOutput ()->willReturn ($ processOutput );
295+
296+ $ processBuilderStub ->getProcess ()->willReturn ($ processStub );
297+
298+ return $ processBuilderStub ;
299+ }
148300}
0 commit comments