Skip to content

Commit a7bf4e2

Browse files
mvrieljaapio
authored andcommitted
Add test for Expressions
1 parent a537057 commit a7bf4e2

File tree

1 file changed

+136
-0
lines changed

1 file changed

+136
-0
lines changed
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace phpDocumentor\Reflection\Php;
6+
7+
use InvalidArgumentException;
8+
use phpDocumentor\Reflection\Fqsen;
9+
use PHPUnit\Framework\TestCase;
10+
11+
use function sprintf;
12+
13+
/**
14+
* @coversDefaultClass Expression
15+
* @covers ::__construct
16+
* @covers ::<private>
17+
*/
18+
final class ExpressionTest extends TestCase
19+
{
20+
private const EXAMPLE_FQSEN = '\\' . self::class;
21+
private const EXAMPLE_FQSEN_PLACEHOLDER = '{{ PHPDOC0450ed2a7bac1efcf0c13b6560767954 }}';
22+
23+
/**
24+
* @covers ::generatePlaceholder
25+
*/
26+
public function testGeneratingPlaceholder(): void
27+
{
28+
$placeholder = Expression::generatePlaceholder(self::EXAMPLE_FQSEN);
29+
30+
self::assertSame(self::EXAMPLE_FQSEN_PLACEHOLDER, $placeholder);
31+
}
32+
33+
/**
34+
* @covers ::generatePlaceholder
35+
*/
36+
public function testGeneratingPlaceholderErrorsUponPassingAnEmptyName(): void
37+
{
38+
$this->expectException(InvalidArgumentException::class);
39+
40+
Expression::generatePlaceholder('');
41+
}
42+
43+
/**
44+
* @covers ::__construct
45+
*/
46+
public function testExpressionTemplateCannotBeEmpty(): void
47+
{
48+
$this->expectException(InvalidArgumentException::class);
49+
50+
new Expression('', []);
51+
}
52+
53+
/**
54+
* @covers ::__construct
55+
*/
56+
public function testPartsShouldContainFqsensOrTypes(): void
57+
{
58+
$this->expectException(InvalidArgumentException::class);
59+
60+
new Expression('This is an expression', [self::EXAMPLE_FQSEN_PLACEHOLDER => self::EXAMPLE_FQSEN]);
61+
}
62+
63+
/**
64+
* @covers ::__construct
65+
* @covers ::getExpression
66+
*/
67+
public function testGetExpressionTemplateString(): void
68+
{
69+
$expressionTemplate = sprintf('This is an %s expression', self::EXAMPLE_FQSEN_PLACEHOLDER);
70+
$parts = [self::EXAMPLE_FQSEN_PLACEHOLDER => new Fqsen(self::EXAMPLE_FQSEN)];
71+
$expression = new Expression($expressionTemplate, $parts);
72+
73+
$result = $expression->getExpression();
74+
75+
self::assertSame($expressionTemplate, $result);
76+
}
77+
78+
/**
79+
* @covers ::__construct
80+
* @covers ::getParts
81+
*/
82+
public function testGetExtractedParts(): void
83+
{
84+
$expressionTemplate = sprintf('This is an %s expression', self::EXAMPLE_FQSEN_PLACEHOLDER);
85+
$parts = [self::EXAMPLE_FQSEN_PLACEHOLDER => new Fqsen(self::EXAMPLE_FQSEN)];
86+
$expression = new Expression($expressionTemplate, $parts);
87+
88+
$result = $expression->getParts();
89+
90+
self::assertSame($parts, $result);
91+
}
92+
93+
/**
94+
* @covers ::__toString
95+
*/
96+
public function testReplacePlaceholdersWhenCastingToString(): void
97+
{
98+
$expressionTemplate = sprintf('This is an %s expression', self::EXAMPLE_FQSEN_PLACEHOLDER);
99+
$parts = [self::EXAMPLE_FQSEN_PLACEHOLDER => new Fqsen(self::EXAMPLE_FQSEN)];
100+
$expression = new Expression($expressionTemplate, $parts);
101+
102+
$result = (string) $expression;
103+
104+
self::assertSame(sprintf('This is an %s expression', self::EXAMPLE_FQSEN), $result);
105+
}
106+
107+
/**
108+
* @covers ::render
109+
*/
110+
public function testRenderingExpressionWithoutOverridesIsTheSameAsWhenCastingToString(): void
111+
{
112+
$expressionTemplate = sprintf('This is an %s expression', self::EXAMPLE_FQSEN_PLACEHOLDER);
113+
$parts = [self::EXAMPLE_FQSEN_PLACEHOLDER => new Fqsen(self::EXAMPLE_FQSEN)];
114+
$expression = new Expression($expressionTemplate, $parts);
115+
116+
$result = $expression->render();
117+
118+
self::assertSame((string) $expression, $result);
119+
}
120+
121+
/**
122+
* @covers ::render
123+
*/
124+
public function testOverridePartsWhenRenderingExpression(): void
125+
{
126+
$replacement = 'ExpressionTest';
127+
128+
$expressionTemplate = sprintf('This is an %s expression', self::EXAMPLE_FQSEN_PLACEHOLDER);
129+
$parts = [self::EXAMPLE_FQSEN_PLACEHOLDER => new Fqsen(self::EXAMPLE_FQSEN)];
130+
$expression = new Expression($expressionTemplate, $parts);
131+
132+
$result = $expression->render([self::EXAMPLE_FQSEN_PLACEHOLDER => $replacement]);
133+
134+
self::assertSame(sprintf('This is an %s expression', $replacement), $result);
135+
}
136+
}

0 commit comments

Comments
 (0)