Completed
Push — master ( 984bef...668191 )
by Michael
04:42
created

testLicenseeActionWithOneLicensee()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 10
nc 1
nop 0
1
<?php
2
/* Copyright (C) 2017 Michael Giesler
3
 *
4
 * This file is part of Dembelo.
5
 *
6
 * Dembelo is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU Affero General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * Dembelo is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
 * GNU Affero General Public License 3 for more details.
15
 *
16
 * You should have received a copy of the GNU Affero General Public License 3
17
 * along with Dembelo. If not, see <http://www.gnu.org/licenses/>.
18
 */
19
20
namespace AdminBundle\Tests\Controller;
21
22
use AdminBundle\Controller\LicenseeController;
23
use DembeloMain\Document\Licensee;
24
use DembeloMain\Model\Repository\Doctrine\ODM\LicenseeRepository;
25
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
26
use Symfony\Component\HttpFoundation\Response;
27
28
/**
29
 * Class LicenseeControllerTest
30
 * @package AdminBundle\Tests\Controller
31
 */
32
class LicenseeControllerTest extends WebTestCase
33
{
34
    /**
35
     * @var LicenseeController
36
     */
37
    private $controller;
38
39
    /**
40
     * @var \PHPUnit_Framework_MockObject_MockObject|LicenseeRepository
41
     */
42
    private $licenseeRepositoryMock;
43
44
    /**
45
     * @return void
46
     */
47
    protected function setUp(): void
48
    {
49
        $this->licenseeRepositoryMock = $this->createLicenseeRepositoryMock();
50
51
        $this->controller = new LicenseeController($this->licenseeRepositoryMock);
52
    }
53
54
    /**
55
     * tests licenseeAction with no licensees
56
     * @return void
57
     */
58
    public function testLicenseeActionWithNoLicensees(): void
59
    {
60
        $this->licenseeRepositoryMock->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not exist on DembeloMain\Model\Reposi...\ODM\LicenseeRepository. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

60
        $this->licenseeRepositoryMock->/** @scrutinizer ignore-call */ 
61
                                       expects($this->once())
Loading history...
61
            ->method('findAll')
62
            ->willReturn([]);
63
64
        /* @var $response \Symfony\Component\HttpFoundation\Response */
65
        $response = $this->controller->licenseesAction();
66
        $this->assertInstanceOf(Response::class, $response);
67
        $this->assertJsonStringEqualsJsonString('[]', $response->getContent());
68
        $this->assertEquals('200', $response->getStatusCode());
69
    }
70
71
    /**
72
     * tests licenseeAction() with one licensee
73
     * @return void
74
     */
75
    public function testLicenseeActionWithOneLicensee(): void
76
    {
77
        $licensee = new Licensee();
78
        $licensee->setName('someName');
79
        $licensee->setId('someId');
80
81
        $this->licenseeRepositoryMock->expects($this->once())
82
            ->method('findAll')
83
            ->willReturn([$licensee]);
84
85
        /* @var $response \Symfony\Component\HttpFoundation\Response */
86
        $response = $this->controller->licenseesAction();
87
        $this->assertInstanceOf(Response::class, $response);
88
        $this->assertJsonStringEqualsJsonString('[{"id":"someId","name":"someName"}]', $response->getContent());
89
        $this->assertEquals('200', $response->getStatusCode());
90
    }
91
92
    /**
93
     * @return \PHPUnit_Framework_MockObject_MockObject|LicenseeRepository
94
     */
95 View Code Duplication
    private function createLicenseeRepositoryMock(): LicenseeRepository
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
96
    {
97
        $repository = $this->getMockBuilder(LicenseeRepository::class)
98
            ->disableOriginalConstructor()
99
            ->setMethods(['findAll', 'save', 'find', 'findBy', 'findOneBy', 'getClassName', 'findOneByName'])
100
            ->getMock();
101
102
        return $repository;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $repository returns the type PHPUnit_Framework_MockObject_MockObject which is incompatible with the type-hinted return DembeloMain\Model\Reposi...\ODM\LicenseeRepository.
Loading history...
103
    }
104
}
105