Skip to content

Commit 3e4aa91

Browse files
mvrieljaapio
authored andcommitted
Update tests for elements with Expressions
1 parent a7bf4e2 commit 3e4aa91

File tree

6 files changed

+180
-33
lines changed

6 files changed

+180
-33
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
@@ -26,49 +26,50 @@ final class ArgumentTest extends TestCase
2626
{
2727
public function testGetTypes(): void
2828
{
29-
$argument = new Argument('myArgument', null, 'myDefaultValue', true, true);
30-
$this->assertInstanceOf(Mixed_::class, $argument->getType());
29+
$argument = new Argument('myArgument', null, new Expression('myDefaultValue'), true, true);
30+
self::assertInstanceOf(Mixed_::class, $argument->getType());
3131

3232
$argument = new Argument(
3333
'myArgument',
3434
new String_(),
35-
'myDefaultValue',
35+
new Expression('myDefaultValue'),
3636
true,
3737
true,
3838
);
39-
$this->assertEquals(new String_(), $argument->getType());
39+
self::assertEquals(new String_(), $argument->getType());
4040
}
4141

4242
public function testGetName(): void
4343
{
44-
$argument = new Argument('myArgument', null, 'myDefault', true, true);
45-
$this->assertEquals('myArgument', $argument->getName());
44+
$argument = new Argument('myArgument', null, new Expression('myDefault'), true, true);
45+
46+
self::assertEquals('myArgument', $argument->getName());
4647
}
4748

4849
public function testGetDefault(): void
4950
{
50-
$argument = new Argument('myArgument', null, 'myDefaultValue', true, true);
51-
$this->assertEquals('myDefaultValue', $argument->getDefault());
51+
$argument = new Argument('myArgument', null, new Expression('myDefaultValue'), true, true);
52+
self::assertEquals(new Expression('myDefaultValue'), $argument->getDefault());
5253

5354
$argument = new Argument('myArgument', null, null, true, true);
54-
$this->assertNull($argument->getDefault());
55+
self::assertNull($argument->getDefault());
5556
}
5657

5758
public function testGetWhetherArgumentIsPassedByReference(): void
5859
{
59-
$argument = new Argument('myArgument', null, 'myDefaultValue', true, true);
60-
$this->assertTrue($argument->isByReference());
60+
$argument = new Argument('myArgument', null, new Expression('myDefaultValue'), true, true);
61+
self::assertTrue($argument->isByReference());
6162

6263
$argument = new Argument('myArgument', null, null, false, true);
63-
$this->assertFalse($argument->isByReference());
64+
self::assertFalse($argument->isByReference());
6465
}
6566

6667
public function testGetWhetherArgumentisVariadic(): void
6768
{
68-
$argument = new Argument('myArgument', null, 'myDefaultValue', true, true);
69-
$this->assertTrue($argument->isVariadic());
69+
$argument = new Argument('myArgument', null, new Expression('myDefaultValue'), true, true);
70+
self::assertTrue($argument->isVariadic());
7071

71-
$argument = new Argument('myArgument', null, 'myDefaultValue', true, false);
72-
$this->assertFalse($argument->isVariadic());
72+
$argument = new Argument('myArgument', null, new Expression('myDefaultValue'), true, false);
73+
self::assertFalse($argument->isVariadic());
7374
}
7475
}

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ protected function setUp(): void
4242
{
4343
$this->fqsen = new Fqsen('\MySpace\CONSTANT');
4444
$this->docBlock = new DocBlock('');
45-
$this->fixture = new Constant($this->fqsen, $this->docBlock, $this->value);
45+
$this->fixture = new Constant($this->fqsen, $this->docBlock, new Expression($this->value));
4646
}
4747

4848
private function getFixture(): MetaDataContainerInterface
@@ -52,28 +52,28 @@ private function getFixture(): MetaDataContainerInterface
5252

5353
public function testGetValue(): void
5454
{
55-
$this->assertSame($this->value, $this->fixture->getValue());
55+
self::assertEquals(new Expression($this->value), $this->fixture->getValue());
5656
}
5757

5858
public function testIsFinal(): void
5959
{
60-
$this->assertFalse($this->fixture->isFinal());
60+
self::assertFalse($this->fixture->isFinal());
6161
}
6262

6363
public function testGetFqsen(): void
6464
{
65-
$this->assertSame($this->fqsen, $this->fixture->getFqsen());
66-
$this->assertSame($this->fqsen->getName(), $this->fixture->getName());
65+
self::assertSame($this->fqsen, $this->fixture->getFqsen());
66+
self::assertSame($this->fqsen->getName(), $this->fixture->getName());
6767
}
6868

6969
public function testGetDocblock(): void
7070
{
71-
$this->assertSame($this->docBlock, $this->fixture->getDocBlock());
71+
self::assertSame($this->docBlock, $this->fixture->getDocBlock());
7272
}
7373

7474
public function testGetVisibility(): void
7575
{
76-
$this->assertEquals(new Visibility(Visibility::PUBLIC_), $this->fixture->getVisibility());
76+
self::assertEquals(new Visibility(Visibility::PUBLIC_), $this->fixture->getVisibility());
7777
}
7878

7979
public function testLineAndColumnNumberIsReturnedWhenALocationIsProvided(): void

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

Lines changed: 128 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,18 @@ final class EnumCaseTest extends TestCase
3232
private DocBlock $docBlock;
3333

3434
/**
35-
* Creates a new (emoty) fixture object.
35+
* Creates a new (empty) fixture object.
3636
*/
3737
protected function setUp(): void
3838
{
3939
$this->fqsen = new Fqsen('\Enum::VALUE');
4040
$this->docBlock = new DocBlock('');
4141

42-
$this->fixture = new EnumCase($this->fqsen, $this->docBlock);
42+
// needed for MetaDataContainer testing
43+
$this->fixture = new EnumCase(
44+
$this->fqsen,
45+
$this->docBlock,
46+
);
4347
}
4448

4549
private function getFixture(): MetaDataContainerInterface
@@ -49,26 +53,141 @@ private function getFixture(): MetaDataContainerInterface
4953

5054
public function testGettingName(): void
5155
{
52-
$this->assertSame($this->fqsen->getName(), $this->fixture->getName());
56+
$fixture = new EnumCase(
57+
$this->fqsen,
58+
$this->docBlock,
59+
);
60+
61+
$this->assertSame($this->fqsen->getName(), $fixture->getName());
5362
}
5463

5564
public function testGettingFqsen(): void
5665
{
57-
$this->assertSame($this->fqsen, $this->fixture->getFqsen());
66+
$fixture = new EnumCase(
67+
$this->fqsen,
68+
$this->docBlock,
69+
);
70+
71+
$this->assertSame($this->fqsen, $fixture->getFqsen());
5872
}
5973

6074
public function testGettingDocBlock(): void
6175
{
62-
$this->assertSame($this->docBlock, $this->fixture->getDocBlock());
76+
$fixture = new EnumCase(
77+
$this->fqsen,
78+
$this->docBlock,
79+
);
80+
81+
$this->assertSame($this->docBlock, $fixture->getDocBlock());
82+
}
83+
84+
/**
85+
* @covers ::getValue
86+
*/
87+
public function testValueCanBeOmitted(): void
88+
{
89+
$fixture = new EnumCase(
90+
$this->fqsen,
91+
$this->docBlock,
92+
);
93+
94+
$this->assertNull($fixture->getValue());
95+
}
96+
97+
/**
98+
* @uses Expression
99+
*
100+
* @covers ::getValue
101+
*/
102+
public function testValueCanBeProvidedAsAnExpression(): void
103+
{
104+
$expression = new Expression('Enum case expression');
105+
$fixture = new EnumCase(
106+
$this->fqsen,
107+
$this->docBlock,
108+
null,
109+
null,
110+
$expression,
111+
);
112+
113+
$this->assertSame($expression, $fixture->getValue(false));
63114
}
64115

65-
public function testGetValue(): void
116+
/**
117+
* @uses Expression
118+
*
119+
* @covers ::getValue
120+
*/
121+
public function testValueCanBeReturnedAsString(): void
66122
{
67-
$this->assertNull($this->fixture->getValue());
123+
$expression = new Expression('Enum case expression');
124+
$fixture = new EnumCase(
125+
$this->fqsen,
126+
$this->docBlock,
127+
null,
128+
null,
129+
$expression,
130+
);
131+
132+
$this->assertSame('Enum case expression', $fixture->getValue(true));
68133
}
69134

70-
public function testGetLocationReturnsDefault(): void
135+
/**
136+
* @covers ::getLocation
137+
*/
138+
public function testGetLocationReturnsProvidedValue(): void
71139
{
72-
self::assertEquals(new Location(-1), $this->fixture->getLocation());
140+
$location = new Location(15, 10);
141+
$fixture = new EnumCase(
142+
$this->fqsen,
143+
$this->docBlock,
144+
$location,
145+
);
146+
147+
self::assertSame($location, $fixture->getLocation());
148+
}
149+
150+
/**
151+
* @uses Location
152+
*
153+
* @covers ::getLocation
154+
*/
155+
public function testGetLocationReturnsUnknownByDefault(): void
156+
{
157+
$fixture = new EnumCase(
158+
$this->fqsen,
159+
$this->docBlock,
160+
);
161+
162+
self::assertEquals(new Location(-1), $fixture->getLocation());
163+
}
164+
165+
/**
166+
* @covers ::getEndLocation
167+
*/
168+
public function testGetEndLocationReturnsProvidedValue(): void
169+
{
170+
$location = new Location(11, 23);
171+
$fixture = new EnumCase(
172+
$this->fqsen,
173+
$this->docBlock,
174+
null,
175+
$location,
176+
);
177+
178+
self::assertSame($location, $fixture->getEndLocation());
179+
}
180+
181+
/**
182+
* @covers ::getEndLocation
183+
*/
184+
public function testGetEndLocationReturnsUnknownByDefault(): void
185+
{
186+
$fixture = new EnumCase(
187+
$this->fqsen,
188+
$this->docBlock,
189+
);
190+
191+
self::assertEquals(new Location(-1), $fixture->getEndLocation());
73192
}
74193
}

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)