Skip to content

Commit 4fc6e44

Browse files
committed
Update tests for elements with Expressions
1 parent 27cc774 commit 4fc6e44

File tree

6 files changed

+177
-36
lines changed

6 files changed

+177
-36
lines changed

src/phpDocumentor/Reflection/Php/Expression.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@
22

33
declare(strict_types=1);
44

5+
/**
6+
* This file is part of phpDocumentor.
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*
11+
* @link http://phpdoc.org
12+
*/
13+
514
namespace phpDocumentor\Reflection\Php;
615

716
use phpDocumentor\Reflection\Fqsen;
@@ -76,7 +85,7 @@ public static function generatePlaceholder(string $name): string
7685
/**
7786
* @param array<string, Fqsen|Type> $parts
7887
*/
79-
public function __construct(string $expression, array $parts)
88+
public function __construct(string $expression, array $parts = [])
8089
{
8190
Assert::notEmpty($expression);
8291
Assert::allIsInstanceOfAny($parts, [Fqsen::class, Type::class]);

src/phpDocumentor/Reflection/Php/Expression/ExpressionPrinter.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@
22

33
declare(strict_types=1);
44

5+
/**
6+
* This file is part of phpDocumentor.
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*
11+
* @link http://phpdoc.org
12+
*/
13+
514
namespace phpDocumentor\Reflection\Php\Expression;
615

716
use phpDocumentor\Reflection\Fqsen;

tests/unit/phpDocumentor/Reflection/Php/ArgumentTest.php

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,61 +32,62 @@ final class ArgumentTest extends TestCase
3232
*/
3333
public function testGetTypes(): void
3434
{
35-
$argument = new Argument('myArgument', null, 'myDefaultValue', true, true);
36-
$this->assertInstanceOf(Mixed_::class, $argument->getType());
35+
$argument = new Argument('myArgument', null, new Expression('myDefaultValue'), true, true);
36+
self::assertInstanceOf(Mixed_::class, $argument->getType());
3737

3838
$argument = new Argument(
3939
'myArgument',
4040
new String_(),
41-
'myDefaultValue',
41+
new Expression('myDefaultValue'),
4242
true,
4343
true
4444
);
45-
$this->assertEquals(new String_(), $argument->getType());
45+
self::assertEquals(new String_(), $argument->getType());
4646
}
4747

4848
/**
4949
* @covers ::getName
5050
*/
5151
public function testGetName(): void
5252
{
53-
$argument = new Argument('myArgument', null, 'myDefault', true, true);
54-
$this->assertEquals('myArgument', $argument->getName());
53+
$argument = new Argument('myArgument', null, new Expression('myDefault'), true, true);
54+
55+
self::assertEquals('myArgument', $argument->getName());
5556
}
5657

5758
/**
5859
* @covers ::getDefault
5960
*/
6061
public function testGetDefault(): void
6162
{
62-
$argument = new Argument('myArgument', null, 'myDefaultValue', true, true);
63-
$this->assertEquals('myDefaultValue', $argument->getDefault());
63+
$argument = new Argument('myArgument', null, new Expression('myDefaultValue'), true, true);
64+
self::assertEquals(new Expression('myDefaultValue'), $argument->getDefault());
6465

6566
$argument = new Argument('myArgument', null, null, true, true);
66-
$this->assertNull($argument->getDefault());
67+
self::assertNull($argument->getDefault());
6768
}
6869

6970
/**
7071
* @covers ::isByReference
7172
*/
7273
public function testGetWhetherArgumentIsPassedByReference(): void
7374
{
74-
$argument = new Argument('myArgument', null, 'myDefaultValue', true, true);
75-
$this->assertTrue($argument->isByReference());
75+
$argument = new Argument('myArgument', null, new Expression('myDefaultValue'), true, true);
76+
self::assertTrue($argument->isByReference());
7677

7778
$argument = new Argument('myArgument', null, null, false, true);
78-
$this->assertFalse($argument->isByReference());
79+
self::assertFalse($argument->isByReference());
7980
}
8081

8182
/**
8283
* @covers ::isVariadic
8384
*/
8485
public function testGetWhetherArgumentisVariadic(): void
8586
{
86-
$argument = new Argument('myArgument', null, 'myDefaultValue', true, true);
87-
$this->assertTrue($argument->isVariadic());
87+
$argument = new Argument('myArgument', null, new Expression('myDefaultValue'), true, true);
88+
self::assertTrue($argument->isVariadic());
8889

89-
$argument = new Argument('myArgument', null, 'myDefaultValue', true, false);
90-
$this->assertFalse($argument->isVariadic());
90+
$argument = new Argument('myArgument', null, new Expression('myDefaultValue'), true, false);
91+
self::assertFalse($argument->isVariadic());
9192
}
9293
}

tests/unit/phpDocumentor/Reflection/Php/ConstantTest.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919
use phpDocumentor\Reflection\Metadata\MetaDataContainer as MetaDataContainerInterface;
2020

2121
/**
22-
* @uses \phpDocumentor\Reflection\DocBlock
23-
* @uses \phpDocumentor\Reflection\Php\Visibility
24-
* @uses \phpDocumentor\Reflection\Fqsen
22+
* @uses DocBlock
23+
* @uses Visibility
24+
* @uses Fqsen
2525
*
2626
* @coversDefaultClass \phpDocumentor\Reflection\Php\Constant
2727
* @covers ::__construct
@@ -46,7 +46,7 @@ protected function setUp(): void
4646
{
4747
$this->fqsen = new Fqsen('\MySpace\CONSTANT');
4848
$this->docBlock = new DocBlock('');
49-
$this->fixture = new Constant($this->fqsen, $this->docBlock, $this->value);
49+
$this->fixture = new Constant($this->fqsen, $this->docBlock, new Expression($this->value));
5050
}
5151

5252
private function getFixture(): MetaDataContainerInterface
@@ -60,7 +60,7 @@ private function getFixture(): MetaDataContainerInterface
6060
*/
6161
public function testGetValue(): void
6262
{
63-
$this->assertSame($this->value, $this->fixture->getValue());
63+
self::assertEquals(new Expression($this->value), $this->fixture->getValue());
6464
}
6565

6666
/**
@@ -69,7 +69,7 @@ public function testGetValue(): void
6969
*/
7070
public function testIsFinal(): void
7171
{
72-
$this->assertFalse($this->fixture->isFinal());
72+
self::assertFalse($this->fixture->isFinal());
7373
}
7474

7575
/**
@@ -78,24 +78,24 @@ public function testIsFinal(): void
7878
*/
7979
public function testGetFqsen(): void
8080
{
81-
$this->assertSame($this->fqsen, $this->fixture->getFqsen());
82-
$this->assertSame($this->fqsen->getName(), $this->fixture->getName());
81+
self::assertSame($this->fqsen, $this->fixture->getFqsen());
82+
self::assertSame($this->fqsen->getName(), $this->fixture->getName());
8383
}
8484

8585
/**
8686
* @covers ::getDocBlock
8787
*/
8888
public function testGetDocblock(): void
8989
{
90-
$this->assertSame($this->docBlock, $this->fixture->getDocBlock());
90+
self::assertSame($this->docBlock, $this->fixture->getDocBlock());
9191
}
9292

9393
/**
9494
* @covers ::getVisibility
9595
*/
9696
public function testGetVisibility(): void
9797
{
98-
$this->assertEquals(new Visibility(Visibility::PUBLIC_), $this->fixture->getVisibility());
98+
self::assertEquals(new Visibility(Visibility::PUBLIC_), $this->fixture->getVisibility());
9999
}
100100

101101
public function testLineAndColumnNumberIsReturnedWhenALocationIsProvided(): void

tests/unit/phpDocumentor/Reflection/Php/EnumCaseTest.php

Lines changed: 122 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,18 @@ final class EnumCaseTest extends TestCase
3636
private DocBlock $docBlock;
3737

3838
/**
39-
* Creates a new (emoty) fixture object.
39+
* Creates a new (empty) fixture object.
4040
*/
4141
protected function setUp(): void
4242
{
4343
$this->fqsen = new Fqsen('\Enum::VALUE');
4444
$this->docBlock = new DocBlock('');
4545

46-
$this->fixture = new EnumCase($this->fqsen, $this->docBlock);
46+
// needed for MetaDataContainer testing
47+
$this->fixture = new EnumCase(
48+
$this->fqsen,
49+
$this->docBlock
50+
);
4751
}
4852

4953
private function getFixture(): MetaDataContainerInterface
@@ -56,38 +60,147 @@ private function getFixture(): MetaDataContainerInterface
5660
*/
5761
public function testGettingName(): void
5862
{
59-
$this->assertSame($this->fqsen->getName(), $this->fixture->getName());
63+
$fixture = new EnumCase(
64+
$this->fqsen,
65+
$this->docBlock
66+
);
67+
68+
$this->assertSame($this->fqsen->getName(), $fixture->getName());
6069
}
6170

6271
/**
6372
* @covers ::getFqsen
6473
*/
6574
public function testGettingFqsen(): void
6675
{
67-
$this->assertSame($this->fqsen, $this->fixture->getFqsen());
76+
$fixture = new EnumCase(
77+
$this->fqsen,
78+
$this->docBlock
79+
);
80+
81+
$this->assertSame($this->fqsen, $fixture->getFqsen());
6882
}
6983

7084
/**
7185
* @covers ::getDocBlock
7286
*/
7387
public function testGettingDocBlock(): void
7488
{
75-
$this->assertSame($this->docBlock, $this->fixture->getDocBlock());
89+
$fixture = new EnumCase(
90+
$this->fqsen,
91+
$this->docBlock
92+
);
93+
94+
$this->assertSame($this->docBlock, $fixture->getDocBlock());
7695
}
7796

7897
/**
7998
* @covers ::getValue
8099
*/
81-
public function testGetValue(): void
100+
public function testValueCanBeOmitted(): void
101+
{
102+
$fixture = new EnumCase(
103+
$this->fqsen,
104+
$this->docBlock
105+
);
106+
107+
$this->assertNull($fixture->getValue());
108+
}
109+
110+
/**
111+
* @uses Expression
112+
*
113+
* @covers ::getValue
114+
*/
115+
public function testValueCanBeProvidedAsAnExpression(): void
116+
{
117+
$expression = new Expression('Enum case expression');
118+
$fixture = new EnumCase(
119+
$this->fqsen,
120+
$this->docBlock,
121+
null,
122+
null,
123+
$expression
124+
);
125+
126+
$this->assertSame($expression, $fixture->getValue(false));
127+
}
128+
129+
/**
130+
* @uses Expression
131+
*
132+
* @covers ::getValue
133+
*/
134+
public function testValueCanBeReturnedAsString(): void
135+
{
136+
$expression = new Expression('Enum case expression');
137+
$fixture = new EnumCase(
138+
$this->fqsen,
139+
$this->docBlock,
140+
null,
141+
null,
142+
$expression
143+
);
144+
145+
$this->assertSame('Enum case expression', $fixture->getValue(true));
146+
}
147+
148+
/**
149+
* @covers ::getLocation
150+
*/
151+
public function testGetLocationReturnsProvidedValue(): void
82152
{
83-
$this->assertNull($this->fixture->getValue());
153+
$location = new Location(15, 10);
154+
$fixture = new EnumCase(
155+
$this->fqsen,
156+
$this->docBlock,
157+
$location
158+
);
159+
160+
self::assertSame($location, $fixture->getLocation());
84161
}
85162

86163
/**
164+
* @uses Location
165+
*
87166
* @covers ::getLocation
88167
*/
89-
public function testGetLocationReturnsDefault(): void
168+
public function testGetLocationReturnsUnknownByDefault(): void
90169
{
91-
self::assertEquals(new Location(-1), $this->fixture->getLocation());
170+
$fixture = new EnumCase(
171+
$this->fqsen,
172+
$this->docBlock
173+
);
174+
175+
self::assertEquals(new Location(-1), $fixture->getLocation());
176+
}
177+
178+
/**
179+
* @covers ::getEndLocation
180+
*/
181+
public function testGetEndLocationReturnsProvidedValue(): void
182+
{
183+
$location = new Location(11, 23);
184+
$fixture = new EnumCase(
185+
$this->fqsen,
186+
$this->docBlock,
187+
null,
188+
$location
189+
);
190+
191+
self::assertSame($location, $fixture->getEndLocation());
192+
}
193+
194+
/**
195+
* @covers ::getEndLocation
196+
*/
197+
public function testGetEndLocationReturnsUnknownByDefault(): void
198+
{
199+
$fixture = new EnumCase(
200+
$this->fqsen,
201+
$this->docBlock
202+
);
203+
204+
self::assertEquals(new Location(-1), $fixture->getEndLocation());
92205
}
93206
}

tests/unit/phpDocumentor/Reflection/Php/ExpressionTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@
22

33
declare(strict_types=1);
44

5+
/**
6+
* This file is part of phpDocumentor.
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*
11+
* @link http://phpdoc.org
12+
*/
13+
514
namespace phpDocumentor\Reflection\Php;
615

716
use InvalidArgumentException;

0 commit comments

Comments
 (0)