1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace spec\Fenos\Notifynder\Groups; |
4
|
|
|
|
5
|
|
|
use Fenos\Notifynder\Contracts\NotifynderGroupCategoryDB; |
6
|
|
|
use Fenos\Notifynder\Contracts\NotifynderGroupDB; |
7
|
|
|
use Fenos\Notifynder\Exceptions\NotifynderGroupNotFoundException; |
8
|
|
|
use Fenos\Notifynder\Models\NotificationGroup; |
9
|
|
|
use PhpSpec\ObjectBehavior; |
10
|
|
|
|
11
|
|
|
class GroupManagerSpec extends ObjectBehavior |
12
|
|
|
{ |
13
|
|
|
public function let(NotifynderGroupDB $groupDB, NotifynderGroupCategoryDB $groupCategoryDB) |
14
|
|
|
{ |
15
|
|
|
$this->beConstructedWith($groupDB, $groupCategoryDB); |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
public function it_is_initializable() |
19
|
|
|
{ |
20
|
|
|
$this->shouldHaveType('Fenos\Notifynder\Groups\GroupManager'); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** @test */ |
24
|
|
View Code Duplication |
public function it_find_a_group_by_id(NotifynderGroupDB $groupDB, NotificationGroup $group) |
|
|
|
|
25
|
|
|
{ |
26
|
|
|
$group_id = 1; |
27
|
|
|
|
28
|
|
|
$groupDB->find($group_id)->shouldBeCalled() |
29
|
|
|
->willReturn($group); |
30
|
|
|
|
31
|
|
|
$this->findById($group_id)->shouldReturnAnInstanceOf(NotificationGroup::class); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** @test */ |
35
|
|
View Code Duplication |
public function it_try_to_find_an_not_existing_group_by_id(NotifynderGroupDB $groupDB) |
|
|
|
|
36
|
|
|
{ |
37
|
|
|
$group_id = 1; |
38
|
|
|
|
39
|
|
|
$groupDB->find($group_id)->shouldBeCalled() |
40
|
|
|
->willReturn(null); |
41
|
|
|
|
42
|
|
|
$this->shouldThrow(NotifynderGroupNotFoundException::class)->during('findById', [$group_id]); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** @test */ |
46
|
|
View Code Duplication |
public function it_find_a_group_by_name(NotifynderGroupDB $groupDB, NotificationGroup $group) |
|
|
|
|
47
|
|
|
{ |
48
|
|
|
$group_name = 'mygroup'; |
49
|
|
|
|
50
|
|
|
$groupDB->findByName($group_name)->shouldBeCalled() |
51
|
|
|
->willReturn($group); |
52
|
|
|
|
53
|
|
|
$this->findByName($group_name)->shouldReturnAnInstanceOf(NotificationGroup::class); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** @test */ |
57
|
|
View Code Duplication |
public function it_try_to_find_an_not_existing_group_by_name(NotifynderGroupDB $groupDB) |
|
|
|
|
58
|
|
|
{ |
59
|
|
|
$group_name = 'mygroup'; |
60
|
|
|
|
61
|
|
|
$groupDB->findByName($group_name)->shouldBeCalled() |
62
|
|
|
->willReturn(null); |
63
|
|
|
|
64
|
|
|
$this->shouldThrow(NotifynderGroupNotFoundException::class)->during('findByName', [$group_name]); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** @test */ |
68
|
|
View Code Duplication |
public function it_add_a_category_to_a_group_by_id(NotifynderGroupCategoryDB $groupCategoryDB, NotificationGroup $group) |
|
|
|
|
69
|
|
|
{ |
70
|
|
|
$group_id = 1; |
71
|
|
|
$category_id = 2; |
72
|
|
|
|
73
|
|
|
$groupCategoryDB->addCategoryToGroupById($group_id, $category_id)->shouldBeCalled() |
74
|
|
|
->willReturn($group); |
75
|
|
|
|
76
|
|
|
$this->addCategoryToGroupById($group_id, $category_id)->shouldReturnAnInstanceOf(NotificationGroup::class); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** @test */ |
80
|
|
View Code Duplication |
public function it_add_a_category_to_a_group_by_name(NotifynderGroupCategoryDB $groupCategoryDB, NotificationGroup $group) |
|
|
|
|
81
|
|
|
{ |
82
|
|
|
$group_name = 'mygroup'; |
83
|
|
|
$category_name = 'mycategory'; |
84
|
|
|
|
85
|
|
|
$groupCategoryDB->addCategoryToGroupByName($group_name, $category_name)->shouldBeCalled() |
86
|
|
|
->willReturn($group); |
87
|
|
|
|
88
|
|
|
$this->addCategoryToGroupByName($group_name, $category_name)->shouldReturnAnInstanceOf(NotificationGroup::class); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** @test */ |
92
|
|
|
public function it_add_multiple_categories_to_a_group(NotifynderGroupCategoryDB $groupCategoryDB) |
93
|
|
|
{ |
94
|
|
|
$group_name = 'mygroup'; |
95
|
|
|
$category1 = 'mycategory1'; |
96
|
|
|
$category2 = 'mycategory2'; |
97
|
|
|
|
98
|
|
|
$groupCategoryDB->addMultipleCategoriesToGroup($group_name, [$category1, $category2])->shouldBeCalled() |
99
|
|
|
->willReturn(2); |
100
|
|
|
|
101
|
|
|
$this->addMultipleCategoriesToGroup($group_name, $category1, $category2) |
102
|
|
|
->shouldReturn(2); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** @test */ |
106
|
|
View Code Duplication |
public function it_add_a_group_in_the_db_respecting_convention(NotifynderGroupDB $groupDB, NotificationGroup $group) |
|
|
|
|
107
|
|
|
{ |
108
|
|
|
$name = 'my.category'; |
109
|
|
|
|
110
|
|
|
$groupDB->create($name)->shouldBeCalled() |
111
|
|
|
->willReturn($group); |
112
|
|
|
|
113
|
|
|
$this->addGroup($name)->shouldReturnAnInstanceOf(NotificationGroup::class); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** @test */ |
117
|
|
|
public function it_add_a_group_in_the_db_NOT_respecting_convention(NotifynderGroupDB $groupDB, NotificationGroup $group) |
|
|
|
|
118
|
|
|
{ |
119
|
|
|
$name = 'mycategory'; // no dot as 'namespace' |
120
|
|
|
|
121
|
|
|
$this->shouldThrow('InvalidArgumentException')->during('addGroup', [$name]); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.