ProjectManagerTest::setUp()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Developtech\AgilityBundle\Tests\Manager;
4
5
use Developtech\AgilityBundle\Manager\ProjectManager;
6
7
use Developtech\AgilityBundle\Entity\Project;
8
use Developtech\AgilityBundle\Tests\Mock\User;
9
10
use Developtech\AgilityBundle\Utils\Slugger;
11
12
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
13
14
class ProjectManagerTest extends \PHPUnit_Framework_TestCase {
15
    /** @var ProjectManager **/
16
    protected $manager;
17
18
    public function setUp() {
19
        $this->manager = new ProjectManager($this->getEntityManagerMock(), new Slugger(), Project::class);
0 ignored issues
show
Unused Code introduced by
The call to ProjectManager::__construct() has too many arguments starting with \Developtech\AgilityBundle\Entity\Project::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...
20
    }
21
22
    public function testGetProjects() {
23
        $projects = $this->manager->getProjects();
24
25
        $this->assertCount(3, $projects);
26
        $this->assertInstanceOf(Project::class, $projects[0]);
27
    }
28
29
    public function testGetProject() {
30
        $project = $this->manager->getProject('great-project');
31
32
        $this->assertInstanceOf(Project::class, $project);
33
        $this->assertEquals(3, $project->getId());
34
        $this->assertEquals('Great project', $project->getName());
35
        $this->assertEquals('great-project', $project->getSlug());
36
    }
37
38
    public function testCreateProject() {
39
        $project = $this->manager->createProject('Great project', new User());
40
41
        $this->assertInstanceOf(Project::class, $project);
42
        $this->assertEquals('Great project', $project->getName());
43
        $this->assertEquals('great-project', $project->getSlug());
44
        $this->assertInstanceOf(User::class, $project->getProductOwner());
45
    }
46
47
    public function testEditProject() {
48
        $project = $this->manager->editProject(3, 'Updated project', 'open', 10);
49
50
        $this->assertInstanceOf(Project::class, $project);
51
        $this->assertEquals(3, $project->getId());
52
        $this->assertEquals('Updated project', $project->getName());
53
        $this->assertEquals('updated-project', $project->getSlug());
54
        $this->assertEquals('open', $project->getBetaTestStatus());
55
        $this->assertEquals(10, $project->getNbBetaTesters());
56
    }
57
58
    /**
59
     * @expectedException \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
60
     * @expectedExceptionMessage Project not found
61
     */
62
    public function testEditProjectWithUnexistingProject() {
63
        $this->manager->editProject(2, 'Unknown project', 'closed', 0);
64
    }
65
66
    public function getEntityManagerMock() {
67
        $entityManagerMock = $this
68
            ->getMockBuilder('Doctrine\ORM\EntityManager')
69
            ->disableOriginalConstructor()
70
            ->getMock()
71
        ;
72
        $entityManagerMock
73
            ->expects($this->any())
74
            ->method('getRepository')
75
            ->willReturnCallback([$this, 'getRepositoryMock'])
76
        ;
77
        $entityManagerMock
78
            ->expects($this->any())
79
            ->method('persist')
80
            ->willReturn(true)
81
        ;
82
        $entityManagerMock
83
            ->expects($this->any())
84
            ->method('flush')
85
            ->willReturn(true)
86
        ;
87
        return $entityManagerMock;
88
    }
89
90
    public function getRepositoryMock() {
91
        $repositoryMock = $this
92
            ->getMockBuilder('Developtech\AgilityBundle\Repository\ProjectRepository')
93
            ->disableOriginalConstructor()
94
            ->setMethods([
95
                'find',
96
                'findOneBySlug',
97
                'findAll'
98
            ])
99
            ->getMock()
100
        ;
101
        $repositoryMock
102
            ->expects($this->any())
103
            ->method('find')
104
            ->willReturnCallback([$this, 'getProjectMock'])
105
        ;
106
        $repositoryMock
107
            ->expects($this->any())
108
            ->method('findOneBySlug')
109
            ->willReturnCallback([$this, 'getProjectMock'])
110
        ;
111
        $repositoryMock
112
            ->expects($this->any())
113
            ->method('findAll')
114
            ->willReturnCallback([$this, 'getProjectsMock'])
115
        ;
116
        return $repositoryMock;
117
    }
118
119
    /**
120
     * @param mixed $id
121
     * @return Project|null
122
     */
123
    public function getProjectMock($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 Project())
129
            ->setId(3)
130
            ->setName('Great project')
131
            ->setSlug('great-project')
132
            ->setCreatedAt(new \DateTime())
133
            ->setNbBetaTesters(12)
134
            ->setBetaTestStatus('open')
135
            ->setProductOwner(new User())
136
        ;
137
    }
138
139
    public function getProjectsMock() {
140
        return  [
141
            (new Project())
142
            ->setName('Great Project')
143
            ->setSlug('great-project')
144
            ->setCreatedAt(new \DateTime())
145
            ->setNbBetaTesters(12)
146
            ->setBetaTestStatus('open'),
147
            (new Project())
148
            ->setName('Bloody Project')
149
            ->setSlug('bloody-project')
150
            ->setCreatedAt(new \DateTime())
151
            ->setNbBetaTesters(17)
152
            ->setBetaTestStatus('closed'),
153
            (new Project())
154
            ->setName('Messy Project')
155
            ->setSlug('messy-project')
156
            ->setCreatedAt(new \DateTime())
157
            ->setNbBetaTesters(24)
158
            ->setBetaTestStatus('closed'),
159
        ];
160
    }
161
}
162