Skip to content

Commit d29061e

Browse files
committed
Rewritten Since tag and added test
1 parent 01ddd0a commit d29061e

File tree

3 files changed

+246
-7
lines changed

3 files changed

+246
-7
lines changed

src/DocBlock/Tags/Since.php

Lines changed: 80 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,94 @@
11
<?php
22
/**
3-
* phpDocumentor
3+
* This file is part of phpDocumentor.
44
*
5-
* PHP Version 5.3
5+
* For the full copyright and license information, please view the LICENSE
6+
* file that was distributed with this source code.
67
*
7-
* @author Vasil Rangelov <[email protected]>
8-
* @copyright 2010-2011 Mike van Riel / Naenius (http://www.naenius.com)
8+
* @copyright 2010-2015 Mike van Riel<[email protected]>
99
* @license http://www.opensource.org/licenses/mit-license.php MIT
1010
* @link http://phpdoc.org
1111
*/
1212

1313
namespace phpDocumentor\Reflection\DocBlock\Tags;
1414

15+
use phpDocumentor\Reflection\Types\Context;
16+
use phpDocumentor\Reflection\DocBlock\Description;
17+
use phpDocumentor\Reflection\DocBlock\DescriptionFactory;
18+
use Webmozart\Assert\Assert;
19+
1520
/**
16-
* Reflection class for a @since tag in a Docblock.
21+
* Reflection class for a {@}since tag in a Docblock.
1722
*/
18-
class Since extends Version
23+
final class Since extends BaseTag
1924
{
25+
protected $name = 'since';
26+
27+
/**
28+
* PCRE regular expression matching a version vector.
29+
* Assumes the "x" modifier.
30+
*/
31+
const REGEX_VECTOR = '(?:
32+
# Normal release vectors.
33+
\d\S*
34+
|
35+
# VCS version vectors. Per PHPCS, they are expected to
36+
# follow the form of the VCS name, followed by ":", followed
37+
# by the version vector itself.
38+
# By convention, popular VCSes like CVS, SVN and GIT use "$"
39+
# around the actual version vector.
40+
[^\s\:]+\:\s*\$[^\$]+\$
41+
)';
42+
43+
/** @var string The version vector. */
44+
private $version = '';
45+
46+
public function __construct($version = null, Description $description = null)
47+
{
48+
Assert::nullOrStringNotEmpty($version);
49+
50+
$this->version = $version;
51+
$this->description = $description;
52+
}
53+
54+
/**
55+
* @return static
56+
*/
57+
public static function create($body, DescriptionFactory $descriptionFactory = null, Context $context = null)
58+
{
59+
Assert::nullOrString($body);
60+
if (empty($body)) {
61+
return new static();
62+
}
63+
64+
$matches = [];
65+
if (! preg_match('/^(' . self::REGEX_VECTOR . ')\s*(.+)?$/sux', $body, $matches)) {
66+
return null;
67+
}
68+
69+
return new static(
70+
$matches[1],
71+
$descriptionFactory->create(isset($matches[2]) ? $matches[2] : '', $context)
72+
);
73+
}
74+
75+
/**
76+
* Gets the version section of the tag.
77+
*
78+
* @return string
79+
*/
80+
public function getVersion()
81+
{
82+
return $this->version;
83+
}
84+
85+
/**
86+
* Returns a string representation for this tag.
87+
*
88+
* @return string
89+
*/
90+
public function __toString()
91+
{
92+
return $this->version . ($this->description ? ' ' . $this->description->render() : '');
93+
}
2094
}

src/DocBlock/Tags/Version.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
/**
2121
* Reflection class for a {@}version tag in a Docblock.
2222
*/
23-
class Version extends BaseTag
23+
final class Version extends BaseTag
2424
{
2525
protected $name = 'version';
2626

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\Since
22+
* @covers ::<private>
23+
*/
24+
class SinceTest extends \PHPUnit_Framework_TestCase
25+
{
26+
/**
27+
* @uses \phpDocumentor\Reflection\DocBlock\Tags\Since::__construct
28+
* @uses \phpDocumentor\Reflection\DocBlock\Description
29+
* @covers \phpDocumentor\Reflection\DocBlock\Tags\BaseTag::getName
30+
*/
31+
public function testIfCorrectTagNameIsReturned()
32+
{
33+
$fixture = new Since('1.0', new Description('Description'));
34+
35+
$this->assertSame('since', $fixture->getName());
36+
}
37+
38+
/**
39+
* @uses \phpDocumentor\Reflection\DocBlock\Tags\Since::__construct
40+
* @uses \phpDocumentor\Reflection\DocBlock\Tags\Since::__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 Since('1.0', new Description('Description'));
48+
49+
$this->assertSame('1.0 Description', $fixture->render());
50+
}
51+
52+
/**
53+
* @uses \phpDocumentor\Reflection\DocBlock\Tags\Since::__construct
54+
* @uses \phpDocumentor\Reflection\DocBlock\Description
55+
* @covers \phpDocumentor\Reflection\DocBlock\Tags\BaseTag::render
56+
*/
57+
public function testIfTagCanBeRenderedUsingSpecificFormatter()
58+
{
59+
$fixture = new Since('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 Since($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 Since('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 Since('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\Since::<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 = Since::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\Since::<public>
133+
* @uses \phpDocumentor\Reflection\DocBlock\DescriptionFactory
134+
* @uses \phpDocumentor\Reflection\DocBlock\Description
135+
* @uses \phpDocumentor\Reflection\Types\Context
136+
*/
137+
public function testFactoryMethodCreatesEmptySinceTag()
138+
{
139+
$descriptionFactory = m::mock(DescriptionFactory::class);
140+
$descriptionFactory->shouldReceive('create')->never();
141+
142+
$fixture = Since::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 testFactoryMethodFailsIfSinceIsNotString()
154+
{
155+
$this->assertNull(Since::create([]));
156+
}
157+
158+
/**
159+
* @covers ::create
160+
*/
161+
public function testFactoryMethodReturnsNullIfBodyDoesNotMatchRegex()
162+
{
163+
$this->assertNull(Since::create('dkhf<'));
164+
}
165+
}

0 commit comments

Comments
 (0)