Completed
Push — develop ( 8eb671...133594 )
by Mike
19:30 queued 09:24
created

Transformer/Template/CollectionTest.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * phpDocumentor
4
 *
5
 * PHP Version 5.3
6
 *
7
 * @copyright 2010-2018 Mike van Riel / Naenius (http://www.naenius.com)
8
 * @license   http://www.opensource.org/licenses/mit-license.php MIT
9
 * @link      http://phpdoc.org
10
 */
11
12
namespace phpDocumentor\Transformer\Template;
13
14
use Mockery as m;
15
use phpDocumentor\Transformer\Template;
16
use phpDocumentor\Transformer\Transformation;
17
18
class CollectionTest extends \Mockery\Adapter\Phpunit\MockeryTestCase
19
{
20
    /** @var m\MockInterface|\phpDocumentor\Transformer\Writer\Collection */
21
    private $writerCollectionMock;
22
23
    /** @var m\MockInterface|Factory */
24
    private $factoryMock;
25
26
    /** @var Collection */
27
    private $fixture;
28
29
    /**
30
     * Constructs the fixture with provided mocked dependencies.
31
     */
32
    protected function setUp()
33
    {
34
        $this->factoryMock = m::mock('phpDocumentor\Transformer\Template\Factory');
35
        $this->writerCollectionMock = m::mock('phpDocumentor\Transformer\Writer\Collection');
36
        $this->fixture = new Collection($this->factoryMock, $this->writerCollectionMock);
37
    }
38
39
    /**
40
     * @covers \phpDocumentor\Transformer\Template\Collection::__construct
41
     */
42
    public function testIfDependenciesAreRegisteredOnInitialization()
43
    {
44
        $this->assertAttributeSame($this->factoryMock, 'factory', $this->fixture);
45
    }
46
47
    /**
48
     * @covers \phpDocumentor\Transformer\Template\Collection::load
49
     */
50
    public function testIfLoadRetrievesTemplateFromFactoryAndRegistersIt()
51
    {
52
        // Arrange
53
        $templateName = 'default';
54
        $template = new Template($templateName);
55
        $this->factoryMock->shouldReceive('get')->with($templateName)->andReturn($template);
0 ignored issues
show
The method shouldReceive does only exist in Mockery\MockInterface, but not in phpDocumentor\Transformer\Template\Factory.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
56
57
        // Act
58
        $this->fixture->load($templateName);
59
60
        // Assert
61
        $this->assertCount(1, $this->fixture);
62
        $this->assertArrayHasKey($templateName, $this->fixture);
63
        $this->assertSame($template, $this->fixture[$templateName]);
64
    }
65
66
    /**
67
     * @covers \phpDocumentor\Transformer\Template\Collection::getTemplatesPath
68
     */
69
    public function testCollectionProvidesTemplatePath()
70
    {
71
        // Arrange
72
        $path = '/tmp';
73
        $this->factoryMock->shouldReceive('getTemplatePath')->andReturn($path);
0 ignored issues
show
The method shouldReceive does only exist in Mockery\MockInterface, but not in phpDocumentor\Transformer\Template\Factory.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
74
75
        // Act
76
        $result = $this->fixture->getTemplatesPath();
77
78
        // Assert
79
        $this->assertSame($path, $result);
80
    }
81
82
    /**
83
     * @covers \phpDocumentor\Transformer\Template\Collection::getTransformations
84
     */
85
    public function testIfAllTransformationsCanBeRetrieved()
86
    {
87
        // Arrange
88
        $transformation1 = $this->givenAnEmptyTransformation();
89
        $transformation2 = $this->givenAnEmptyTransformation();
90
        $transformation3 = $this->givenAnEmptyTransformation();
91
        $this->whenThereIsATemplateWithNameAndTransformations(
92
            'template1',
93
            ['a' => $transformation1, 'b' => $transformation2]
94
        );
95
        $this->whenThereIsATemplateWithNameAndTransformations('template2', ['c' => $transformation3]);
96
97
        // Act
98
        $result = $this->fixture->getTransformations();
99
100
        // Assert
101
        $this->assertCount(3, $result);
102
        $this->assertSame([$transformation1, $transformation2, $transformation3], $result);
103
    }
104
105
    /**
106
     * Returns a transformation object without information in it.
107
     *
108
     * @return Transformation
109
     */
110
    protected function givenAnEmptyTransformation()
111
    {
112
        return new Transformation('', '', '', '');
113
    }
114
115
    /**
116
     * Adds a template to the fixture with the given name and transformations.
117
     *
118
     * @param string           $name
119
     * @param Transformation[] $transformations
120
     */
121
    protected function whenThereIsATemplateWithNameAndTransformations($name, array $transformations)
122
    {
123
        $template = new Template($name);
124
        foreach ($transformations as $key => $transformation) {
125
            $template[$key] = $transformation;
126
        }
127
128
        $this->fixture[$name] = $template;
129
    }
130
}
131