Completed
Push — develop ( 80740b...61b5c3 )
by Mike
10:20
created

StatisticsTest::givenAProjectDescriptor()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * phpDocumentor
5
 *
6
 * PHP Version 5.3
7
 *
8
 * @author    Pascal de Vink <[email protected]>
9
 * @copyright 2010-2018 Mike van Riel / Naenius (http://www.naenius.com)
10
 * @license   http://www.opensource.org/licenses/mit-license.php MIT
11
 * @link      http://phpdoc.org
12
 */
13
14
namespace phpDocumentor\Transformer\Writer;
15
16
use Mockery as m;
17
use org\bovigo\vfs\vfsStream;
18
use org\bovigo\vfs\vfsStreamDirectory;
19
20
/**
21
 * Test class for \phpDocumentor\Transformer\Writer\Statistics.
22
 */
23
class StatisticsTest extends \Mockery\Adapter\Phpunit\MockeryTestCase
24
{
25
    /**
26
     * @var Statistics
27
     */
28
    protected $statistics;
29
30
    /** @var vfsStreamDirectory */
31
    private $fs;
32
33
    /**
34
     * Sets up the test suite
35
     */
36
    protected function setUp()
37
    {
38
        $this->statistics = $this->getMockBuilder('phpDocumentor\Transformer\Writer\Statistics')
39
            ->setMethods(['getDestinationPath'])
40
            ->getMock();
41
        $this->statistics->method('getDestinationPath')
42
            ->willReturn(vfsStream::url('StatisticsTest/artifact.xml'));
43
44
        $this->fs = vfsStream::setup('StatisticsTest');
45
    }
46
47
    public function testTransformWithStartingArtifactAsString()
48
    {
49
        $markerCount = 1;
50
        $transformer = $this->givenATransformer();
51
        $error = $this->givenAnError();
52
        $fileDescriptor = $this->givenAFileDescriptor([$error], $markerCount);
53
        $projectDescriptor = $this->givenAProjectDescriptor($fileDescriptor);
54
55
        $this->statistics->transform($projectDescriptor, $transformer);
56
57
        // Assert file exists
58
        $this->assertTrue($this->fs->hasChild('artifact.xml'));
59
60
        // Inspect XML
61
        $now = new \DateTime('now');
62
        $date = $now->format(DATE_ATOM);
63
64
        $this->thenTheXmlReportShouldContain($date, 1, 1, 1, $markerCount);
65
    }
66
67
    public function testTransformWithStartingArtifactAsFile()
68
    {
69
        $version = trim(file_get_contents(__DIR__ . '/../../../../../VERSION'));
70
        $statsXml = '<?xml version="1.0"?><phpdoc-stats version="' . $version . '"></phpdoc-stats>';
71
        vfsStream::create(['artifact.xml' => $statsXml]);
72
73
        $markerCount = 12;
74
        $transformer = $this->givenATransformer();
75
        $error = $this->givenAnError();
76
        $fileDescriptor = $this->givenAFileDescriptor([$error, $error], $markerCount);
77
        $projectDescriptor = $this->givenAProjectDescriptor($fileDescriptor);
78
79
        $this->statistics->transform($projectDescriptor, $transformer);
80
81
        // Assert file exists
82
        $this->assertTrue($this->fs->hasChild('artifact.xml'));
83
84
        // Inspect XML
85
        $now = new \DateTime('now');
86
        $date = $now->format(DATE_ATOM);
87
88
        $this->thenTheXmlReportShouldContain($date, 1, 1, 2, $markerCount);
89
    }
90
91
    /**
92
     * @return m\MockInterface
93
     */
94
    private function givenAProjectDescriptor(m\MockInterface $fileDescriptor)
95
    {
96
        $projectDescriptor = m::mock('phpDocumentor\Descriptor\ProjectDescriptor');
97
        $projectDescriptor->shouldReceive('getFiles->getAll')->andReturn([$fileDescriptor]);
0 ignored issues
show
Bug introduced by
The method andReturn does only exist in Mockery\ExpectationInterface, but not in Mockery\HigherOrderMessage.

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...
98
        $projectDescriptor->shouldReceive('getFiles->count')->andReturn(1);
99
        $projectDescriptor->shouldReceive('getIndexes->get')->andReturn([$fileDescriptor]);
100
        return $projectDescriptor;
101
    }
102
103
    /**
104
     * @param int $markerCount
105
     * @return m\MockInterface
106
     */
107
    private function givenAFileDescriptor(array $errors, $markerCount)
108
    {
109
        $fileDescriptor = m::mock('phpDocumentor\Descriptor\FileDescriptor');
110
        $fileDescriptor->shouldReceive('isDeprecated')->andReturn(true);
0 ignored issues
show
Bug introduced by
The method andReturn does only exist in Mockery\ExpectationInterface, but not in Mockery\HigherOrderMessage.

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...
111
        $fileDescriptor->shouldReceive('getAllErrors->getAll')->andReturn($errors);
112
        $fileDescriptor->shouldReceive('getMarkers->count')->andReturn($markerCount);
113
        return $fileDescriptor;
114
    }
115
116
    /**
117
     * @return m\MockInterface
118
     */
119
    private function givenATransformer()
120
    {
121
        $transformer = m::mock('phpDocumentor\Transformer\Transformation');
122
        $transformer->shouldReceive('getTransformer->getTarget')->andReturn(vfsStream::url('StatisticsTest'));
0 ignored issues
show
Bug introduced by
The method andReturn does only exist in Mockery\ExpectationInterface, but not in Mockery\HigherOrderMessage.

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...
123
        $transformer->shouldReceive('getArtifact')->andReturn('artifact.xml');
124
        return $transformer;
125
    }
126
127
    /**
128
     * @return m\MockInterface
129
     */
130
    private function givenAnError()
131
    {
132
        return m::mock('phpDocumentor\Descriptor\Validator\Error');
133
    }
134
135
    private function thenTheXmlReportShouldContain(
136
        $date,
137
        $numberOfFiles,
138
        $numberOfDeprecated,
139
        $numberOfErrors,
140
        $numberOfMarkers
141
    ) {
142
        $version = trim(file_get_contents(__DIR__ . '/../../../../../VERSION'));
143
144
        $expectedXml = new \DOMDocument();
145
        $expectedXml->loadXML(
146
            '<?xml version="1.0"?>
147
<phpdoc-stats version="' . $version . '">
148
  <stat date="' . $date . '">
149
    <counters>
150
        <files>' . $numberOfFiles . '</files>
151
        <deprecated>' . $numberOfDeprecated . '</deprecated>
152
        <errors>' . $numberOfErrors . '</errors>
153
        <markers>' . $numberOfMarkers . '</markers>
154
    </counters>
155
</stat>
156
</phpdoc-stats>'
157
        );
158
159
        $actualXml = new \DOMDocument();
160
        $actualXml->load(vfsStream::url('StatisticsTest' . DIRECTORY_SEPARATOR . 'artifact.xml'));
161
162
        $actualTime = $this->getGeneratedDateTime($actualXml);
163
        $expectedTime = $this->getGeneratedDateTime($expectedXml);
164
        $diff = $actualTime->diff($expectedTime, true);
165
166
        // overwrite to prevent timing issues, otherwise the test might fail due to a second difference
167
        $this->setGeneratedDateTime($actualXml, $expectedTime);
168
169
        // time could have switch a second in between; verify within a range of 2 seconds
170
        $this->assertLessThanOrEqual(2, $diff->s);
171
        $this->assertEqualXMLStructure($expectedXml->firstChild, $actualXml->firstChild, true);
172
        $this->assertSame($expectedXml->saveXML(), $actualXml->saveXML());
173
    }
174
175
    private function getGeneratedDateTime(\DOMDocument $actualXml): \DateTime
176
    {
177
        return new \DateTime(
178
            $actualXml->getElementsByTagName('stat')->item(0)->attributes->getNamedItem('date')->nodeValue
179
        );
180
    }
181
182
    private function setGeneratedDateTime(\DOMDocument $actualXml, \DateTime $dateTime)
183
    {
184
        $actualXml->getElementsByTagName('stat')->item(0)->attributes->getNamedItem('date')->nodeValue
185
            = $dateTime->format(DATE_ATOM);
186
    }
187
}
188