@@ -174,6 +174,42 @@ public function testPersist()
174174 $ crontabRepository ->persist ();
175175 }
176176
177+ /**
178+ * Test Remove a job
179+ */
180+ public function testRemove ()
181+ {
182+ /* Create fake crontabAdapter */
183+ $ fakeCrontabAdapter = $ this ->getMock ('TiBeN\CrontabManager\CrontabAdapter ' );
184+
185+ $ fakeCrontabAdapter
186+ ->expects ($ this ->any ())
187+ ->method ('readCrontab ' )
188+ ->will ($ this ->returnValue (file_get_contents ($ this ->fixturesPath . 'testing_persisted_crontab.txt ' )))
189+ ;
190+
191+ $ fakeCrontabAdapter
192+ ->expects ($ this ->once ())
193+ ->method ('writeCrontab ' )
194+ ->with ($ this ->equalTo (file_get_contents ($ this ->fixturesPath . 'testing_removed_crontab.txt ' )))
195+ ;
196+
197+ $ crontabRepository = new CrontabRepository ($ fakeCrontabAdapter );
198+
199+ /* Retrieve job */
200+ $ crontabJobs = $ crontabRepository ->findJobByRegex ('/launch\ -param\ mycommand/ ' );
201+
202+ /* Apply some local updates on the job to ensure
203+ * it is linked by reference to the repository
204+ */
205+ $ crontabJobs [0 ]->minutes = '00 ' ;
206+ $ crontabJobs [0 ]->hours = '01 ' ;
207+
208+ /* Remove job */
209+ $ crontabRepository ->removeJob ($ crontabJobs [0 ]);
210+ $ crontabRepository ->persist ();
211+ }
212+
177213 /**
178214 * Test if pass a wrong regular expression when searching by regex throw an invalid regex exception
179215 * @expectedException InvalidArgumentException
@@ -191,4 +227,45 @@ public function testExceptionInvalidRegexOnFindJobByRegex()
191227 $ crontabRepository = new CrontabRepository ($ fakeCrontabAdapter );
192228 $ crontabRepository ->findJobByRegex ('/$ ' );
193229 }
230+
231+ /**
232+ * Test remove an unknown Job
233+ * @expectedException LogicException
234+ * @expectedExceptionMessage This job is not part of this crontab
235+ */
236+ public function testRemoveAnUnknownJob ()
237+ {
238+ $ fakeCrontabAdapter = $ this ->getMock ('TiBeN\CrontabManager\CrontabAdapter ' );
239+
240+ $ fakeCrontabAdapter
241+ ->expects ($ this ->any ())
242+ ->method ('readCrontab ' )
243+ ->will ($ this ->returnValue (file_get_contents ($ this ->fixturesPath . 'simple_crontab.txt ' )))
244+ ;
245+ $ crontabRepository = new CrontabRepository ($ fakeCrontabAdapter );
246+
247+ $ job = CrontabJob::createFromCrontabLine ('30 23 * * * launch -param mycommand ' );
248+ $ crontabRepository ->removeJob ($ job );
249+ }
250+
251+ /**
252+ * Add an already in the repository job
253+ * @expectedException \LogicException
254+ */
255+ public function testAddAnAlreadyInTheRepositoryJob ()
256+ {
257+ $ fakeCrontabAdapter = $ this ->getMock ('TiBeN\CrontabManager\CrontabAdapter ' );
258+
259+ $ fakeCrontabAdapter
260+ ->expects ($ this ->any ())
261+ ->method ('readCrontab ' )
262+ ->will ($ this ->returnValue (file_get_contents ($ this ->fixturesPath . 'simple_crontab.txt ' )))
263+ ;
264+
265+ $ crontabRepository = new CrontabRepository ($ fakeCrontabAdapter );
266+
267+ /* Modify the existing job */
268+ $ crontabJobs = $ crontabRepository ->findJobByRegex ('/launch\ -param\ mycommand/ ' );
269+ $ crontabRepository ->addJob ($ crontabJobs [0 ]);
270+ }
194271}
0 commit comments