Skip to content

Commit 3818932

Browse files
committed
Add tests for modern PHP syntax
1 parent 81e707a commit 3818932

File tree

3 files changed

+135
-9
lines changed

3 files changed

+135
-9
lines changed

tests/IntegrationTest.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,8 @@ public function testParseUnitBlock(string $blockName)
115115
$expected = preg_replace('/<\!\-\- REMOVE(.)+\-\->/', '', $expected);
116116

117117
$this->assertSame(
118-
$indenter->indent($expected),
119-
$indenter->indent(trim($actualCrawler->filter('body')->html()))
118+
$this->normalize($indenter->indent($expected)),
119+
$this->normalize($indenter->indent(trim($actualCrawler->filter('body')->html())))
120120
);
121121
}
122122

@@ -351,6 +351,11 @@ public function testParseString()
351351
$this->assertSame($htmlString, (new DocBuilder())->buildString($rstString)->getStringResult());
352352
}
353353

354+
private function normalize(string $str): string
355+
{
356+
return preg_replace('/\s+$/m', '', $str);
357+
}
358+
354359
private function createIndenter(): Indenter
355360
{
356361
$indenter = new Indenter();

tests/fixtures/expected/blocks/code-blocks/php.html

Lines changed: 85 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,94 @@
66
4
77
5
88
6
9-
7</pre>
9+
7
10+
8
11+
9</pre>
1012
<pre class="codeblock-code"><code><span class="hljs-comment">// config/routes.php</span>
1113
<span class="hljs-keyword">namespace</span> <span class="hljs-title">Symfony</span>\<span class="hljs-title">Component</span>\<span class="hljs-title">Routing</span>\<span class="hljs-title">Loader</span>\<span class="hljs-title">Configurator</span>;
1214

13-
<span class="hljs-keyword">return</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-params">(RoutingConfigurator <span class="hljs-variable"><span class="hljs-variable-other-marker">$</span>routes</span>)</span> </span>{
14-
<span class="hljs-variable"><span class="hljs-variable-other-marker">$</span>routes</span><span class="hljs-operator">-&gt;</span>add(<span class="hljs-string">'about_us'</span>, [<span class="hljs-string">'nl'</span> =&gt; <span class="hljs-string">'/over-ons'</span>, <span class="hljs-string">'en'</span> =&gt; <span class="hljs-string">'/about-us'</span>])
15-
<span class="hljs-operator">-&gt;</span>controller(<span class="hljs-string">'App\Controller\CompanyController::about'</span>);
15+
<span class="hljs-keyword">use</span> <span class="hljs-title">App</span>\<span class="hljs-title">Controller</span>\<span class="hljs-title">CompanyController</span>;
16+
17+
<span class="hljs-keyword">return</span> <span class="hljs-keyword">static</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-params">(RoutingConfigurator <span class="hljs-variable"><span class="hljs-variable-other-marker">$</span>routes</span>)</span>: <span class="hljs-title">void</span> </span>{
18+
<span class="hljs-variable"><span class="hljs-variable-other-marker">$</span>routes</span>-&gt;<span class="hljs-title invoke__">add</span>(<span class="hljs-string">'about_us'</span>, [<span class="hljs-string">'nl'</span> =&gt; <span class="hljs-string">'/over-ons'</span>, <span class="hljs-string">'en'</span> =&gt; <span class="hljs-string">'/about-us'</span>])
19+
-&gt;<span class="hljs-title invoke__">controller</span>(<span class="hljs-title class_">CompanyController</span>::<span class="hljs-keyword">class</span> <span class="hljs-string">'::about'</span>);
1620
};</code></pre>
1721
</div>
1822
</div>
23+
<div translate="no" data-loc="16" class="notranslate codeblock codeblock-length-md codeblock-php">
24+
<div class="codeblock-scroll">
25+
<pre class="codeblock-lines">1
26+
2
27+
3
28+
4
29+
5
30+
6
31+
7
32+
8
33+
9
34+
10
35+
11
36+
12
37+
13
38+
14
39+
15
40+
16</pre>
41+
<pre class="codeblock-code"><code>
42+
<span class="hljs-class"><span class="hljs-keyword">enum</span> <span class="hljs-title">TextAlign</span>: <span class="hljs-title">string</span> <span class="hljs-keyword">implements</span> <span class="hljs-title">TranslatableInterface</span></span>
43+
{
44+
<span class="hljs-keyword">case</span> Left = <span class="hljs-string">'Left aligned'</span>;
45+
<span class="hljs-keyword">case</span> Center = <span class="hljs-string">'Center aligned'</span>;
46+
<span class="hljs-keyword">case</span> Right = <span class="hljs-string">'Right aligned'</span>;
47+
48+
<span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">trans</span><span class="hljs-params">(TranslatorInterface <span class="hljs-variable"><span class="hljs-variable-other-marker">$</span>translator</span>, ?<span class="hljs-keyword">string</span> <span class="hljs-variable"><span class="hljs-variable-other-marker">$</span>locale</span> = <span class="hljs-keyword">null</span>)</span>: <span class="hljs-title">string</span></span>
49+
{
50+
<span class="hljs-comment">// Translate enum using custom labels</span>
51+
<span class="hljs-keyword">return</span> <span class="hljs-keyword">match</span> (<span class="hljs-variable"><span class="hljs-variable-other-marker">$</span>this</span>) {
52+
<span class="hljs-keyword">self</span>::Left =&gt; <span class="hljs-variable"><span class="hljs-variable-other-marker">$</span>translator</span>-&gt;<span class="hljs-title invoke__">trans</span>(<span class="hljs-string">'text_align.left.label'</span>, <span class="hljs-attr">locale</span>: <span class="hljs-variable"><span class="hljs-variable-other-marker">$</span>locale</span>),
53+
<span class="hljs-keyword">self</span>::Center =&gt; <span class="hljs-variable"><span class="hljs-variable-other-marker">$</span>translator</span>-&gt;<span class="hljs-title invoke__">trans</span>(<span class="hljs-string">'text_align.center.label'</span>, <span class="hljs-attr">locale</span>: <span class="hljs-variable"><span class="hljs-variable-other-marker">$</span>locale</span>),
54+
<span class="hljs-keyword">self</span>::Right =&gt; <span class="hljs-variable"><span class="hljs-variable-other-marker">$</span>translator</span>-&gt;<span class="hljs-title invoke__">trans</span>(<span class="hljs-string">'text_align.right.label'</span>, <span class="hljs-attr">locale</span>: <span class="hljs-variable"><span class="hljs-variable-other-marker">$</span>locale</span>),
55+
};
56+
}
57+
}
58+
</code>
59+
</pre>
60+
</div>
61+
</div>
62+
<div translate="no" data-loc="16" class="notranslate codeblock codeblock-length-md codeblock-php">
63+
<div class="codeblock-scroll">
64+
<pre class="codeblock-lines">1
65+
2
66+
3
67+
4
68+
5
69+
6
70+
7
71+
8
72+
9
73+
10
74+
11
75+
12
76+
13
77+
14
78+
15
79+
16</pre>
80+
<pre class="codeblock-code"><code>
81+
<span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">getUserBadgeFrom</span><span class="hljs-params">(<span class="hljs-keyword">string</span> <span class="hljs-variable"><span class="hljs-variable-other-marker">$</span>accessToken</span>)</span>: <span class="hljs-title">UserBadge</span></span>
82+
{
83+
<span class="hljs-comment">// get the data from the token</span>
84+
<span class="hljs-variable"><span class="hljs-variable-other-marker">$</span>payload</span> = ...;
85+
86+
<span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-title invoke__">UserBadge</span>(
87+
<span class="hljs-variable"><span class="hljs-variable-other-marker">$</span>payload</span>-&gt;<span class="hljs-title invoke__">getUserId</span>(),
88+
<span class="hljs-function"><span class="hljs-keyword">fn</span> <span class="hljs-params">(<span class="hljs-keyword">string</span> <span class="hljs-variable"><span class="hljs-variable-other-marker">$</span>userIdentifier</span>)</span>: <span class="hljs-title">User</span></span> =&gt; <span class="hljs-keyword">new</span> <span class="hljs-title invoke__">User</span>(<span class="hljs-variable"><span class="hljs-variable-other-marker">$</span>userIdentifier</span>, <span class="hljs-variable"><span class="hljs-variable-other-marker">$</span>payload</span>-&gt;<span class="hljs-title invoke__">getRoles</span>())
89+
);
90+
91+
<span class="hljs-comment">// or</span>
92+
<span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-title invoke__">UserBadge</span>(
93+
<span class="hljs-variable"><span class="hljs-variable-other-marker">$</span>payload</span>-&gt;<span class="hljs-title invoke__">getUserId</span>(),
94+
<span class="hljs-variable"><span class="hljs-variable-other-marker">$</span>this</span>-&gt;<span class="hljs-title invoke__">loadUser</span>(...)
95+
);
96+
}
97+
</code></pre>
98+
</div>
99+
</div>
Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,49 @@
1-
21
.. code-block:: php
2+
33
// config/routes.php
44
namespace Symfony\Component\Routing\Loader\Configurator;
55
6-
return function (RoutingConfigurator $routes) {
6+
use App\Controller\CompanyController;
7+
8+
return static function (RoutingConfigurator $routes): void {
79
$routes->add('about_us', ['nl' => '/over-ons', 'en' => '/about-us'])
8-
->controller('App\Controller\CompanyController::about');
10+
->controller(CompanyController::class.'::about');
911
};
12+
13+
.. code-block:: php
14+
15+
enum TextAlign: string implements TranslatableInterface
16+
{
17+
case Left = 'Left aligned';
18+
case Center = 'Center aligned';
19+
case Right = 'Right aligned';
20+
21+
public function trans(TranslatorInterface $translator, ?string $locale = null): string
22+
{
23+
// Translate enum using custom labels
24+
return match ($this) {
25+
self::Left => $translator->trans('text_align.left.label', locale: $locale),
26+
self::Center => $translator->trans('text_align.center.label', locale: $locale),
27+
self::Right => $translator->trans('text_align.right.label', locale: $locale),
28+
};
29+
}
30+
}
31+
32+
.. code-block:: php
33+
34+
public function getUserBadgeFrom(string $accessToken): UserBadge
35+
{
36+
// get the data from the token
37+
$payload = ...;
38+
39+
return new UserBadge(
40+
$payload->getUserId(),
41+
fn (string $userIdentifier): User => new User($userIdentifier, $payload->getRoles())
42+
);
43+
44+
// or
45+
return new UserBadge(
46+
$payload->getUserId(),
47+
$this->loadUser(...)
48+
);
49+
}

0 commit comments

Comments
 (0)