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

Commit da51523

Browse files
Standardise use of CallbackViewConverter in tests (#31)
* Standardise use of CallbackViewConverter in tests * Improve test * Not needed twice * Less changes, and fix template * Rename * Remove option * Bit simpler
1 parent 1da3225 commit da51523

File tree

6 files changed

+68
-138
lines changed

6 files changed

+68
-138
lines changed

vendor-extra/ContentPageBundle/tests/ViewConvertingTestCase.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,27 @@
88
use Libero\ViewsBundle\Views\CallbackViewConverter;
99
use Libero\ViewsBundle\Views\View;
1010
use Libero\ViewsBundle\Views\ViewConverter;
11+
use LogicException;
1112

1213
trait ViewConvertingTestCase
1314
{
14-
final protected function createConverter() : ViewConverter
15+
final protected function createDumpingConverter() : ViewConverter
1516
{
1617
return new CallbackViewConverter(
1718
function (Element $object, ?string $template, array $context) : View {
18-
return new View($template, ['element' => $object->getNodePath(), 'context' => $context]);
19+
return new View(
20+
null,
21+
['element' => $object->getNodePath(), 'template' => $template, 'context' => $context]
22+
);
23+
}
24+
);
25+
}
26+
27+
final protected function createFailingConverter() : ViewConverter
28+
{
29+
return new CallbackViewConverter(
30+
function () : View {
31+
throw new LogicException('Not expected to be used');
1932
}
2033
);
2134
}

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

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@
88
use FluentDOM\DOM\Element;
99
use Libero\ContentPageBundle\Handler\ContentHandler;
1010
use Libero\JatsContentBundle\Handler\JatsContentHandler;
11-
use Libero\ViewsBundle\Views\CallbackViewConverter;
12-
use Libero\ViewsBundle\Views\View;
13-
use LogicException;
1411
use PHPUnit\Framework\TestCase;
1512
use tests\Libero\ContentPageBundle\ViewConvertingTestCase;
1613
use UnexpectedValueException;
@@ -25,13 +22,7 @@ final class JatsContentHandlerTest extends TestCase
2522
*/
2623
public function it_is_a_content_handler() : void
2724
{
28-
$handler = new JatsContentHandler(
29-
new CallbackViewConverter(
30-
function () : View {
31-
throw new LogicException();
32-
}
33-
)
34-
);
25+
$handler = new JatsContentHandler($this->createFailingConverter());
3526

3627
$this->assertInstanceOf(ContentHandler::class, $handler);
3728
}
@@ -42,7 +33,7 @@ function () : View {
4233
*/
4334
public function it_returns_the_title(string $xml, array $context, array $expected) : void
4435
{
45-
$handler = new JatsContentHandler($this->createConverter());
36+
$handler = new JatsContentHandler($this->createDumpingConverter());
4637

4738
$document = FluentDOM::load($xml);
4839
/** @var Element $documentElement */
@@ -85,9 +76,10 @@ public function pageProvider() : iterable
8576
'title' => null,
8677
'content' => [
8778
[
88-
'template' => '@LiberoPatterns/content-header.html.twig',
79+
'template' => null,
8980
'arguments' => [
9081
'element' => '/libero:item/jats:article/jats:front',
82+
'template' => '@LiberoPatterns/content-header.html.twig',
9183
'context' => [
9284
'lang' => 'en',
9385
'dir' => 'ltr',
@@ -127,9 +119,10 @@ public function pageProvider() : iterable
127119
'title' => null,
128120
'content' => [
129121
[
130-
'template' => '@LiberoPatterns/content-header.html.twig',
122+
'template' => null,
131123
'arguments' => [
132124
'element' => '/libero:item/jats:article/jats:front',
125+
'template' => '@LiberoPatterns/content-header.html.twig',
133126
'context' => [
134127
'lang' => 'fr',
135128
'dir' => 'ltr',
@@ -169,9 +162,10 @@ public function pageProvider() : iterable
169162
'title' => null,
170163
'content' => [
171164
[
172-
'template' => '@LiberoPatterns/content-header.html.twig',
165+
'template' => null,
173166
'arguments' => [
174167
'element' => '/libero:item/jats:article/jats:front',
168+
'template' => '@LiberoPatterns/content-header.html.twig',
175169
'context' => [
176170
'lang' => 'ar-EG',
177171
'dir' => 'rtl',
@@ -188,13 +182,7 @@ public function pageProvider() : iterable
188182
*/
189183
public function it_fails_if_it_does_not_find_the_front() : void
190184
{
191-
$handler = new JatsContentHandler(
192-
new CallbackViewConverter(
193-
function () : View {
194-
throw new LogicException();
195-
}
196-
)
197-
);
185+
$handler = new JatsContentHandler($this->createFailingConverter());
198186

199187
$document = FluentDOM::load(
200188
<<<XML

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

Lines changed: 16 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,21 @@
77
use FluentDOM;
88
use FluentDOM\DOM\Element;
99
use Libero\JatsContentBundle\ViewConverter\FrontContentHeaderVisitor;
10-
use Libero\ViewsBundle\Views\CallbackViewConverter;
1110
use Libero\ViewsBundle\Views\View;
12-
use LogicException;
1311
use PHPUnit\Framework\TestCase;
12+
use tests\Libero\ContentPageBundle\ViewConvertingTestCase;
1413

1514
final class FrontContentHeaderVisitorTest extends TestCase
1615
{
16+
use ViewConvertingTestCase;
17+
1718
/**
1819
* @test
1920
* @dataProvider nodeProvider
2021
*/
2122
public function it_does_nothing_if_it_is_not_a_jats_front_element(string $xml) : void
2223
{
23-
$visitor = new FrontContentHeaderVisitor(
24-
new CallbackViewConverter(
25-
function () : View {
26-
throw new LogicException();
27-
}
28-
)
29-
);
24+
$visitor = new FrontContentHeaderVisitor($this->createFailingConverter());
3025

3126
$xml = FluentDOM::load("<foo>${xml}</foo>");
3227
/** @var Element $element */
@@ -51,13 +46,7 @@ public function nodeProvider() : iterable
5146
*/
5247
public function it_does_nothing_if_is_not_the_content_header_template() : void
5348
{
54-
$visitor = new FrontContentHeaderVisitor(
55-
new CallbackViewConverter(
56-
function () : View {
57-
throw new LogicException();
58-
}
59-
)
60-
);
49+
$visitor = new FrontContentHeaderVisitor($this->createFailingConverter());
6150

6251
$xml = FluentDOM::load(
6352
<<<XML
@@ -87,13 +76,7 @@ function () : View {
8776
*/
8877
public function it_does_nothing_if_there_is_no_title_group() : void
8978
{
90-
$visitor = new FrontContentHeaderVisitor(
91-
new CallbackViewConverter(
92-
function () : View {
93-
throw new LogicException();
94-
}
95-
)
96-
);
79+
$visitor = new FrontContentHeaderVisitor($this->createFailingConverter());
9780

9881
$xml = FluentDOM::load('<front xmlns="http://jats.nlm.nih.gov"><article-meta/></front>');
9982
/** @var Element $element */
@@ -116,13 +99,7 @@ function () : View {
11699
*/
117100
public function it_does_nothing_if_there_is_already_a_content_title_set() : void
118101
{
119-
$visitor = new FrontContentHeaderVisitor(
120-
new CallbackViewConverter(
121-
function () : View {
122-
throw new LogicException();
123-
}
124-
)
125-
);
102+
$visitor = new FrontContentHeaderVisitor($this->createFailingConverter());
126103

127104
$xml = FluentDOM::load(
128105
<<<XML
@@ -156,24 +133,18 @@ function () : View {
156133
*/
157134
public function it_sets_the_text_argument() : void
158135
{
159-
$visitor = new FrontContentHeaderVisitor(
160-
new CallbackViewConverter(
161-
function (Element $object, ?string $template, array $context) : View {
162-
return new View('child', ['object' => $object, 'template' => $template, 'context' => $context]);
163-
}
164-
)
165-
);
136+
$visitor = new FrontContentHeaderVisitor($this->createDumpingConverter());
166137

167138
$xml = FluentDOM::load(
168139
<<<XML
169140
<?xml version="1.0" encoding="UTF-8"?>
170-
<front xmlns="http://jats.nlm.nih.gov">
171-
<article-meta>
172-
<title-group>
173-
<article-title>foo</article-title>
174-
</title-group>
175-
</article-meta>
176-
</front>
141+
<jats:front xmlns:jats="http://jats.nlm.nih.gov">
142+
<jats:article-meta>
143+
<jats:title-group>
144+
<jats:article-title>foo</jats:article-title>
145+
</jats:title-group>
146+
</jats:article-meta>
147+
</jats:front>
177148
XML
178149
);
179150
/** @var Element $element */
@@ -182,17 +153,11 @@ function (Element $object, ?string $template, array $context) : View {
182153
$newContext = ['foo' => 'bar'];
183154
$view = $visitor->visit($element, new View('@LiberoPatterns/content-header.html.twig'), $newContext);
184155

185-
/** @var Element $articleMeta */
186-
$articleMeta = $element->childNodes->item(0);
187-
188-
/** @var Element $titleGroup */
189-
$titleGroup = $articleMeta->childNodes->item(0);
190-
191156
$this->assertSame('@LiberoPatterns/content-header.html.twig', $view->getTemplate());
192157
$this->assertEquals(
193158
[
194159
'contentTitle' => [
195-
'object' => $titleGroup->childNodes->item(0),
160+
'element' => '/jats:front/jats:article-meta/jats:title-group/jats:article-title',
196161
'template' => '@LiberoPatterns/heading.html.twig',
197162
'context' => ['foo' => 'bar'],
198163
],

vendor-extra/LiberoContentBundle/tests/Handler/LiberoContentHandlerTest.php

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@
88
use FluentDOM\DOM\Element;
99
use Libero\ContentPageBundle\Handler\ContentHandler;
1010
use Libero\LiberoContentBundle\Handler\LiberoContentHandler;
11-
use Libero\ViewsBundle\Views\CallbackViewConverter;
12-
use Libero\ViewsBundle\Views\View;
13-
use LogicException;
1411
use PHPUnit\Framework\TestCase;
1512
use tests\Libero\ContentPageBundle\ViewConvertingTestCase;
1613
use UnexpectedValueException;
@@ -25,13 +22,7 @@ final class LiberoContentHandlerTest extends TestCase
2522
*/
2623
public function it_is_a_content_handler() : void
2724
{
28-
$handler = new LiberoContentHandler(
29-
new CallbackViewConverter(
30-
function () : View {
31-
throw new LogicException();
32-
}
33-
)
34-
);
25+
$handler = new LiberoContentHandler($this->createFailingConverter());
3526

3627
$this->assertInstanceOf(ContentHandler::class, $handler);
3728
}
@@ -42,7 +33,7 @@ function () : View {
4233
*/
4334
public function it_returns_the_title(string $xml, array $context, array $expected) : void
4435
{
45-
$handler = new LiberoContentHandler($this->createConverter());
36+
$handler = new LiberoContentHandler($this->createDumpingConverter());
4637

4738
$document = FluentDOM::load($xml);
4839
/** @var Element $documentElement */
@@ -79,9 +70,10 @@ public function pageProvider() : iterable
7970
'title' => null,
8071
'content' => [
8172
[
82-
'template' => '@LiberoPatterns/content-header.html.twig',
73+
'template' => null,
8374
'arguments' => [
8475
'element' => '/libero:item/libero:front',
76+
'template' => '@LiberoPatterns/content-header.html.twig',
8577
'context' => [
8678
'lang' => 'en',
8779
'dir' => 'ltr',
@@ -115,9 +107,10 @@ public function pageProvider() : iterable
115107
'title' => null,
116108
'content' => [
117109
[
118-
'template' => '@LiberoPatterns/content-header.html.twig',
110+
'template' => null,
119111
'arguments' => [
120112
'element' => '/libero:item/libero:front',
113+
'template' => '@LiberoPatterns/content-header.html.twig',
121114
'context' => [
122115
'lang' => 'fr',
123116
'dir' => 'ltr',
@@ -151,9 +144,10 @@ public function pageProvider() : iterable
151144
'title' => null,
152145
'content' => [
153146
[
154-
'template' => '@LiberoPatterns/content-header.html.twig',
147+
'template' => null,
155148
'arguments' => [
156149
'element' => '/libero:item/libero:front',
150+
'template' => '@LiberoPatterns/content-header.html.twig',
157151
'context' => [
158152
'lang' => 'ar-EG',
159153
'dir' => 'rtl',
@@ -170,13 +164,7 @@ public function pageProvider() : iterable
170164
*/
171165
public function it_fails_if_it_does_not_find_the_front() : void
172166
{
173-
$handler = new LiberoContentHandler(
174-
new CallbackViewConverter(
175-
function () : View {
176-
throw new LogicException();
177-
}
178-
)
179-
);
167+
$handler = new LiberoContentHandler($this->createFailingConverter());
180168

181169
$document = FluentDOM::load(
182170
<<<XML

0 commit comments

Comments
 (0)