Completed
Pull Request — master (#790)
by Guilherme
11:13 queued 05:10
created

GetTasksEventTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 95
rs 10
c 0
b 0
f 0
wmc 7
1
<?php
2
/**
3
 * This file is part of the login-cidadao project or it's bundles.
4
 *
5
 * (c) Guilherme Donato <guilhermednt on github>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace LoginCidadao\TaskStackBundle\Tests\Event;
12
13
use LoginCidadao\TaskStackBundle\Event\GetTasksEvent;
14
use PHPUnit\Framework\TestCase;
15
16
class GetTasksEventTest extends TestCase
17
{
18
    public function testEvent()
19
    {
20
        $task = $this->createMock('LoginCidadao\TaskStackBundle\Model\TaskInterface');
21
22
        $stackManager = $this->createMock('LoginCidadao\TaskStackBundle\Service\TaskStackManagerInterface');
23
        $stackManager->expects($this->once())->method('addNotSkippedTaskOnce')->with($task);
24
        $stackManager->expects($this->exactly(2))->method('setTaskSkipped')->with($task);
25
26
        $request = $this->createMock('Symfony\Component\HttpFoundation\Request');
27
        $event = new GetTasksEvent($stackManager, $request);
28
29
        $event->forceAddUniqueTask($task);
30
        $event->setTaskSkipped($task);
31
32
        $this->assertEquals($request, $event->getRequest());
33
    }
34
35
    public function testAddTaskIfStackEmpty()
36
    {
37
        $tasks = [];
38
        $task = $this->createMock('LoginCidadao\TaskStackBundle\Model\TaskInterface');
39
40
        $stackManager = $this->createMock('LoginCidadao\TaskStackBundle\Service\TaskStackManagerInterface');
41
        $stackManager->expects($this->atLeastOnce())->method('addNotSkippedTaskOnce')
42
            ->with($task)->willReturnCallback(
43
                function ($task) use (&$tasks) {
44
                    foreach ($tasks as $t) {
45
                        if ($t == $task) {
46
                            return $this;
47
                        }
48
                    }
49
                    $tasks[] = $task;
50
51
                    return $this;
52
                }
53
            );
54
        $stackManager->expects($this->atLeastOnce())->method('countTasks')->willReturnCallback(
55
            function () use ($tasks) {
56
                return count($tasks);
57
            }
58
        );
59
60
        $request = $this->createMock('Symfony\Component\HttpFoundation\Request');
61
        $event = new GetTasksEvent($stackManager, $request);
62
63
        $event->addTaskIfStackEmpty($task);
64
        $event->addTaskIfStackEmpty($task);
65
        $this->assertEquals(1, count($tasks));
66
    }
67
68
    public function testAddTaskIfStackEmptyWithIntent()
69
    {
70
        $tasks = [];
71
        $task = $this->createMock('LoginCidadao\TaskStackBundle\Model\TaskInterface');
72
        $intentTask = $this->getMockBuilder('LoginCidadao\TaskStackBundle\Model\IntentTask')
73
            ->disableOriginalConstructor()
74
            ->getMock();
75
76
        $stackManager = $this->createMock('LoginCidadao\TaskStackBundle\Service\TaskStackManagerInterface');
77
        $stackManager->expects($this->atLeastOnce())->method('getCurrentTask')->willReturnCallback(
78
            function () use (&$tasks) {
79
                return $tasks[0];
80
            }
81
        );
82
        $stackManager->expects($this->atLeastOnce())->method('addNotSkippedTaskOnce')
83
            ->with($this->isInstanceOf('LoginCidadao\TaskStackBundle\Model\TaskInterface'))
84
            ->willReturnCallback(
85
                function ($task) use (&$tasks) {
86
                    foreach ($tasks as $t) {
87
                        if ($t == $task) {
88
                            return $this;
89
                        }
90
                    }
91
                    $tasks[] = $task;
92
93
                    return $this;
94
                }
95
            );
96
        $stackManager->expects($this->atLeastOnce())->method('countTasks')->willReturnCallback(
97
            function () use (&$tasks) {
98
                return count($tasks);
99
            }
100
        );
101
102
        $request = $this->createMock('Symfony\Component\HttpFoundation\Request');
103
        $event = new GetTasksEvent($stackManager, $request);
104
105
        $event->addTaskIfStackEmpty($intentTask);
106
        $this->assertEquals(1, count($tasks));
107
108
        $event->addTaskIfStackEmpty($task);
109
        $event->addTaskIfStackEmpty($task);
110
        $this->assertEquals(2, count($tasks));
111
    }
112
}
113