|
| 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-2015 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\Reflection\DocBlock\Tags; |
| 14 | + |
| 15 | +use Mockery as m; |
| 16 | +use phpDocumentor\Reflection\DocBlock\Description; |
| 17 | +use phpDocumentor\Reflection\DocBlock\DescriptionFactory; |
| 18 | +use phpDocumentor\Reflection\TypeResolver; |
| 19 | +use phpDocumentor\Reflection\Types\Context; |
| 20 | +use phpDocumentor\Reflection\Types\String_; |
| 21 | + |
| 22 | +/** |
| 23 | + * @coversDefaultClass \phpDocumentor\Reflection\DocBlock\Tags\Var_ |
| 24 | + * @covers ::<private> |
| 25 | + */ |
| 26 | +class VarTest extends \PHPUnit_Framework_TestCase |
| 27 | +{ |
| 28 | + /** |
| 29 | + * @uses \phpDocumentor\Reflection\DocBlock\Tags\Var_::__construct |
| 30 | + * @uses \phpDocumentor\Reflection\DocBlock\Description |
| 31 | + * @covers \phpDocumentor\Reflection\DocBlock\Tags\BaseTag::getName |
| 32 | + */ |
| 33 | + public function testIfCorrectTagNameIsReturned() |
| 34 | + { |
| 35 | + $fixture = new Var_('myVariable', null, new Description('Description')); |
| 36 | + |
| 37 | + $this->assertSame('var', $fixture->getName()); |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * @uses \phpDocumentor\Reflection\DocBlock\Tags\Var_::__construct |
| 42 | + * @uses \phpDocumentor\Reflection\DocBlock\Tags\Var_::__toString |
| 43 | + * @uses \phpDocumentor\Reflection\DocBlock\Tags\Formatter\PassthroughFormatter |
| 44 | + * @uses \phpDocumentor\Reflection\DocBlock\Description |
| 45 | + * @covers \phpDocumentor\Reflection\DocBlock\Tags\BaseTag::render |
| 46 | + */ |
| 47 | + public function testIfTagCanBeRenderedUsingDefaultFormatter() |
| 48 | + { |
| 49 | + $fixture = new Var_('myVariable', new String_(), new Description('Description')); |
| 50 | + $this->assertSame('string $myVariable Description', $fixture->render()); |
| 51 | + |
| 52 | + $fixture = new Var_('myVariable', null, new Description('Description')); |
| 53 | + $this->assertSame('$myVariable Description', $fixture->render()); |
| 54 | + |
| 55 | + $fixture = new Var_('myVariable'); |
| 56 | + $this->assertSame('$myVariable', $fixture->render()); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * @uses \phpDocumentor\Reflection\DocBlock\Tags\Var_::__construct |
| 61 | + * @covers \phpDocumentor\Reflection\DocBlock\Tags\BaseTag::render |
| 62 | + */ |
| 63 | + public function testIfTagCanBeRenderedUsingSpecificFormatter() |
| 64 | + { |
| 65 | + $fixture = new Var_('myVariable'); |
| 66 | + |
| 67 | + $formatter = m::mock(Formatter::class); |
| 68 | + $formatter->shouldReceive('format')->with($fixture)->andReturn('Rendered output'); |
| 69 | + |
| 70 | + $this->assertSame('Rendered output', $fixture->render($formatter)); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * @covers ::__construct |
| 75 | + * @covers ::getVariableName |
| 76 | + */ |
| 77 | + public function testHasVariableName() |
| 78 | + { |
| 79 | + $expected = 'myVariable'; |
| 80 | + |
| 81 | + $fixture = new Var_($expected); |
| 82 | + |
| 83 | + $this->assertSame($expected, $fixture->getVariableName()); |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * @covers ::__construct |
| 88 | + * @covers ::getType |
| 89 | + */ |
| 90 | + public function testHasType() |
| 91 | + { |
| 92 | + $expected = new String_(); |
| 93 | + |
| 94 | + $fixture = new Var_('myVariable', $expected); |
| 95 | + |
| 96 | + $this->assertSame($expected, $fixture->getType()); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * @covers ::__construct |
| 101 | + * @covers \phpDocumentor\Reflection\DocBlock\Tags\BaseTag::getDescription |
| 102 | + * @uses \phpDocumentor\Reflection\DocBlock\Description |
| 103 | + */ |
| 104 | + public function testHasDescription() |
| 105 | + { |
| 106 | + $expected = new Description('Description'); |
| 107 | + |
| 108 | + $fixture = new Var_('1.0', null, $expected); |
| 109 | + |
| 110 | + $this->assertSame($expected, $fixture->getDescription()); |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * @covers ::__construct |
| 115 | + * @covers ::__toString |
| 116 | + * @uses \phpDocumentor\Reflection\DocBlock\Description |
| 117 | + * @uses \phpDocumentor\Reflection\Types\String_ |
| 118 | + */ |
| 119 | + public function testStringRepresentationIsReturned() |
| 120 | + { |
| 121 | + $fixture = new Var_('myVariable', new String_(), new Description('Description')); |
| 122 | + |
| 123 | + $this->assertSame('string $myVariable Description', (string)$fixture); |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * @covers ::create |
| 128 | + * @uses \phpDocumentor\Reflection\DocBlock\Tags\Var_::<public> |
| 129 | + * @uses \phpDocumentor\Reflection\DocBlock\DescriptionFactory |
| 130 | + * @uses \phpDocumentor\Reflection\DocBlock\Description |
| 131 | + * @uses \phpDocumentor\Reflection\Types\Context |
| 132 | + */ |
| 133 | + public function testFactoryMethod() |
| 134 | + { |
| 135 | + $typeResolver = new TypeResolver(); |
| 136 | + $descriptionFactory = m::mock(DescriptionFactory::class); |
| 137 | + $context = new Context(''); |
| 138 | + |
| 139 | + $description = new Description('My Description'); |
| 140 | + $descriptionFactory->shouldReceive('create')->with('My Description', $context)->andReturn($description); |
| 141 | + |
| 142 | + $fixture = Var_::create('string $myVariable My Description', $typeResolver, $descriptionFactory, $context); |
| 143 | + |
| 144 | + $this->assertSame('string $myVariable My Description', (string)$fixture); |
| 145 | + $this->assertSame('myVariable', $fixture->getVariableName()); |
| 146 | + $this->assertInstanceOf(String_::class, $fixture->getType()); |
| 147 | + $this->assertSame($description, $fixture->getDescription()); |
| 148 | + } |
| 149 | + |
| 150 | + /** |
| 151 | + * @covers ::create |
| 152 | + * @uses \phpDocumentor\Reflection\DocBlock\Tags\Var_::<public> |
| 153 | + * @uses \phpDocumentor\Reflection\TypeResolver |
| 154 | + * @uses \phpDocumentor\Reflection\DocBlock\DescriptionFactory |
| 155 | + * @expectedException \InvalidArgumentException |
| 156 | + */ |
| 157 | + public function testFactoryMethodFailsIfEmptyBodyIsGiven() |
| 158 | + { |
| 159 | + $descriptionFactory = m::mock(DescriptionFactory::class); |
| 160 | + Var_::create('', new TypeResolver(), $descriptionFactory); |
| 161 | + } |
| 162 | + |
| 163 | + /** |
| 164 | + * @covers ::create |
| 165 | + * @expectedException \InvalidArgumentException |
| 166 | + */ |
| 167 | + public function testFactoryMethodFailsIfBodyIsNotString() |
| 168 | + { |
| 169 | + Var_::create([]); |
| 170 | + } |
| 171 | + |
| 172 | + /** |
| 173 | + * @covers ::create |
| 174 | + * @expectedException \InvalidArgumentException |
| 175 | + */ |
| 176 | + public function testFactoryMethodFailsIfResolverIsNull() |
| 177 | + { |
| 178 | + Var_::create('body'); |
| 179 | + } |
| 180 | + |
| 181 | + /** |
| 182 | + * @covers ::create |
| 183 | + * @uses \phpDocumentor\Reflection\TypeResolver |
| 184 | + * @expectedException \InvalidArgumentException |
| 185 | + */ |
| 186 | + public function testFactoryMethodFailsIfDescriptionFactoryIsNull() |
| 187 | + { |
| 188 | + Var_::create('body', new TypeResolver()); |
| 189 | + } |
| 190 | + |
| 191 | + /** |
| 192 | + * @covers ::__construct |
| 193 | + * @expectedException \InvalidArgumentException |
| 194 | + */ |
| 195 | + public function testExceptionIsThrownIfVariableNameIsNotString() |
| 196 | + { |
| 197 | + new Var_([]); |
| 198 | + } |
| 199 | +} |
0 commit comments