Completed
Pull Request — develop (#39)
by Axel
08:43
created

testGetProjectFeedbacksByAuthor()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 9
rs 9.6666
cc 1
eloc 7
nc 1
nop 0
1
<?php
2
3
namespace Developtech\AgilityBundle\Tests\Manager;
4
5
use Developtech\AgilityBundle\Manager\FeedbackManager;
6
7
use Developtech\AgilityBundle\Entity\Feedback;
8
use Developtech\AgilityBundle\Tests\Mock\User;
9
use Developtech\AgilityBundle\Entity\Project;
10
11
use Developtech\AgilityBundle\Utils\Slugger;
12
13
class FeedbackManagerTest extends \PHPUnit_Framework_TestCase {
14
    /** @var FeedbackManager **/
15
    protected $manager;
16
17
    public function setUp() {
18
        $this->manager = new FeedbackManager($this->getEntityManagerMock(), new Slugger(), Feedback::class);
0 ignored issues
show
Unused Code introduced by
The call to FeedbackManager::__construct() has too many arguments starting with \Developtech\AgilityBundle\Entity\Feedback::class.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
19
    }
20
21
    public function testGetProjectFeedbacks() {
22
        $feedbacks = $this->manager->getProjectFeedbacks((new Project()));
23
24
        $this->assertCount(2, $feedbacks);
25
        $this->assertInstanceOf(Feedback::class, $feedbacks[0]);
26
        $this->assertEquals(2, $feedbacks[1]->getId());
27
    }
28
29
    public function testGetProjectFeedbacksByAuthor() {
30
        $feedbacks = $this->manager->getProjectFeedbacksByAuthor(new Project(), new User('Hagrid'));
31
32
        $this->assertCount(2, $feedbacks);
33
        $this->assertInstanceOf(Feedback::class, $feedbacks[0]);
34
        $this->assertEquals(2, $feedbacks[1]->getId());
35
        $this->assertEquals('Hagrid', $feedbacks[0]->getAuthor()->getUsername());
36
        $this->assertEquals('Hagrid', $feedbacks[1]->getAuthor()->getUsername());
37
    }
38
39
    public function testGetFeedback() {
40
        $feedback = $this->manager->getFeedback(1);
41
42
        $this->assertInstanceOf(Feedback::class, $feedback);
43
        $this->assertEquals(1, $feedback->getId());
44
    }
45
46
    /**
47
     * @expectedException Symfony\Component\HttpKernel\Exception\NotFoundHttpException
48
     * @expectedExceptionMessage Feedback not found
49
     */
50
    public function testGetFeedbackWithUnexistingFeedback() {
51
        $this->manager->getFeedback(2);
52
    }
53
54
    public function testCreateFeedback() {
55
        $feedback = $this->manager->createFeedback(
56
            (new Project()),
57
            'There is a bug in the kitchen',
58
            'The fridge is hotter than my computer !',
59
            (new User())
60
        );
61
        $this->assertInstanceOf(Feedback::class, $feedback);
62
        $this->assertInstanceOf(Project::class, $feedback->getProject());
63
        $this->assertEquals('There is a bug in the kitchen', $feedback->getName());
64
        $this->assertEquals('there-is-a-bug-in-the-kitchen', $feedback->getSlug());
65
        $this->assertEquals('The fridge is hotter than my computer !', $feedback->getDescription());
66
        $this->assertInstanceOf(User::class, $feedback->getAuthor());
67
        $this->assertEquals(Feedback::STATUS_OPEN, $feedback->getStatus());
68
    }
69
70
    public function testCountFeedbacksPerStatus() {
71
        $this->assertEquals(3, $this->manager->countFeedbacksPerStatus((new Project())->setId(1), Feedback::STATUS_OPEN));
72
    }
73
74
    public function getEntityManagerMock() {
75
        $entityManagerMock = $this
76
            ->getMockBuilder('Doctrine\ORM\EntityManager')
77
            ->disableOriginalConstructor()
78
            ->getMock()
79
        ;
80
        $entityManagerMock
81
            ->expects($this->any())
82
            ->method('getRepository')
83
            ->willReturnCallback([$this, 'getRepositoryMock'])
84
        ;
85
        $entityManagerMock
86
            ->expects($this->any())
87
            ->method('persist')
88
            ->willReturn(true)
89
        ;
90
        $entityManagerMock
91
            ->expects($this->any())
92
            ->method('flush')
93
            ->willReturn(true)
94
        ;
95
        return $entityManagerMock;
96
    }
97
98
    public function getRepositoryMock() {
99
        $repositoryMock = $this
100
            ->getMockBuilder('Developtech\AgilityBundle\Repository\FeedbackRepository')
101
            ->disableOriginalConstructor()
102
            ->setMethods([
103
                'find',
104
                'findBy',
105
                'findByProject',
106
                'countPerStatus'
107
            ])
108
            ->getMock()
109
        ;
110
        $repositoryMock
111
            ->expects($this->any())
112
            ->method('findByProject')
113
            ->willReturnCallback([$this, 'getFeedbacksMock'])
114
        ;
115
        $repositoryMock
116
            ->expects($this->any())
117
            ->method('findBy')
118
            ->willReturnCallback([$this, 'getFeedbacksWithAuthorMock'])
119
        ;
120
        $repositoryMock
121
            ->expects($this->any())
122
            ->method('find')
123
            ->willReturnCallback([$this, 'getFeedbackMock'])
124
        ;
125
        $repositoryMock
126
            ->expects($this->any())
127
            ->method('countPerStatus')
128
            ->willReturn(3)
129
        ;
130
        return $repositoryMock;
131
    }
132
133
    public function getFeedbackMock($id) {
134
        if($id === 2) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after IF keyword; 0 found
Loading history...
135
            return null;
136
        }
137
        return
138
            (new Feedback())
139
            ->setId(1)
140
            ->setName('I can\'t see the calendar')
141
            ->setSlug('i-can-t-see-the-calendar')
142
            ->setDescription('Add brightness to this calendar !')
143
            ->setProject(new Project())
144
            ->setAuthor(new User())
145
            ->setDeveloper(new User())
146
            ->setCreatedAt(new \DateTime())
147
            ->setUpdatedAt(new \DateTime())
148
        ;
149
    }
150
151
    public function getFeedbacksMock() {
152
        return [
153
            (new Feedback())
154
            ->setId(1)
155
            ->setName('I can\'t see the calendar')
156
            ->setSlug('i-can-t-see-the-calendar')
157
            ->setDescription('Add brightness to this calendar !')
158
            ->setProject(new Project())
159
            ->setAuthor(new User())
160
            ->setDeveloper(new User())
161
            ->setCreatedAt(new \DateTime())
162
            ->setUpdatedAt(new \DateTime()),
163
            (new Feedback())
164
            ->setId(2)
165
            ->setName('The calendar is not shiny enough')
166
            ->setSlug('the-calendar-is-not-shiny-enough')
167
            ->setDescription('This calendar blew my eyes away !')
168
            ->setProject(new Project())
169
            ->setAuthor(new User())
170
            ->setDeveloper(new User())
171
            ->setCreatedAt(new \DateTime())
172
            ->setUpdatedAt(new \DateTime()),
173
        ];
174
    }
175
176
    public function getFeedbacksWithAuthorMock() {
177
        return [
178
            (new Feedback())
179
            ->setId(1)
180
            ->setName('I can\'t see the calendar')
181
            ->setSlug('i-can-t-see-the-calendar')
182
            ->setDescription('Add brightness to this calendar !')
183
            ->setProject(new Project())
184
            ->setAuthor(new User('Hagrid'))
185
            ->setDeveloper(new User())
186
            ->setCreatedAt(new \DateTime())
187
            ->setUpdatedAt(new \DateTime()),
188
            (new Feedback())
189
            ->setId(2)
190
            ->setName('The calendar is not shiny enough')
191
            ->setSlug('the-calendar-is-not-shiny-enough')
192
            ->setDescription('This calendar blew my eyes away !')
193
            ->setProject(new Project())
194
            ->setAuthor(new User('Hagrid'))
195
            ->setDeveloper(new User())
196
            ->setCreatedAt(new \DateTime())
197
            ->setUpdatedAt(new \DateTime()),
198
        ];
199
    }
200
}
201