Skip to content

Commit 2bab7d9

Browse files
committed
Refactored Uses tag and written test
1 parent 6b92695 commit 2bab7d9

File tree

3 files changed

+411
-6
lines changed

3 files changed

+411
-6
lines changed

src/DocBlock/Tags/Uses.php

Lines changed: 69 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,83 @@
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 Mike van Riel <[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\DocBlock\Description;
16+
use phpDocumentor\Reflection\DocBlock\DescriptionFactory;
17+
use phpDocumentor\Reflection\Fqsen;
18+
use phpDocumentor\Reflection\FqsenResolver;
19+
use phpDocumentor\Reflection\Types\Context;
20+
use Webmozart\Assert\Assert;
21+
1522
/**
16-
* Reflection class for a @uses tag in a Docblock.
23+
* Reflection class for a {@}uses tag in a Docblock.
1724
*/
18-
class Uses extends See
25+
final class Uses extends BaseTag
1926
{
27+
protected $name = 'uses';
28+
29+
/** @var Fqsen */
30+
protected $refers = null;
31+
32+
/**
33+
* Initializes this tag.
34+
*
35+
* @param Fqsen $refers
36+
* @param Description $description
37+
*/
38+
public function __construct(Fqsen $refers, Description $description = null)
39+
{
40+
$this->refers = $refers;
41+
$this->description = $description;
42+
}
43+
44+
/**
45+
* {@inheritdoc}
46+
*/
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+
56+
$parts = preg_split('/\s+/Su', $body, 2);
57+
58+
return new static(
59+
$resolver->resolve($parts[0], $context),
60+
$descriptionFactory->create(isset($parts[1]) ? $parts[1] : '', $context)
61+
);
62+
}
63+
64+
/**
65+
* Returns the structural element this tag refers to.
66+
*
67+
* @return Fqsen
68+
*/
69+
public function getReference()
70+
{
71+
return $this->refers;
72+
}
73+
74+
/**
75+
* Returns a string representation of this tag.
76+
*
77+
* @return string
78+
*/
79+
public function __toString()
80+
{
81+
return $this->refers . ' ' . $this->description->render();
82+
}
2083
}
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
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\Throws
24+
* @covers ::<private>
25+
*/
26+
class ThrowsTest extends \PHPUnit_Framework_TestCase
27+
{
28+
/**
29+
* @uses \phpDocumentor\Reflection\DocBlock\Tags\Throws::__construct
30+
* @uses \phpDocumentor\Reflection\DocBlock\Description
31+
* @covers \phpDocumentor\Reflection\DocBlock\Tags\BaseTag::getName
32+
*/
33+
public function testIfCorrectTagNameIsReturned()
34+
{
35+
$fixture = new Throws(new String_(), new Description('Description'));
36+
37+
$this->assertSame('throws', $fixture->getName());
38+
}
39+
40+
/**
41+
* @uses \phpDocumentor\Reflection\DocBlock\Tags\Throws::__construct
42+
* @uses \phpDocumentor\Reflection\DocBlock\Tags\Throws::__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 Throws(new String_(), new Description('Description'));
50+
51+
$this->assertSame('string Description', $fixture->render());
52+
}
53+
54+
/**
55+
* @uses \phpDocumentor\Reflection\DocBlock\Tags\Throws::__construct
56+
* @uses \phpDocumentor\Reflection\DocBlock\Description
57+
* @covers \phpDocumentor\Reflection\DocBlock\Tags\BaseTag::render
58+
*/
59+
public function testIfTagCanBeRenderedUsingSpecificFormatter()
60+
{
61+
$fixture = new Throws(new String_(), 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 ::getType
72+
*/
73+
public function testHasType()
74+
{
75+
$expected = new String_();
76+
77+
$fixture = new Throws($expected);
78+
79+
$this->assertSame($expected, $fixture->getType());
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 Throws(new String_(), $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 Throws(new String_(), new Description('Description'));
104+
105+
$this->assertSame('string Description', (string)$fixture);
106+
}
107+
108+
/**
109+
* @covers ::create
110+
* @uses \phpDocumentor\Reflection\DocBlock\Tags\Throws::<public>
111+
* @uses \phpDocumentor\Reflection\DocBlock\DescriptionFactory
112+
* @uses \phpDocumentor\Reflection\TypeResolver
113+
* @uses \phpDocumentor\Reflection\DocBlock\Description
114+
* @uses \phpDocumentor\Reflection\Types\String_
115+
* @uses \phpDocumentor\Reflection\Types\Context
116+
*/
117+
public function testFactoryMethod()
118+
{
119+
$descriptionFactory = m::mock(DescriptionFactory::class);
120+
$resolver = new TypeResolver();
121+
$context = new Context('');
122+
123+
$type = new String_();
124+
$description = new Description('My Description');
125+
$descriptionFactory->shouldReceive('create')->with('My Description', $context)->andReturn($description);
126+
127+
$fixture = Throws::create('string My Description', $resolver, $descriptionFactory, $context);
128+
129+
$this->assertSame('string My Description', (string)$fixture);
130+
$this->assertEquals($type, $fixture->getType());
131+
$this->assertSame($description, $fixture->getDescription());
132+
}
133+
134+
/**
135+
* @covers ::create
136+
* @expectedException \InvalidArgumentException
137+
*/
138+
public function testFactoryMethodFailsIfBodyIsNotString()
139+
{
140+
$this->assertNull(Throws::create([]));
141+
}
142+
143+
/**
144+
* @covers ::create
145+
* @expectedException \InvalidArgumentException
146+
*/
147+
public function testFactoryMethodFailsIfBodyIsNotEmpty()
148+
{
149+
$this->assertNull(Throws::create(''));
150+
}
151+
152+
/**
153+
* @covers ::create
154+
* @expectedException \InvalidArgumentException
155+
*/
156+
public function testFactoryMethodFailsIfResolverIsNull()
157+
{
158+
Throws::create('body');
159+
}
160+
161+
/**
162+
* @covers ::create
163+
* @expectedException \InvalidArgumentException
164+
*/
165+
public function testFactoryMethodFailsIfDescriptionFactoryIsNull()
166+
{
167+
Throws::create('body', new TypeResolver());
168+
}
169+
}

0 commit comments

Comments
 (0)