Completed
Push — develop ( 373768...b82813 )
by Mike
05:50
created

FactoryTest::testRetrieveInstantiatedTemplate()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 61

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 61
rs 8.8509
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 org\bovigo\vfs\vfsStream;
16
use phpDocumentor\Transformer\Template;
17
18
class FactoryTest extends \Mockery\Adapter\Phpunit\MockeryTestCase
19
{
20
    /** @var m\MockInterface|PathResolver */
21
    private $pathResolverMock;
22
23
    /** @var Factory */
24
    private $fixture;
25
26
    /**
27
     * Sets up the fixture with mocked dependencies.
28
     */
29
    protected function setUp()
30
    {
31
        $this->pathResolverMock = m::mock('phpDocumentor\Transformer\Template\PathResolver');
32
33
        $this->fixture = new Factory($this->pathResolverMock);
34
    }
35
36
    /**
37
     * @covers phpDocumentor\Transformer\Template\Factory::__construct
38
     */
39
    public function testIfDependenciesAreCorrectlyRegisteredOnInitialization()
40
    {
41
        $this->assertAttributeSame($this->pathResolverMock, 'pathResolver', $this->fixture);
42
    }
43
44
    /**
45
     * @covers phpDocumentor\Transformer\Template\Factory::get
46
     * @covers phpDocumentor\Transformer\Template\Factory::fetchTemplateXmlFromPath
47
     * @covers phpDocumentor\Transformer\Template\Factory::createTemplateFromXml
48
     * @todo test parameters in template and transformations
49
     */
50
    public function testRetrieveInstantiatedTemplate()
51
    {
52
        // Arrange
53
        $templateName = 'clean';
54
        $xml = <<<'XML'
55
<?xml version="1.0" encoding="utf-8"?>
56
<template>
57
  <name>clean</name>
58
  <author>Mike van Riel</author>
59
  <email>[email protected]</email>
60
  <version>1.0.0</version>
61
  <copyright>Mike van Riel 2013</copyright>
62
  <description><![CDATA[This is the description]]></description>
63
  <transformations>
64
    <transformation query="copy" writer="FileIo" source="templates/clean/htaccess.dist" artifact=".htaccess"/>
65
    <transformation query="copy" writer="FileIo" source="templates/clean/images" artifact="images"/>
66
    <transformation query="copy" writer="FileIo" source="templates/clean/css" artifact="css"/>
67
    <transformation query="copy" writer="FileIo" source="templates/clean/js" artifact="js"/>
68
    <transformation query="copy" writer="FileIo" source="templates/clean/font" artifact="font"/>
69
    <transformation writer="twig" query="namespace" source="templates/clean/namespace.html.twig" artifact="index.html"/>
70
    <transformation writer="twig" query="indexes.namespaces" source="templates/clean/namespace.html.twig" />
71
    <transformation writer="twig" query="indexes.classes" source="templates/clean/class.html.twig" />
72
    <transformation writer="twig" query="indexes.interfaces" source="templates/clean/interface.html.twig" />
73
    <transformation writer="twig" query="indexes.traits" source="templates/clean/class.html.twig" />
74
    <transformation writer="twig" query="files" source="templates/clean/file.html.twig" />
75
    <transformation 
76
        writer="twig" 
77
        query="files" 
78
        source="templates/clean/file.source.txt.twig" 
79
        artifact="files/{{path}}.txt"
80
    />
81
    <transformation writer="twig" source="templates/clean/reports/markers.html.twig" artifact="reports/markers.html"/>
82
    <transformation writer="twig" source="templates/clean/reports/errors.html.twig" artifact="reports/errors.html"/>
83
    <transformation 
84
        writer="twig" 
85
        source="templates/clean/reports/deprecated.html.twig" 
86
        artifact="reports/deprecated.html"
87
    />
88
    <transformation writer="twig" source="templates/clean/graphs/class.html.twig" artifact="graphs/class.html"/>
89
    <transformation writer="Graph" source="Class" artifact="graphs/classes.svg" />
90
  </transformations>
91
</template>
92
XML;
93
        vfsStream::setup('exampleDir')->addChild(vfsStream::newFile('template.xml')->setContent($xml));
94
        $this->pathResolverMock->shouldReceive('resolve')->with($templateName)->andReturn(vfsStream::url('exampleDir'));
0 ignored issues
show
Bug introduced by
The method shouldReceive does only exist in Mockery\MockInterface, but not in phpDocumentor\Transformer\Template\PathResolver.

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...
95
96
        // Act
97
        $result = $this->fixture->get($templateName);
98
99
        // Assert
100
        $this->assertSame($templateName, $result->getName());
101
        $this->assertSame('Mike van Riel <[email protected]>', $result->getAuthor());
102
        $this->assertSame('1.0.0', $result->getVersion());
103
        $this->assertSame('Mike van Riel 2013', $result->getCopyright());
104
        $this->assertSame('This is the description', $result->getDescription());
105
        $this->assertSame(17, $result->count());
106
        $this->assertSame('copy', $result[0]->getQuery());
107
        $this->assertSame('FileIo', $result[0]->getWriter());
108
        $this->assertSame('templates/clean/htaccess.dist', $result[0]->getSource());
109
        $this->assertSame('.htaccess', $result[0]->getArtifact());
110
    }
111
112
    /**
113
     * @covers phpDocumentor\Transformer\Template\Factory::getTemplatePath
114
     */
115
    public function testReturnTemplatePathFromResolver()
116
    {
117
        // Arrange
118
        $expected = 'test';
119
        $this->pathResolverMock->shouldReceive('getTemplatePath')->andReturn($expected);
0 ignored issues
show
Bug introduced by
The method shouldReceive does only exist in Mockery\MockInterface, but not in phpDocumentor\Transformer\Template\PathResolver.

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...
120
121
        // Act
122
        $result = $this->fixture->getTemplatePath();
123
124
        // Assert
125
        $this->assertSame($expected, $result);
126
    }
127
128
    /**
129
     * @covers phpDocumentor\Transformer\Template\Factory::getAllNames
130
     */
131
    public function testRetrieveAllTemplateNames()
132
    {
133
        // Arrange
134
        $expected = ['template1', 'template2'];
135
        $root = vfsStream::setup('exampleDir');
136
        $root->addChild(vfsStream::newDirectory($expected[0]));
137
        $root->addChild(vfsStream::newFile('aFile.txt'));
138
        $root->addChild(vfsStream::newDirectory($expected[1]));
139
        $this->pathResolverMock->shouldReceive('getTemplatePath')->andReturn(vfsStream::url('exampleDir'));
0 ignored issues
show
Bug introduced by
The method shouldReceive does only exist in Mockery\MockInterface, but not in phpDocumentor\Transformer\Template\PathResolver.

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...
140
141
        // Act
142
        $result = $this->fixture->getAllNames();
143
144
        // Assert
145
        $this->assertSame($expected, $result);
146
    }
147
}
148