Completed
Push — develop ( 67be7e...e4ed82 )
by Jaap
03:36
created

Compiler/Pass/MarkerFromTagsExtractorTest.php (1 issue)

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-2014 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\Compiler\Pass;
13
14
use phpDocumentor\Descriptor\ClassDescriptor;
15
use phpDocumentor\Descriptor\Collection;
16
use phpDocumentor\Descriptor\DescriptorAbstract;
17
use phpDocumentor\Descriptor\FileDescriptor;
18
use phpDocumentor\Descriptor\ProjectDescriptor;
19
use phpDocumentor\Descriptor\TagDescriptor;
20
21
class MarkerFromTagsExtractorTest extends \PHPUnit_Framework_TestCase
22
{
23
    /** @var MarkerFromTagsExtractor */
24
    protected $fixture;
25
26
    /** @var ProjectDescriptor */
27
    protected $project;
28
29
    /**
30
     * Initialize the fixture for this test.
31
     */
32
    protected function setUp()
33
    {
34
        $this->fixture = new MarkerFromTagsExtractor();
35
        $this->project = new ProjectDescriptor('MyProject');
36
    }
37
38
    /**
39
     * @covers phpDocumentor\Compiler\Pass\MarkerFromTagsExtractor::getDescription
40
     */
41
    public function testDescriptionReturnsCorrectString()
42
    {
43
        $this->assertSame('Collect all markers embedded in tags', $this->fixture->getDescription());
44
    }
45
46
    /**
47
     * @covers phpDocumentor\Compiler\Pass\MarkerFromTagsExtractor::execute
48
     * @covers phpDocumentor\Compiler\Pass\MarkerFromTagsExtractor::getFileDescriptor
49
     * @covers phpDocumentor\Compiler\Pass\MarkerFromTagsExtractor::addTodoMarkerToFile
50
     */
51
    public function testAddTodoMarkerForEachTodoTagInAnyElement()
52
    {
53
        $fileDescriptor = $this->givenProjectHasFileDescriptor();
54
        $fileDescriptor->setLine(10);
55
        $this->givenDescriptorHasTodoTagWithDescription($fileDescriptor, '123');
56
        $this->givenDescriptorHasTodoTagWithDescription($fileDescriptor, '456');
57
        $classDescriptor = $this->givenProjectHasClassDescriptorAssociatedWithFile($fileDescriptor);
58
        $classDescriptor->setLine(20);
59
        $this->givenDescriptorHasTodoTagWithDescription($classDescriptor, '789');
60
61
        $this->fixture->execute($this->project);
62
63
        $this->assertCount(2, $fileDescriptor->getTags()->get('todo'));
64
        $this->assertCount(1, $classDescriptor->getTags()->get('todo'));
65
        $this->assertCount(3, $fileDescriptor->getMarkers());
66
        $this->assertSame(
67
            array('type' => 'TODO', 'message' => '123', 'line' => 10),
68
            $fileDescriptor->getMarkers()->get(0)
69
        );
70
        $this->assertSame(
71
            array('type' => 'TODO', 'message' => '456', 'line' => 10),
72
            $fileDescriptor->getMarkers()->get(1)
73
        );
74
        $this->assertSame(
75
            array('type' => 'TODO', 'message' => '789', 'line' => 20),
76
            $fileDescriptor->getMarkers()->get(2)
77
        );
78
    }
79
80
    /**
81
     * @covers phpDocumentor\Compiler\Pass\MarkerFromTagsExtractor::execute
82
     * @covers phpDocumentor\Compiler\Pass\MarkerFromTagsExtractor::getFileDescriptor
83
     */
84
    public function testExceptionShouldBeThrownIfElementHasNoFileAssociated()
85
    {
86
        $classDescriptor = $this->givenProjectHasClassDescriptorAssociatedWithFile(null);
87
        $this->givenDescriptorHasTodoTagWithDescription($classDescriptor, '789');
88
89
        $this->setExpectedException(
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::setExpectedException() has been deprecated with message: Method deprecated since Release 5.2.0; use expectException() instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
90
            'UnexpectedValueException',
91
            'An element should always have a file associated with it'
92
        );
93
94
        $this->fixture->execute($this->project);
95
    }
96
97
    /**
98
     * @return FileDescriptor
99
     */
100
    protected function givenProjectHasFileDescriptor()
101
    {
102
        $fileDescriptor1 = new FileDescriptor('123');
103
        $elementIndex = $this->project->getIndexes()->get('elements', new Collection());
104
        $elementIndex->add($fileDescriptor1);
105
        return $fileDescriptor1;
106
    }
107
108
    /**
109
     * @param DescriptorAbstract $descriptor
110
     * @param string             $description
111
     */
112
    protected function givenDescriptorHasTodoTagWithDescription($descriptor, $description)
113
    {
114
        $todoTag = new TagDescriptor('todo');
115
        $todoTag->setDescription($description);
116
117
        $todoTags = $descriptor->getTags()->get('todo', array());
118
        $todoTags[] = $todoTag;
119
        $descriptor->getTags()->set('todo', $todoTags);
120
    }
121
122
    /**
123
     * Adds a class descriptor to the project's elements and add a parent file.
124
     *
125
     * @param FileDescriptor $fileDescriptor
126
     *
127
     * @return ClassDescriptor
128
     */
129
    protected function givenProjectHasClassDescriptorAssociatedWithFile($fileDescriptor)
130
    {
131
        $classDescriptor = new ClassDescriptor();
132
        if ($fileDescriptor) {
133
            $classDescriptor->setFile($fileDescriptor);
134
        }
135
        $elementIndex = $this->project->getIndexes()->get('elements', new Collection());
136
        $elementIndex->add($classDescriptor);
137
        return $classDescriptor;
138
    }
139
}
140