Completed
Push — master ( b322d1...651cee )
by Axel
8s
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\Tests\Mock\Feedback;
8
use Developtech\AgilityBundle\Tests\Mock\User;
9
use Developtech\AgilityBundle\Tests\Mock\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);
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 getEntityManagerMock() {
71
        $entityManagerMock = $this
72
            ->getMockBuilder('Doctrine\ORM\EntityManager')
73
            ->disableOriginalConstructor()
74
            ->getMock()
75
        ;
76
        $entityManagerMock
77
            ->expects($this->any())
78
            ->method('getRepository')
79
            ->willReturnCallback([$this, 'getRepositoryMock'])
80
        ;
81
        $entityManagerMock
82
            ->expects($this->any())
83
            ->method('persist')
84
            ->willReturn(true)
85
        ;
86
        $entityManagerMock
87
            ->expects($this->any())
88
            ->method('flush')
89
            ->willReturn(true)
90
        ;
91
        return $entityManagerMock;
92
    }
93
94
    public function getRepositoryMock() {
95
        $repositoryMock = $this
96
            ->getMockBuilder('Developtech\AgilityBundle\Repository\FeedbackRepository')
97
            ->disableOriginalConstructor()
98
            ->setMethods([
99
                'find',
100
                'findBy',
101
                'findByProject'
102
            ])
103
            ->getMock()
104
        ;
105
        $repositoryMock
106
            ->expects($this->any())
107
            ->method('findByProject')
108
            ->willReturnCallback([$this, 'getFeedbacksMock'])
109
        ;
110
        $repositoryMock
111
            ->expects($this->any())
112
            ->method('findBy')
113
            ->willReturnCallback([$this, 'getFeedbacksWithAuthorMock'])
114
        ;
115
        $repositoryMock
116
            ->expects($this->any())
117
            ->method('find')
118
            ->willReturnCallback([$this, 'getFeedbackMock'])
119
        ;
120
        return $repositoryMock;
121
    }
122
123
    public function getFeedbackMock($id) {
124
        if($id === 2) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after IF keyword; 0 found
Loading history...
125
            return null;
126
        }
127
        return
128
            (new Feedback())
129
            ->setId(1)
130
            ->setName('I can\'t see the calendar')
131
            ->setSlug('i-can-t-see-the-calendar')
132
            ->setDescription('Add brightness to this calendar !')
133
            ->setProject(new Project())
134
            ->setAuthor(new User())
135
            ->setDeveloper(new User())
136
            ->setCreatedAt(new \DateTime())
137
            ->setUpdatedAt(new \DateTime())
138
        ;
139
    }
140
141
    public function getFeedbacksMock() {
142
        return [
143
            (new Feedback())
144
            ->setId(1)
145
            ->setName('I can\'t see the calendar')
146
            ->setSlug('i-can-t-see-the-calendar')
147
            ->setDescription('Add brightness to this calendar !')
148
            ->setProject(new Project())
149
            ->setAuthor(new User())
150
            ->setDeveloper(new User())
151
            ->setCreatedAt(new \DateTime())
152
            ->setUpdatedAt(new \DateTime()),
153
            (new Feedback())
154
            ->setId(2)
155
            ->setName('The calendar is not shiny enough')
156
            ->setSlug('the-calendar-is-not-shiny-enough')
157
            ->setDescription('This calendar blew my eyes away !')
158
            ->setProject(new Project())
159
            ->setAuthor(new User())
160
            ->setDeveloper(new User())
161
            ->setCreatedAt(new \DateTime())
162
            ->setUpdatedAt(new \DateTime()),
163
        ];
164
    }
165
166
    public function getFeedbacksWithAuthorMock() {
167
        return [
168
            (new Feedback())
169
            ->setId(1)
170
            ->setName('I can\'t see the calendar')
171
            ->setSlug('i-can-t-see-the-calendar')
172
            ->setDescription('Add brightness to this calendar !')
173
            ->setProject(new Project())
174
            ->setAuthor(new User('Hagrid'))
175
            ->setDeveloper(new User())
176
            ->setCreatedAt(new \DateTime())
177
            ->setUpdatedAt(new \DateTime()),
178
            (new Feedback())
179
            ->setId(2)
180
            ->setName('The calendar is not shiny enough')
181
            ->setSlug('the-calendar-is-not-shiny-enough')
182
            ->setDescription('This calendar blew my eyes away !')
183
            ->setProject(new Project())
184
            ->setAuthor(new User('Hagrid'))
185
            ->setDeveloper(new User())
186
            ->setCreatedAt(new \DateTime())
187
            ->setUpdatedAt(new \DateTime()),
188
        ];
189
    }
190
}
191