Skip to content

Commit e8088e8

Browse files
committed
Add tests to get to 100% branch coverage
1 parent a8ec38f commit e8088e8

File tree

6 files changed

+178
-34
lines changed

6 files changed

+178
-34
lines changed

src/viewmodel/ViewModelRenderer.php

+4
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,10 @@ private function objectApply(DOMElement $context, object $model): void {
426426
str_replace(['-', ':'], '', ucwords($attribute->nodeName, '-:'))
427427
);
428428

429+
if (in_array($name, ['property','prefix','vocab','resource'])) {
430+
continue;
431+
}
432+
429433
$result = match (true) {
430434
// method variants
431435
method_exists($model, $name) => $model->{$name}($attribute->nodeValue),

tests/_data/viewmodel/prefix/prefixmodel.php

+19
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,22 @@ public function __call($method, $args) {
8282
throw new \RuntimeException('FAIL:' . $method);
8383
}
8484
}
85+
86+
class PrefixPropertyViewModel {
87+
88+
public function __construct(
89+
public User $user = new User,
90+
public Header $header = new Header
91+
) {}
92+
}
93+
94+
class PrefixPropertyGetViewModel {
95+
public function __get($method) {
96+
switch ($method) {
97+
case 'user': return new User();
98+
case 'header': return new Header();
99+
}
100+
101+
throw new \RuntimeException('FAIL:' . $method);
102+
}
103+
}

tests/_data/viewmodel/resource/resourcemodel.php

+20
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,23 @@ public function __call($method, $args) {
8282
throw new \RuntimeException('FAIL:' . $method);
8383
}
8484
}
85+
86+
class ResourcePropertyViewModel {
87+
88+
public function __construct(
89+
public User $user = new User,
90+
public Header $header = new Header,
91+
) {}
92+
93+
}
94+
95+
class ResourcePropertyGetViewModel {
96+
public function __get(string $method) {
97+
switch ($method) {
98+
case 'user': return new User();
99+
case 'header': return new Header();
100+
}
101+
102+
throw new \RuntimeException('FAIL:' . $method);
103+
}
104+
}

tests/document/DocumentTest.php

+40
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
*/
1010
namespace Templado\Engine;
1111

12+
use DOMNode;
1213
use function libxml_get_errors;
1314
use ArrayIterator;
1415
use DOMDocument;
@@ -31,6 +32,7 @@
3132
#[UsesClass(DocumentCollection::class)]
3233
#[UsesClass(Signal::class)]
3334
#[UsesClass(StaticNodeList::class)]
35+
#[UsesClass(TransformationProcessor::class)]
3436
class DocumentTest extends TestCase {
3537
use DomDocumentsEqualTrait;
3638

@@ -326,5 +328,43 @@ public function testAsStringResultIsFormattedAsException(): void {
326328
$this->assertEquals($expected, (Document::fromDomDocument($xml))->asString());
327329
}
328330

331+
public function testSelectorGetsUsedWhenViewModelGetsApplied(): void {
332+
$target = Document::fromString('<root><child property="handle" /><child property="ignore" /></root>');
333+
334+
$target->applyViewModel(
335+
new class {
336+
public string $handle = 'text body';
337+
},
338+
new XPathSelector('//child[@property="handle"]')
339+
);
340+
341+
342+
$dom = new DomDocument();
343+
$dom->loadXML('<root><child property="handle">text body</child><child property="ignore" /></root>');
344+
345+
$this->assertResultMatches($dom->documentElement, $target->asDomDocument()->documentElement);
346+
}
347+
348+
public function testSelectorGetsUsedWhenTransformationGetsApplied(): void {
349+
$target = Document::fromString('<root><child property="handle" /><child property="ignore" /></root>');
350+
351+
$target->applyTransformation(
352+
new class implements Transformation {
353+
public function selector() : Selector{
354+
return new XPathSelector('self::*');
355+
}
356+
public function apply(DOMNode $context) : void{
357+
$context->setAttribute('transformation', 'done');
358+
}
359+
},
360+
new XPathSelector('//child[@property="handle"]')
361+
);
362+
363+
364+
$dom = new DomDocument();
365+
$dom->loadXML('<root><child transformation="done" property="handle" /><child property="ignore" /></root>');
366+
367+
$this->assertResultMatches($dom->documentElement, $target->asDomDocument()->documentElement);
368+
}
329369

330370
}

tests/selectors/XPathSelectorTest.php

+17
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,23 @@ public function testSelectReturnsExceptedNode(): void {
2222
}
2323
}
2424

25+
public function testSelectReturnsExceptedNodeWhenDomDocuemtnIsUsed(): void {
26+
$dom = new DOMDocument();
27+
$dom->loadXML('<?xml version="1.0" ?><root><child /></root>');
28+
29+
$selector = new XPathSelector('//child');
30+
$selection = $selector->select($dom);
31+
32+
$this->assertInstanceOf(Selection::class, $selection);
33+
34+
foreach ($selection as $node) {
35+
$this->assertSame(
36+
$dom->documentElement->firstChild,
37+
$node
38+
);
39+
}
40+
}
41+
2542
public function testRegisteredNamespacePrefixIsUsed(): void {
2643
$dom = new DOMDocument();
2744
$dom->loadXML('<?xml version="1.0" ?><root xmlns="foo:ns"><child /></root>');

tests/viewmodel/ViewModelRendererTest.php

+78-34
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,12 @@
1010
use PHPUnit\Framework\TestCase;
1111
use Templado\Engine\Example\ViewModel;
1212
use Templado\Engine\PrefixModel\PrefixCallViewModel;
13+
use Templado\Engine\PrefixModel\PrefixPropertyGetViewModel;
14+
use Templado\Engine\PrefixModel\PrefixPropertyViewModel;
1315
use Templado\Engine\PrefixModel\PrefixViewModel;
1416
use Templado\Engine\ResourceModel\ResourceCallViewModel;
17+
use Templado\Engine\ResourceModel\ResourcePropertyGetViewModel;
18+
use Templado\Engine\ResourceModel\ResourcePropertyViewModel;
1519
use Templado\Engine\ResourceModel\ResourceViewModel;
1620

1721
#[CoversClass(ViewModelRenderer::class)]
@@ -431,8 +435,8 @@ public function asString() {
431435
);
432436
}
433437

434-
public function testResourceViewModelGetsAppliedAsExcepted(): void {
435-
$viewModel = new ResourceViewModel();
438+
#[DataProvider('resourceViewModelProvider')]
439+
public function testResourceViewModelGetsAppliedAsExcepted(object $viewModel): void {
436440
$dom = new DOMDocument();
437441
$dom->preserveWhiteSpace = false;
438442
$dom->load(__DIR__ . '/../_data/viewmodel/resource/source.html');
@@ -447,20 +451,13 @@ public function testResourceViewModelGetsAppliedAsExcepted(): void {
447451
$this->assertResultMatches($expected->documentElement, $dom->documentElement);
448452
}
449453

450-
public function testResourceViewModelWithMagicCallGetsAppliedAsExcepted(): void {
451-
$viewModel = new ResourceCallViewModel();
452-
$dom = new DOMDocument();
453-
$dom->preserveWhiteSpace = false;
454-
$dom->load(__DIR__ . '/../_data/viewmodel/resource/source.html');
455-
456-
$renderer = new ViewModelRenderer();
457-
$renderer->render($dom->documentElement, $viewModel);
458-
459-
$expected = new DOMDocument();
460-
$expected->preserveWhiteSpace = false;
461-
$expected->load(__DIR__ . '/../_data/viewmodel/resource/expected.html');
462-
463-
$this->assertResultMatches($expected->documentElement, $dom->documentElement);
454+
public static function resourceViewModelProvider(): array {
455+
return [
456+
'method' => [new ResourceViewModel()],
457+
'call' => [new ResourceCallViewModel()],
458+
'property' => [new ResourcePropertyViewModel()],
459+
'get' => [new ResourcePropertyGetViewModel()]
460+
];
464461
}
465462

466463
public function testUsingAResourceWithNoMethodToRequestItThrowsException(): void {
@@ -483,8 +480,9 @@ public function testResolvingResourceToNonObjectThrowsException(): void {
483480
$renderer->render($dom->documentElement, new class { public function foo(): string { return 'crash'; }});
484481
}
485482

486-
public function testPrefixViewModelGetsAppliedAsExcepted(): void {
487-
$viewModel = new PrefixViewModel();
483+
484+
#[DataProvider('prefixViewModelProvider')]
485+
public function testPrefixViewModelGetsAppliedAsExcepted(object $viewModel): void {
488486
$dom = new DOMDocument();
489487
$dom->preserveWhiteSpace = false;
490488
$dom->load(__DIR__ . '/../_data/viewmodel/prefix/source.html');
@@ -499,6 +497,15 @@ public function testPrefixViewModelGetsAppliedAsExcepted(): void {
499497
$this->assertResultMatches($expected->documentElement, $dom->documentElement);
500498
}
501499

500+
public static function prefixViewModelProvider(): array {
501+
return [
502+
'method' => [new PrefixViewModel()],
503+
'call' => [new PrefixCallViewModel()],
504+
'property' => [new PrefixPropertyViewModel()],
505+
'get' => [new PrefixPropertyGetViewModel()]
506+
];
507+
}
508+
502509
public function testPrefixWithDoubleColonViewModelGetsAppliedAsExcepted(): void {
503510
$viewModel = new PrefixViewModel();
504511
$dom = new DOMDocument();
@@ -515,22 +522,6 @@ public function testPrefixWithDoubleColonViewModelGetsAppliedAsExcepted(): void
515522
$this->assertResultMatches($expected->documentElement, $dom->documentElement);
516523
}
517524

518-
public function testPrefixViewModelWithMagicCallGetsAppliedAsExcepted(): void {
519-
$viewModel = new PrefixCallViewModel();
520-
$dom = new DOMDocument();
521-
$dom->preserveWhiteSpace = false;
522-
$dom->load(__DIR__ . '/../_data/viewmodel/prefix/source.html');
523-
524-
$renderer = new ViewModelRenderer();
525-
$renderer->render($dom->documentElement, $viewModel);
526-
527-
$expected = new DOMDocument();
528-
$expected->preserveWhiteSpace = false;
529-
$expected->load(__DIR__ . '/../_data/viewmodel/prefix/expected.html');
530-
531-
$this->assertResultMatches($expected->documentElement, $dom->documentElement);
532-
}
533-
534525
public function testUsingAPrefixWithNoMethodToRequestItThrowsException(): void {
535526
$dom = new DOMDocument();
536527
$dom->loadXML('<?xml version="1.0"?><root prefix="p: foo" />');
@@ -1049,4 +1040,57 @@ public function document(): Document {
10491040

10501041
$this->assertResultMatches($expected->documentElement, $dom->documentElement);
10511042
}
1043+
1044+
#[DataProvider('attributeViewModelProvider')]
1045+
public function testAttributesGetAppliedFromViewModelProperties(object $model, string $value): void {
1046+
$dom = new DOMDocument();
1047+
$dom->loadXML('<root property="document" attr="default" />');
1048+
1049+
$renderer = new ViewModelRenderer();
1050+
$renderer->render(
1051+
$dom->documentElement,
1052+
$model
1053+
);
1054+
1055+
$expected = new DOMDocument();
1056+
$expected->loadXML('<root property="document" attr="'.$value.'" />');
1057+
1058+
$this->assertResultMatches($expected->documentElement, $dom->documentElement);
1059+
}
1060+
1061+
public static function attributeViewModelProvider(): array {
1062+
return [
1063+
'property' => [
1064+
new class {
1065+
public function document(): object {
1066+
return new class {
1067+
public string $attr='changed';
1068+
};
1069+
}
1070+
},
1071+
'changed'
1072+
],
1073+
'get' => [
1074+
new class {
1075+
public function document(): object {
1076+
return new class {
1077+
public function __get(string $key): string|true {
1078+
return $key === 'attr' ? 'changed' : true;
1079+
}
1080+
};
1081+
}
1082+
},
1083+
'changed'
1084+
],
1085+
'none' => [
1086+
new class {
1087+
public function document(): object {
1088+
return new class {
1089+
};
1090+
}
1091+
},
1092+
'default'
1093+
]
1094+
];
1095+
}
10521096
}

0 commit comments

Comments
 (0)