Skip to content

Commit 01ddd0a

Browse files
committed
Rewritten Version tag and added test
1 parent c27aeed commit 01ddd0a

File tree

2 files changed

+185
-8
lines changed

2 files changed

+185
-8
lines changed

src/DocBlock/Tags/Version.php

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,18 @@
1212

1313
namespace phpDocumentor\Reflection\DocBlock\Tags;
1414

15-
use phpDocumentor\Reflection\DocBlock\Tag;
15+
use phpDocumentor\Reflection\Types\Context;
16+
use phpDocumentor\Reflection\DocBlock\Description;
17+
use phpDocumentor\Reflection\DocBlock\DescriptionFactory;
18+
use Webmozart\Assert\Assert;
1619

1720
/**
18-
* Reflection class for a @version tag in a Docblock.
21+
* Reflection class for a {@}version tag in a Docblock.
1922
*/
20-
class Version extends Tag
23+
class Version extends BaseTag
2124
{
25+
protected $name = 'version';
26+
2227
/**
2328
* PCRE regular expression matching a version vector.
2429
* Assumes the "x" modifier.
@@ -38,25 +43,32 @@ class Version extends Tag
3843
/** @var string The version vector. */
3944
private $version = '';
4045

41-
public function __construct($version, Description $description = null)
46+
public function __construct($version = null, Description $description = null)
4247
{
48+
Assert::nullOrStringNotEmpty($version);
49+
4350
$this->version = $version;
4451
$this->description = $description;
4552
}
4653

4754
/**
48-
* {@inheritdoc}
55+
* @return static
4956
*/
50-
public static function create($body, Context $context = null)
57+
public static function create($body, DescriptionFactory $descriptionFactory = null, Context $context = null)
5158
{
59+
Assert::nullOrString($body);
60+
if (empty($body)) {
61+
return new static();
62+
}
63+
5264
$matches = [];
5365
if (!preg_match('/^(' . self::REGEX_VECTOR . ')\s*(.+)?$/sux', $body, $matches)) {
5466
return null;
5567
}
5668

5769
return new static(
5870
$matches[1],
59-
new Description(isset($matches[2]) ? $matches[2] : '', $context)
71+
$descriptionFactory->create(isset($matches[2]) ? $matches[2] : '', $context)
6072
);
6173
}
6274

@@ -77,6 +89,6 @@ public function getVersion()
7789
*/
7890
public function __toString()
7991
{
80-
return $this->version . ' ' . $this->description->render();
92+
return $this->version . ($this->description ? ' ' . $this->description->render() : '');
8193
}
8294
}
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
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\Types\Context;
19+
20+
/**
21+
* @coversDefaultClass \phpDocumentor\Reflection\DocBlock\Tags\Version
22+
* @covers ::<private>
23+
*/
24+
class VersionTest extends \PHPUnit_Framework_TestCase
25+
{
26+
/**
27+
* @uses \phpDocumentor\Reflection\DocBlock\Tags\Version::__construct
28+
* @uses \phpDocumentor\Reflection\DocBlock\Description
29+
* @covers \phpDocumentor\Reflection\DocBlock\Tags\BaseTag::getName
30+
*/
31+
public function testIfCorrectTagNameIsReturned()
32+
{
33+
$fixture = new Version('1.0', new Description('Description'));
34+
35+
$this->assertSame('version', $fixture->getName());
36+
}
37+
38+
/**
39+
* @uses \phpDocumentor\Reflection\DocBlock\Tags\Version::__construct
40+
* @uses \phpDocumentor\Reflection\DocBlock\Tags\Version::__toString
41+
* @uses \phpDocumentor\Reflection\DocBlock\Tags\Formatter\PassthroughFormatter
42+
* @uses \phpDocumentor\Reflection\DocBlock\Description
43+
* @covers \phpDocumentor\Reflection\DocBlock\Tags\BaseTag::render
44+
*/
45+
public function testIfTagCanBeRenderedUsingDefaultFormatter()
46+
{
47+
$fixture = new Version('1.0', new Description('Description'));
48+
49+
$this->assertSame('1.0 Description', $fixture->render());
50+
}
51+
52+
/**
53+
* @uses \phpDocumentor\Reflection\DocBlock\Tags\Version::__construct
54+
* @uses \phpDocumentor\Reflection\DocBlock\Description
55+
* @covers \phpDocumentor\Reflection\DocBlock\Tags\BaseTag::render
56+
*/
57+
public function testIfTagCanBeRenderedUsingSpecificFormatter()
58+
{
59+
$fixture = new Version('1.0', new Description('Description'));
60+
61+
$formatter = m::mock(Formatter::class);
62+
$formatter->shouldReceive('format')->with($fixture)->andReturn('Rendered output');
63+
64+
$this->assertSame('Rendered output', $fixture->render($formatter));
65+
}
66+
67+
/**
68+
* @covers ::__construct
69+
* @covers ::getVersion
70+
*/
71+
public function testHasVersionNumber()
72+
{
73+
$expected = '1.0';
74+
75+
$fixture = new Version($expected);
76+
77+
$this->assertSame($expected, $fixture->getVersion());
78+
}
79+
80+
/**
81+
* @covers ::__construct
82+
* @covers \phpDocumentor\Reflection\DocBlock\Tags\BaseTag::getDescription
83+
* @uses \phpDocumentor\Reflection\DocBlock\Description
84+
*/
85+
public function testHasDescription()
86+
{
87+
$expected = new Description('Description');
88+
89+
$fixture = new Version('1.0', $expected);
90+
91+
$this->assertSame($expected, $fixture->getDescription());
92+
}
93+
94+
/**
95+
* @covers ::__construct
96+
* @covers ::__toString
97+
* @uses \phpDocumentor\Reflection\DocBlock\Description
98+
*/
99+
public function testStringRepresentationIsReturned()
100+
{
101+
$fixture = new Version('1.0', new Description('Description'));
102+
103+
$this->assertSame('1.0 Description', (string)$fixture);
104+
}
105+
106+
/**
107+
* @covers ::create
108+
* @uses \phpDocumentor\Reflection\DocBlock\Tags\Version::<public>
109+
* @uses \phpDocumentor\Reflection\DocBlock\DescriptionFactory
110+
* @uses \phpDocumentor\Reflection\DocBlock\Description
111+
* @uses \phpDocumentor\Reflection\Types\Context
112+
*/
113+
public function testFactoryMethod()
114+
{
115+
$descriptionFactory = m::mock(DescriptionFactory::class);
116+
$context = new Context('');
117+
118+
$version = '1.0';
119+
$description = new Description('My Description');
120+
121+
$descriptionFactory->shouldReceive('create')->with('My Description', $context)->andReturn($description);
122+
123+
$fixture = Version::create('1.0 My Description', $descriptionFactory, $context);
124+
125+
$this->assertSame('1.0 My Description', (string)$fixture);
126+
$this->assertSame($version, $fixture->getVersion());
127+
$this->assertSame($description, $fixture->getDescription());
128+
}
129+
130+
/**
131+
* @covers ::create
132+
* @uses \phpDocumentor\Reflection\DocBlock\Tags\Version::<public>
133+
* @uses \phpDocumentor\Reflection\DocBlock\DescriptionFactory
134+
* @uses \phpDocumentor\Reflection\DocBlock\Description
135+
* @uses \phpDocumentor\Reflection\Types\Context
136+
*/
137+
public function testFactoryMethodCreatesEmptyVersionTag()
138+
{
139+
$descriptionFactory = m::mock(DescriptionFactory::class);
140+
$descriptionFactory->shouldReceive('create')->never();
141+
142+
$fixture = Version::create('', $descriptionFactory, new Context(''));
143+
144+
$this->assertSame('', (string)$fixture);
145+
$this->assertSame(null, $fixture->getVersion());
146+
$this->assertSame(null, $fixture->getDescription());
147+
}
148+
149+
/**
150+
* @covers ::create
151+
* @expectedException \InvalidArgumentException
152+
*/
153+
public function testFactoryMethodFailsIfVersionIsNotString()
154+
{
155+
$this->assertNull(Version::create([]));
156+
}
157+
158+
/**
159+
* @covers ::create
160+
*/
161+
public function testFactoryMethodReturnsNullIfBodyDoesNotMatchRegex()
162+
{
163+
$this->assertNull(Version::create('dkhf<'));
164+
}
165+
}

0 commit comments

Comments
 (0)