Skip to content

Commit 32b796d

Browse files
committed
improve tests with variables count check to ensure number of occurrences replaced
1 parent aa13b54 commit 32b796d

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

tests/PhpWordTests/TemplateProcessorTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -635,20 +635,32 @@ public function testSetValues(): void
635635
// test with a specific limit that is lower than the number of replacements
636636
$templateProcessor = new TestableTemplateProcesor($mainPart);
637637
$templateProcessor->setValues(['firstname' => 'Jane', 'lastname' => 'Smith'], 2);
638+
$variablesCounts = $templateProcessor->getVariableCount();
639+
638640
self::assertStringContainsString('Hello Jane Smith', $templateProcessor->getMainPart());
639641
self::assertStringContainsString('Hello ${firstname} ${lastname}', $templateProcessor->getMainPart());
642+
self::assertEquals(1, $variablesCounts['firstname']);
643+
self::assertEquals(1, $variablesCounts['lastname']);
640644

641645
// test with a limit for only one replacement
642646
$templateProcessor = new TestableTemplateProcesor($mainPart);
643647
$templateProcessor->setValues(['firstname' => 'Alice', 'lastname' => 'Wonderland'], 1);
648+
$variablesCounts = $templateProcessor->getVariableCount();
649+
644650
self::assertStringContainsString('Hello Alice Wonderland', $templateProcessor->getMainPart());
645651
self::assertStringContainsString('Hello ${firstname} ${lastname}', $templateProcessor->getMainPart());
652+
self::assertEquals(2, $variablesCounts['firstname']);
653+
self::assertEquals(2, $variablesCounts['lastname']);
646654

647655
// Test with a limit of 0 for a result with no replacements
648656
$templateProcessor = new TestableTemplateProcesor($mainPart);
649657
$templateProcessor->setValues(['firstname' => 'Test', 'lastname' => 'User'], 0);
658+
$variablesCounts = $templateProcessor->getVariableCount();
659+
650660
self::assertStringContainsString('Hello ${firstname} ${lastname}', $templateProcessor->getMainPart());
651661
self::assertStringNotContainsString('Hello Test User', $templateProcessor->getMainPart());
662+
self::assertEquals(3, $variablesCounts['firstname']);
663+
self::assertEquals(3, $variablesCounts['lastname']);
652664
}
653665

654666
/**

0 commit comments

Comments
 (0)