Skip to content

Commit 447acda

Browse files
committed
Written unit tests for See tag and made it more solid
1 parent 255bc6f commit 447acda

File tree

3 files changed

+208
-9
lines changed

3 files changed

+208
-9
lines changed

src/DocBlock/Tags/See.php

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

1313
namespace phpDocumentor\Reflection\DocBlock\Tags;
1414

15-
use DocBlock\Types\Resolver;
15+
use phpDocumentor\Reflection\DocBlock\DescriptionFactory;
1616
use phpDocumentor\Reflection\Fqsen;
17-
use phpDocumentor\Reflection\DocBlock\Context;
17+
use phpDocumentor\Reflection\FqsenResolver;
18+
use phpDocumentor\Reflection\Types\Context;
1819
use phpDocumentor\Reflection\DocBlock\Description;
19-
use phpDocumentor\Reflection\DocBlock\Tag;
20+
use Webmozart\Assert\Assert;
2021

2122
/**
2223
* Reflection class for an {@}see tag in a Docblock.
2324
*/
24-
class See extends Tag
25+
class See extends BaseTag
2526
{
27+
protected $name = 'see';
28+
2629
/** @var Fqsen */
2730
protected $refers = null;
2831

@@ -32,7 +35,7 @@ class See extends Tag
3235
* @param Fqsen $refers
3336
* @param Description $description
3437
*/
35-
public function __construct(Fqsen $refers, Description $description)
38+
public function __construct(Fqsen $refers, Description $description = null)
3639
{
3740
$this->refers = $refers;
3841
$this->description = $description;
@@ -41,14 +44,20 @@ public function __construct(Fqsen $refers, Description $description)
4144
/**
4245
* {@inheritdoc}
4346
*/
44-
public static function create($body, Context $context)
45-
{
47+
public static function create(
48+
$body,
49+
FqsenResolver $resolver = null,
50+
DescriptionFactory $descriptionFactory = null,
51+
Context $context = null
52+
) {
53+
Assert::string($body);
54+
Assert::allNotNull([$resolver, $descriptionFactory]);
55+
4656
$parts = preg_split('/\s+/Su', $body, 2);
47-
$resolver = new Resolver();
4857

4958
return new static(
5059
$resolver->resolve($parts[0], $context),
51-
new Description(isset($parts[1]) ? $parts[1] : '', $context)
60+
$descriptionFactory->create(isset($parts[1]) ? $parts[1] : '', $context)
5261
);
5362
}
5463

tests/unit/DocBlock/Tags/ReturnTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,4 +148,22 @@ public function testFactoryMethodFailsIfBodyIsNotEmpty()
148148
{
149149
$this->assertNull(Return_::create(''));
150150
}
151+
152+
/**
153+
* @covers ::create
154+
* @expectedException \InvalidArgumentException
155+
*/
156+
public function testFactoryMethodFailsIfResolverIsNull()
157+
{
158+
Return_::create('body');
159+
}
160+
161+
/**
162+
* @covers ::create
163+
* @expectedException \InvalidArgumentException
164+
*/
165+
public function testFactoryMethodFailsIfDescriptionFactoryIsNull()
166+
{
167+
Return_::create('body', new TypeResolver());
168+
}
151169
}

tests/unit/DocBlock/Tags/SeeTest.php

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

0 commit comments

Comments
 (0)