File tree Expand file tree Collapse file tree 5 files changed +84
-1
lines changed Expand file tree Collapse file tree 5 files changed +84
-1
lines changed Original file line number Diff line number Diff line change 11
11
],
12
12
"require" : {
13
13
"php" : " ~5.5 || ~7.0" ,
14
- "php-task/php-task" : " dev-master " ,
14
+ "php-task/php-task" : " dev-bugfix/event-after " ,
15
15
"symfony/http-kernel" : " ^2.6 || ^3.0" ,
16
16
"symfony/dependency-injection" : " ^2.6 || ^3.0" ,
17
17
"symfony/config" : " ^2.6 || ^3.0" ,
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ /*
4
+ * This file is part of php-task library.
5
+ *
6
+ * (c) php-task
7
+ *
8
+ * This source file is subject to the MIT license that is bundled
9
+ * with this source code in the file LICENSE.
10
+ */
11
+
12
+ namespace Task \TaskBundle \EventListener ;
13
+
14
+ use Doctrine \ORM \EntityManagerInterface ;
15
+ use Task \Event \TaskExecutionEvent ;
16
+
17
+ /**
18
+ * Listens on task-execution events.
19
+ */
20
+ class TaskExecutionListener
21
+ {
22
+ /**
23
+ * @var EntityManagerInterface
24
+ */
25
+ private $ entityManager ;
26
+
27
+ /**
28
+ * @param EntityManagerInterface $entityManager
29
+ */
30
+ public function __construct (EntityManagerInterface $ entityManager )
31
+ {
32
+ $ this ->entityManager = $ entityManager ;
33
+ }
34
+
35
+ /**
36
+ * This method clears the entity-manager after each task to ensure clean state before next task.
37
+ *
38
+ * @param TaskExecutionEvent $event
39
+ */
40
+ public function clearEntityManagerAfterTask (TaskExecutionEvent $ event )
41
+ {
42
+ $ this ->entityManager ->clear ();
43
+ }
44
+ }
Original file line number Diff line number Diff line change 16
16
<argument type =" string" >TaskBundle:TaskExecution</argument >
17
17
</service >
18
18
<service id =" task.storage.task_execution" alias =" task.repository.task_execution" />
19
+
20
+ <service id =" task.event_listener.execution" class =" Task\TaskBundle\EventListener\TaskExecutionListener" >
21
+ <argument type =" service" id =" doctrine.orm.entity_manager" />
22
+
23
+ <tag name =" %task.events.after%" method =" clearEntityManagerAfterTask" />
24
+ </service >
19
25
</services >
20
26
</container >
Original file line number Diff line number Diff line change 7
7
<parameter key =" task.events.create" type =" constant" >Task\Event\Events::TASK_CREATE</parameter >
8
8
<parameter key =" task.events.create_execution" type =" constant" >Task\Event\Events::TASK_EXECUTION_CREATE</parameter >
9
9
<parameter key =" task.events.before" type =" constant" >Task\Event\Events::TASK_BEFORE</parameter >
10
+ <parameter key =" task.events.after" type =" constant" >Task\Event\Events::TASK_AFTER</parameter >
10
11
<parameter key =" task.events.finished" type =" constant" >Task\Event\Events::TASK_FINISHED</parameter >
11
12
<parameter key =" task.events.passed" type =" constant" >Task\Event\Events::TASK_PASSED</parameter >
12
13
<parameter key =" task.events.failed" type =" constant" >Task\Event\Events::TASK_FAILED</parameter >
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ /*
4
+ * This file is part of php-task library.
5
+ *
6
+ * (c) php-task
7
+ *
8
+ * This source file is subject to the MIT license that is bundled
9
+ * with this source code in the file LICENSE.
10
+ */
11
+
12
+ namespace Unit \EventListener ;
13
+
14
+ use Doctrine \ORM \EntityManagerInterface ;
15
+ use Task \Event \TaskExecutionEvent ;
16
+ use Task \TaskBundle \EventListener \TaskExecutionListener ;
17
+
18
+ /**
19
+ * Tests for class TaskExecutionListener.
20
+ */
21
+ class TaskExecutionListenerTest extends \PHPUnit_Framework_TestCase
22
+ {
23
+ public function testClearEntityManagerAfterTask ()
24
+ {
25
+ $ entityManager = $ this ->prophesize (EntityManagerInterface::class);
26
+
27
+ $ listener = new TaskExecutionListener ($ entityManager ->reveal ());
28
+ $ listener ->clearEntityManagerAfterTask ($ this ->prophesize (TaskExecutionEvent::class)->reveal ());
29
+
30
+ $ entityManager ->clear ()->shouldBeCalled ();
31
+ }
32
+ }
You can’t perform that action at this time.
0 commit comments