Skip to content
This repository was archived by the owner on Nov 21, 2019. It is now read-only.

Commit d111720

Browse files
Inline templating (#32)
* Standardise use of CallbackViewConverter in tests * Improve test * Not needed twice * Less changes, and fix template * Add inline view converters * Make a trait rather than another method * Can simplify and use the same main visitor interface as we'll only look at elements * Fix name * New trait * It's all foo bar * Reuse same visitor interface (and standardise XML terminology) * Revert (incomplete) renames where the type isn't changing
1 parent da51523 commit d111720

23 files changed

+611
-50
lines changed

phpstan.neon.dist

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
parameters:
22
ignoreErrors:
3+
- '~^Call to an undefined method FluentDOM\\DOM\\Node\\.+?::getNodePath\(\)\.$~'
4+
- '~^Cannot cast FluentDOM\\DOM\\Node\\.+? to string\.$~'
35
- '~^Cannot call method ([a-z]+)Node\(\) on Symfony\\Component\\Config\\Definition\\Builder\\NodeParentInterface\|null\.$~'
46
- '~^Service "csa_guzzle\.mock\.storage" is not registered in the container\.$~'
57
level: max

vendor-extra/ContentPageBundle/tests/ViewConvertingTestCase.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace tests\Libero\ContentPageBundle;
66

7-
use FluentDOM\DOM\Element;
7+
use FluentDOM\DOM\Node\NonDocumentTypeChildNode;
88
use Libero\ViewsBundle\Views\CallbackViewConverter;
99
use Libero\ViewsBundle\Views\View;
1010
use Libero\ViewsBundle\Views\ViewConverter;
@@ -15,10 +15,10 @@ trait ViewConvertingTestCase
1515
final protected function createDumpingConverter() : ViewConverter
1616
{
1717
return new CallbackViewConverter(
18-
function (Element $object, ?string $template, array $context) : View {
18+
function (NonDocumentTypeChildNode $node, ?string $template = null, array $context = []) : View {
1919
return new View(
2020
null,
21-
['element' => $object->getNodePath(), 'template' => $template, 'context' => $context]
21+
['node' => $node->getNodePath(), 'template' => $template, 'context' => $context]
2222
);
2323
}
2424
);

vendor-extra/JatsContentBundle/src/Resources/config/services.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
<service id="Libero\JatsContentBundle\ViewConverter\ArticleTitleHeadingVisitor"
1818
class="Libero\JatsContentBundle\ViewConverter\ArticleTitleHeadingVisitor">
19+
<argument type="service" id="Libero\ViewsBundle\Views\ViewConverter"/>
1920
<tag name="libero.view_converter"/>
2021
</service>
2122

@@ -25,6 +26,12 @@
2526
<tag name="libero.view_converter"/>
2627
</service>
2728

29+
<service id="Libero\JatsContentBundle\ViewConverter\ItalicVisitor"
30+
class="Libero\JatsContentBundle\ViewConverter\ItalicVisitor">
31+
<argument type="service" id="Libero\ViewsBundle\Views\ViewConverter"/>
32+
<tag name="libero.view_converter"/>
33+
</service>
34+
2835
</services>
2936

3037
</container>

vendor-extra/JatsContentBundle/src/ViewConverter/ArticleTitleHeadingVisitor.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,25 @@
55
namespace Libero\JatsContentBundle\ViewConverter;
66

77
use FluentDOM\DOM\Element;
8+
use Libero\ViewsBundle\Views\ConvertsChildren;
89
use Libero\ViewsBundle\Views\SimplifiedVisitor;
910
use Libero\ViewsBundle\Views\View;
11+
use Libero\ViewsBundle\Views\ViewConverter;
1012
use Libero\ViewsBundle\Views\ViewConverterVisitor;
1113

1214
final class ArticleTitleHeadingVisitor implements ViewConverterVisitor
1315
{
16+
use ConvertsChildren;
1417
use SimplifiedVisitor;
1518

19+
public function __construct(ViewConverter $converter)
20+
{
21+
$this->converter = $converter;
22+
}
23+
1624
protected function doVisit(Element $object, View $view, array &$context = []) : View
1725
{
18-
return $view->withArgument('text', (string) $object);
26+
return $view->withArgument('text', $this->convertChildren($object, $context));
1927
}
2028

2129
protected function expectedTemplate() : string
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Libero\JatsContentBundle\ViewConverter;
6+
7+
use FluentDOM\DOM\Element;
8+
use Libero\ViewsBundle\Views\ConvertsChildren;
9+
use Libero\ViewsBundle\Views\SimplifiedChildVisitor;
10+
use Libero\ViewsBundle\Views\View;
11+
use Libero\ViewsBundle\Views\ViewConverter;
12+
use Libero\ViewsBundle\Views\ViewConverterVisitor;
13+
14+
final class ItalicVisitor implements ViewConverterVisitor
15+
{
16+
use ConvertsChildren;
17+
use SimplifiedChildVisitor;
18+
19+
public function __construct(ViewConverter $converter)
20+
{
21+
$this->converter = $converter;
22+
}
23+
24+
protected function doVisit(Element $object, View $view, array &$context = []) : View
25+
{
26+
return $view->withArgument('text', $this->convertChildren($object, $context));
27+
}
28+
29+
protected function possibleTemplate() : string
30+
{
31+
return '@LiberoPatterns/italic.html.twig';
32+
}
33+
34+
protected function expectedElement() : string
35+
{
36+
return '{http://jats.nlm.nih.gov}italic';
37+
}
38+
39+
protected function unexpectedArguments() : array
40+
{
41+
return ['text'];
42+
}
43+
}

vendor-extra/JatsContentBundle/tests/Handler/JatsContentHandlerTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public function pageProvider() : iterable
7878
[
7979
'template' => null,
8080
'arguments' => [
81-
'element' => '/libero:item/jats:article/jats:front',
81+
'node' => '/libero:item/jats:article/jats:front',
8282
'template' => '@LiberoPatterns/content-header.html.twig',
8383
'context' => [
8484
'lang' => 'en',
@@ -121,7 +121,7 @@ public function pageProvider() : iterable
121121
[
122122
'template' => null,
123123
'arguments' => [
124-
'element' => '/libero:item/jats:article/jats:front',
124+
'node' => '/libero:item/jats:article/jats:front',
125125
'template' => '@LiberoPatterns/content-header.html.twig',
126126
'context' => [
127127
'lang' => 'fr',
@@ -164,7 +164,7 @@ public function pageProvider() : iterable
164164
[
165165
'template' => null,
166166
'arguments' => [
167-
'element' => '/libero:item/jats:article/jats:front',
167+
'node' => '/libero:item/jats:article/jats:front',
168168
'template' => '@LiberoPatterns/content-header.html.twig',
169169
'context' => [
170170
'lang' => 'ar-EG',

vendor-extra/JatsContentBundle/tests/ViewConverter/ArticleTitleHeadingVisitorTest.php

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,19 @@
99
use Libero\JatsContentBundle\ViewConverter\ArticleTitleHeadingVisitor;
1010
use Libero\ViewsBundle\Views\View;
1111
use PHPUnit\Framework\TestCase;
12+
use tests\Libero\ContentPageBundle\ViewConvertingTestCase;
1213

1314
final class ArticleTitleHeadingVisitorTest extends TestCase
1415
{
16+
use ViewConvertingTestCase;
17+
1518
/**
1619
* @test
1720
* @dataProvider nodeProvider
1821
*/
1922
public function it_does_nothing_if_it_is_not_a_jats_article_title_element(string $xml) : void
2023
{
21-
$visitor = new ArticleTitleHeadingVisitor();
24+
$visitor = new ArticleTitleHeadingVisitor($this->createFailingConverter());
2225

2326
$xml = FluentDOM::load("<foo>${xml}</foo>");
2427
/** @var Element $element */
@@ -43,7 +46,7 @@ public function nodeProvider() : iterable
4346
*/
4447
public function it_does_nothing_if_is_not_the_heading_template() : void
4548
{
46-
$visitor = new ArticleTitleHeadingVisitor();
49+
$visitor = new ArticleTitleHeadingVisitor($this->createFailingConverter());
4750

4851
$xml = FluentDOM::load('<article-title xmlns="http://jats.nlm.nih.gov">foo</article-title>');
4952
/** @var Element $element */
@@ -62,7 +65,7 @@ public function it_does_nothing_if_is_not_the_heading_template() : void
6265
*/
6366
public function it_does_nothing_if_there_is_already_text_set() : void
6467
{
65-
$visitor = new ArticleTitleHeadingVisitor();
68+
$visitor = new ArticleTitleHeadingVisitor($this->createFailingConverter());
6669

6770
$xml = FluentDOM::load('<article-title xmlns="http://jats.nlm.nih.gov">foo</article-title>');
6871
/** @var Element $element */
@@ -85,17 +88,53 @@ public function it_does_nothing_if_there_is_already_text_set() : void
8588
*/
8689
public function it_sets_the_text_argument() : void
8790
{
88-
$visitor = new ArticleTitleHeadingVisitor();
89-
90-
$xml = FluentDOM::load('<article-title xmlns="http://jats.nlm.nih.gov">foo <bar>baz</bar></article-title>');
91+
$visitor = new ArticleTitleHeadingVisitor($this->createDumpingConverter());
92+
93+
$xml = FluentDOM::load(
94+
<<<XML
95+
<jats:article-title xmlns:jats="http://jats.nlm.nih.gov">
96+
foo <jats:italic>bar</jats:italic> baz
97+
</jats:article-title>
98+
XML
99+
);
91100
/** @var Element $element */
92101
$element = $xml->documentElement;
93102

94-
$newContext = ['foo' => 'bar'];
103+
$newContext = ['qux' => 'quux'];
95104
$view = $visitor->visit($element, new View('@LiberoPatterns/heading.html.twig'), $newContext);
96105

97106
$this->assertSame('@LiberoPatterns/heading.html.twig', $view->getTemplate());
98-
$this->assertEquals(['text' => 'foo baz'], $view->getArguments());
99-
$this->assertSame(['foo' => 'bar'], $newContext);
107+
$this->assertEquals(
108+
[
109+
'text' => [
110+
new View(
111+
null,
112+
[
113+
'node' => '/jats:article-title/text()[1]',
114+
'template' => null,
115+
'context' => ['qux' => 'quux'],
116+
]
117+
),
118+
new View(
119+
null,
120+
[
121+
'node' => '/jats:article-title/jats:italic',
122+
'template' => null,
123+
'context' => ['qux' => 'quux'],
124+
]
125+
),
126+
new View(
127+
null,
128+
[
129+
'node' => '/jats:article-title/text()[2]',
130+
'template' => null,
131+
'context' => ['qux' => 'quux'],
132+
]
133+
),
134+
],
135+
],
136+
$view->getArguments()
137+
);
138+
$this->assertSame(['qux' => 'quux'], $newContext);
100139
}
101140
}

vendor-extra/JatsContentBundle/tests/ViewConverter/FrontContentHeaderVisitorTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,20 +150,20 @@ public function it_sets_the_text_argument() : void
150150
/** @var Element $element */
151151
$element = $xml->documentElement;
152152

153-
$newContext = ['foo' => 'bar'];
153+
$newContext = ['bar' => 'baz'];
154154
$view = $visitor->visit($element, new View('@LiberoPatterns/content-header.html.twig'), $newContext);
155155

156156
$this->assertSame('@LiberoPatterns/content-header.html.twig', $view->getTemplate());
157157
$this->assertEquals(
158158
[
159159
'contentTitle' => [
160-
'element' => '/jats:front/jats:article-meta/jats:title-group/jats:article-title',
160+
'node' => '/jats:front/jats:article-meta/jats:title-group/jats:article-title',
161161
'template' => '@LiberoPatterns/heading.html.twig',
162-
'context' => ['foo' => 'bar'],
162+
'context' => ['bar' => 'baz'],
163163
],
164164
],
165165
$view->getArguments()
166166
);
167-
$this->assertSame(['foo' => 'bar'], $newContext);
167+
$this->assertSame(['bar' => 'baz'], $newContext);
168168
}
169169
}
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace tests\Libero\JatsContentBundle\ViewConverter;
6+
7+
use FluentDOM;
8+
use FluentDOM\DOM\Element;
9+
use Libero\JatsContentBundle\ViewConverter\ItalicVisitor;
10+
use Libero\ViewsBundle\Views\View;
11+
use PHPUnit\Framework\TestCase;
12+
use tests\Libero\ContentPageBundle\ViewConvertingTestCase;
13+
14+
final class ItalicVisitorTest extends TestCase
15+
{
16+
use ViewConvertingTestCase;
17+
18+
/**
19+
* @test
20+
* @dataProvider nodeProvider
21+
*/
22+
public function it_does_nothing_if_it_is_not_a_jats_italic_element(string $xml) : void
23+
{
24+
$visitor = new ItalicVisitor($this->createFailingConverter());
25+
26+
$xml = FluentDOM::load("<foo>${xml}</foo>");
27+
/** @var Element $element */
28+
$element = $xml->documentElement->firstChild;
29+
30+
$newContext = [];
31+
$view = $visitor->visit($element, new View(null), $newContext);
32+
33+
$this->assertNull($view->getTemplate());
34+
$this->assertEmpty($view->getArguments());
35+
$this->assertEmpty($newContext);
36+
}
37+
38+
public function nodeProvider() : iterable
39+
{
40+
yield 'different namespace' => ['<italic xmlns="http://example.com">foo</italic>'];
41+
yield 'different element' => ['<bold xmlns="http://jats.nlm.nih.gov">foo</bold>'];
42+
}
43+
44+
/**
45+
* @test
46+
*/
47+
public function it_does_nothing_if_is_not_the_italic_template() : void
48+
{
49+
$visitor = new ItalicVisitor($this->createFailingConverter());
50+
51+
$xml = FluentDOM::load('<italic xmlns="http://jats.nlm.nih.gov">foo</italic>');
52+
/** @var Element $element */
53+
$element = $xml->documentElement;
54+
55+
$newContext = [];
56+
$view = $visitor->visit($element, new View('template'), $newContext);
57+
58+
$this->assertSame('template', $view->getTemplate());
59+
$this->assertEmpty($view->getArguments());
60+
$this->assertEmpty($newContext);
61+
}
62+
63+
/**
64+
* @test
65+
*/
66+
public function it_does_nothing_if_there_is_already_text_set() : void
67+
{
68+
$visitor = new ItalicVisitor($this->createFailingConverter());
69+
70+
$xml = FluentDOM::load('<italic xmlns="http://jats.nlm.nih.gov">foo</italic>');
71+
/** @var Element $element */
72+
$element = $xml->documentElement;
73+
74+
$newContext = [];
75+
$view = $visitor->visit($element, new View(null, ['text' => 'bar']), $newContext);
76+
77+
$this->assertNull($view->getTemplate());
78+
$this->assertSame(['text' => 'bar'], $view->getArguments());
79+
$this->assertEmpty($newContext);
80+
}
81+
82+
/**
83+
* @test
84+
*/
85+
public function it_sets_the_template_and_text_argument() : void
86+
{
87+
$visitor = new ItalicVisitor($this->createDumpingConverter());
88+
89+
$xml = FluentDOM::load(
90+
<<<XML
91+
<jats:italic xmlns:jats="http://jats.nlm.nih.gov">
92+
foo <jats:bold>bar</jats:bold> baz
93+
</jats:italic>
94+
XML
95+
);
96+
/** @var Element $element */
97+
$element = $xml->documentElement;
98+
99+
$newContext = ['qux' => 'quux'];
100+
$view = $visitor->visit($element, new View(null), $newContext);
101+
102+
$this->assertSame('@LiberoPatterns/italic.html.twig', $view->getTemplate());
103+
$this->assertEquals(
104+
[
105+
'text' => [
106+
new View(
107+
null,
108+
['node' => '/jats:italic/text()[1]', 'template' => null, 'context' => ['qux' => 'quux']]
109+
),
110+
new View(
111+
null,
112+
['node' => '/jats:italic/jats:bold', 'template' => null, 'context' => ['qux' => 'quux']]
113+
),
114+
new View(
115+
null,
116+
['node' => '/jats:italic/text()[2]', 'template' => null, 'context' => ['qux' => 'quux']]
117+
),
118+
],
119+
],
120+
$view->getArguments()
121+
);
122+
$this->assertSame(['qux' => 'quux'], $newContext);
123+
}
124+
}

0 commit comments

Comments
 (0)