Completed
Push — develop ( fbdd82...317691 )
by Mike
09:29
created

FactoryTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 4
c 1
b 0
f 1
lcom 1
cbo 10
dl 0
loc 78
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 5 1
A itCreatesAReadModelFromADefinitionAndDocumentation() 0 18 1
A itCanApplyFiltersWhenCreatingAReadModel() 0 20 1
A itShouldThrowAnExceptionIfNoMapperWasFound() 0 11 1
1
<?php
2
/**
3
 * This file is part of phpDocumentor.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 *
8
 * @copyright 2010-2016 Mike van Riel<[email protected]>
9
 * @license   http://www.opensource.org/licenses/mit-license.php MIT
10
 * @link      http://phpdoc.org
11
 */
12
13
namespace phpDocumentor\DomainModel\ReadModel;
14
15
use Mockery as m;
16
use phpDocumentor\DomainModel\Parser\Documentation;
17
use phpDocumentor\DomainModel\Parser\Version\Number;
18
19
/**
20
 * @coversDefaultClass phpDocumentor\DomainModel\ReadModel\Factory
21
 * @covers ::<private>
22
 * @covers ::__construct
23
 */
24
final class FactoryTest extends \PHPUnit_Framework_TestCase
25
{
26
    /** @var Mapper\Factory|m\MockInterface */
27
    private $mapperFactory;
28
29
    /** @var Factory */
30
    private $factory;
31
32
    public function setUp()
33
    {
34
        $this->mapperFactory = m::mock(Mapper\Factory::class);
35
        $this->factory = new Factory($this->mapperFactory);
36
    }
37
38
    /**
39
     * @test
40
     * @covers ::create
41
     */
42
    public function itCreatesAReadModelFromADefinitionAndDocumentation()
43
    {
44
        $type = new Type('all');
45
        $modelName = 'name';
46
        $data = 'data';
47
        $readModelDefinition = new Definition($modelName, $type);
48
        $documentation = new Documentation(new Number('1.0'));
49
50
        $mapper = m::mock(Mapper::class);
51
        $mapper->shouldReceive('create')->with($readModelDefinition, $documentation)->andReturn($data);
52
        $this->mapperFactory->shouldReceive('create')->with($type)->andReturn($mapper);
53
54
        $resultingReadModel = $this->factory->create($readModelDefinition, $documentation);
55
56
        $this->assertInstanceOf(ReadModel::class, $resultingReadModel);
57
        $this->assertSame($modelName, $resultingReadModel->getName());
58
        $this->assertSame($data, $resultingReadModel->getData());
59
    }
60
61
    /**
62
     * @test
63
     * @covers ::create
64
     */
65
    public function itCanApplyFiltersWhenCreatingAReadModel()
66
    {
67
        $filters = [
68
            function ($data) {
69
                return $data . ', more data';
70
            }
71
        ];
72
73
        $mapper = m::mock(Mapper::class);
74
        $mapper->shouldReceive('create')->andReturn('data');
75
        $this->mapperFactory->shouldReceive('create')->andReturn($mapper);
76
77
        $resultingReadModel = $this->factory->create(
78
            new Definition('name', new Type('all'), $filters),
79
            new Documentation(new Number('1.0'))
80
        );
81
82
        $this->assertInstanceOf(ReadModel::class, $resultingReadModel);
83
        $this->assertSame('data, more data', $resultingReadModel->getData());
84
    }
85
86
    /**
87
     * @test
88
     * @covers ::create
89
     */
90
    public function itShouldThrowAnExceptionIfNoMapperWasFound()
91
    {
92
        $this->setExpectedException(\InvalidArgumentException::class);
93
94
        $this->mapperFactory->shouldReceive('create')->andReturnNull();
0 ignored issues
show
Unused Code introduced by
The call to the method Mockery\Expectation::andReturnNull() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
95
96
        $this->factory->create(
97
            new Definition('name', new Type('all')),
98
            new Documentation(new Number('1.0'))
99
        );
100
    }
101
}
102