Test Failed
Push — master ( 06fcec...ff7c45 )
by Martin
07:58
created

CommandSpec   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 184
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 2
dl 0
loc 184
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A it_is_initializable() 0 4 1
A generate_task() 0 7 1
A let() 0 51 1
A it_can_add_new_task() 0 4 1
A it_cannot_add_existed_task() 0 5 1
A it_cannot_add_empty_task() 0 5 1
A it_can_complete_task() 0 6 1
A it_can_redo_task() 0 6 1
B it_can_edit_task() 0 43 1
A it_can_remove_task() 0 10 1
A it_can_clean_completed_task() 0 4 1
1
<?php
2
3
namespace spec\Todo\Application\Task;
4
5
use Todo\Application\Task\Command;
6
use Todo\Domain\Exception\TaskNameIsAlreadyExistedException;
7
use Todo\Domain\Exception\TaskNameIsEmptyException;
8
use Todo\Domain\Exception\TaskNotFoundException;
9
use Todo\Domain\Factory\TaskFactory;
10
use Todo\Domain\Repository\TaskRepositoryInterface;
11
use Todo\Domain\Service\TaskValidationService;
12
use Todo\Domain\Task;
13
use PhpSpec\ObjectBehavior;
14
use Prophecy\Argument;
15
16
class CommandSpec extends ObjectBehavior
17
{
18
    /** @var TaskRepositoryInterface */
19
    protected $taskRepository;
20
21
    /** @var TaskFactory */
22
    protected $taskFactory;
23
24
    /** @var  TaskValidationService */
25
    protected $taskValidationService;
26
27
    protected $tasks;
28
29
30
    /** @var Task */
31
    protected $newTask;
32
33
34
    /** @var Task */
35
    protected $remainingTask;
36
37
    /** @var Task */
38
    protected $completedTask;
39
40
    function it_is_initializable()
41
    {
42
        $this->shouldHaveType(Command::class);
43
    }
44
45
    function generate_task(string $name) : Task
46
    {
47
        $task = new Task();
48
        $task->setName($name);
49
50
        return $task;
51
    }
52
53
    function let(TaskRepositoryInterface $taskRepository)
54
    {
55
        $this->newTask = $this->generate_task('Buying salt');
56
        $this->newTask->setStatus(Task::STATUS_REMAINING);
57
58
        $this->remainingTask = $this->generate_task('Buying sugar');
59
        $this->remainingTask->setId(1);
60
        $this->remainingTask->setStatus(Task::STATUS_REMAINING);
61
62
        $this->completedTask = $this->generate_task('Buying milk');
63
        $this->completedTask->setId(2);
64
        $this->completedTask->setStatus(Task::STATUS_COMPLETED);
65
66
        $this->tasks = [
67
            $this->remainingTask,
68
            $this->completedTask
69
        ];
70
71
72
        $this->taskRepository = $taskRepository;
73
74
75
76
        $this->taskRepository->find($this->remainingTask->getId())
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Todo\Domain\Task>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
77
            ->willReturn($this->remainingTask);
78
        $this->taskRepository->find($this->completedTask->getId())
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Todo\Domain\Task>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
79
            ->willReturn($this->completedTask);
80
        $this->taskRepository->findByName($this->remainingTask->getName())
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Todo\Domain\Task>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
81
            ->willReturn($this->remainingTask);
82
        $this->taskRepository->findByName($this->completedTask->getName())
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Todo\Domain\Task>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
83
            ->willReturn($this->completedTask);
84
        $this->taskRepository->findByName('Buying salt')
0 ignored issues
show
Bug introduced by
The method willThrow() does not seem to exist on object<Todo\Domain\Task>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
85
            ->willThrow(TaskNotFoundException::class);
86
        $this->taskRepository->findByName('')
0 ignored issues
show
Bug introduced by
The method willThrow() does not seem to exist on object<Todo\Domain\Task>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
87
            ->willThrow(TaskNotFoundException::class);
88
89
        $this->taskRepository->remove($this->remainingTask)
90
            ->willReturn(null);
91
92
        $this->taskRepository->save($this->newTask)
93
            ->willReturn(null);
94
95
        $this->taskRepository->save($this->remainingTask)
96
            ->willReturn(null);
97
        $this->taskRepository->save($this->completedTask)
98
            ->willReturn(null);
99
        $this->taskRepository->removeByStatus(Task::STATUS_COMPLETED)
100
            ->willReturn(null);
101
102
        $this->beConstructedWith($this->taskRepository);
103
    }
104
105
    function it_can_add_new_task()
106
    {
107
        $this->addNewTask($this->newTask->getName());
108
    }
109
110
    function it_cannot_add_existed_task()
111
    {
112
        $this->shouldThrow(TaskNameIsAlreadyExistedException::class)
113
            ->duringAddNewTask($this->remainingTask->getName());
114
    }
115
116
    function it_cannot_add_empty_task()
117
    {
118
        $this->shouldThrow(TaskNameIsEmptyException::class)
119
            ->duringAddNewTask('');
120
    }
121
122
123
124
    function it_can_complete_task()
125
    {
126
        $task = $this->completeTask($this->remainingTask->getId());
127
128
        $task->getStatus()->shouldBe(Task::STATUS_COMPLETED);
129
    }
130
131
    function it_can_redo_task()
132
    {
133
        $task = $this->redoTask($this->completedTask->getId());
134
135
        $task->getStatus()->shouldBe(Task::STATUS_REMAINING);
136
    }
137
138
    function it_can_edit_task()
139
    {
140
        $this->taskRepository->findByName('New name')
0 ignored issues
show
Bug introduced by
The method willThrow() does not seem to exist on object<Todo\Domain\Task>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
141
            ->willThrow(TaskNotFoundException::class);
142
143
        $task = $this->editTask(
144
            $this->remainingTask->getId(),
145
            [
146
                'name' => 'New name',
147
                'status' => Task::STATUS_COMPLETED
148
            ]
149
        );
150
151
        $task->getName()->shouldBe('New name');
152
        $task->getStatus()->shouldBe(Task::STATUS_COMPLETED);
153
154
155
        $task = $this->editTask(
156
            $this->remainingTask->getId(),
157
            [
158
                'name' => $this->remainingTask->getName(),
159
                'status' => Task::STATUS_COMPLETED
160
            ]
161
        );
162
163
        $task->getName()->shouldBe($this->remainingTask->getName());
164
        $task->getStatus()->shouldBe(Task::STATUS_COMPLETED);
165
166
        $this->shouldThrow(TaskNameIsAlreadyExistedException::class)
167
            ->duringEditTask(
168
                $this->remainingTask->getId(),
169
                [
170
                    'name' => 'Buying milk',
171
                ]
172
            );
173
        $this->shouldThrow(TaskNameIsEmptyException::class)
174
            ->duringEditTask(
175
                $this->remainingTask->getId(),
176
                [
177
                    'name' => '',
178
                ]
179
            );
180
    }
181
182
183
    function it_can_remove_task()
184
    {
185
        $this->removeTask(
186
            $this->remainingTask->getId(),
187
            [
188
                'name' => 'New name',
189
                'status' => Task::STATUS_COMPLETED
190
            ]
191
        );
192
    }
193
194
    function it_can_clean_completed_task()
195
    {
196
        $this->cleanAllCompletedTasks();
197
    }
198
199
}
200