Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FEAT Phpunit - update i18n suite for windows / phpunit 10 compatibility #11548

Open
wants to merge 3 commits into
base: 5
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions tests/php/i18n/YamlReaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ public function testThrowsMeaningfulExceptionWhenYmlIsCorrupted()
{
$path = __DIR__ . '/i18nTest/_fakewebroot/i18ntestmodule/lang/en_corrupt.yml';
$this->expectException(InvalidResourceException::class);
$regex_path = str_replace('.', '\.', $path ?? '');
$this->expectExceptionMessageMatches('@^Error parsing YAML, invalid file "' . $regex_path . '"\. Message: ([\w ].*) line 5 @');
$this->expectExceptionMessageMatches('@^Error parsing YAML, invalid file \".*en_corrupt\.yml\"\. Message: ([\w ].*) line 5 @');
$reader = new YamlReader();
$reader->read('en', $path);
}
Expand Down
112 changes: 67 additions & 45 deletions tests/php/i18n/i18nTextCollectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,25 @@ protected function tearDown(): void
parent::tearDown();
}

/**
* This helper function allows to expect error messages in a PHPUnit 10 compatible way
* @param string $message
* @param callable $callback
* @param mixed ...$args
* @return void
*/
protected function assertThrowableMessage(
string $message,
callable $callback,
...$args
): void {
try {
$callback(...$args);
} catch (\Throwable $e) {
$this->assertEquals($message, $e->getMessage());
}
}

public function testConcatenationInEntityValues()
{
$c = i18nTextCollector::create();
Expand Down Expand Up @@ -115,10 +134,9 @@ public function testCollectFromNewTemplateSyntaxUsingParserSubclass()

// Test warning is raised on empty default
$c->setWarnOnEmptyDefault(true);
$this->expectNotice();
$this->expectNoticeMessage('Missing localisation default for key i18nTestModule.INJECTIONS_3');

$c->collectFromTemplate($html, null, $mymodule);
$this->assertThrowableMessage('Missing localisation default for key i18nTestModule.INJECTIONS_3', function () use ($c, $html, $mymodule) {
$c->collectFromTemplate($html, null, $mymodule);
});
}

public function testCollectFromTemplateSimple()
Expand All @@ -130,23 +148,23 @@ public function testCollectFromTemplateSimple()
<%t Test.SINGLEQUOTE 'Single Quote' %>
SS;
$this->assertEquals(
[ 'Test.SINGLEQUOTE' => 'Single Quote' ],
['Test.SINGLEQUOTE' => 'Single Quote'],
$c->collectFromTemplate($html, null, $mymodule)
);

$html = <<<SS
<%t Test.DOUBLEQUOTE "Double Quote and Spaces" %>
SS;
$this->assertEquals(
[ 'Test.DOUBLEQUOTE' => "Double Quote and Spaces" ],
['Test.DOUBLEQUOTE' => "Double Quote and Spaces"],
$c->collectFromTemplate($html, null, $mymodule)
);

$html = <<<SS
<%t Test.NOSEMICOLON "No Semicolon" %>
SS;
$this->assertEquals(
[ 'Test.NOSEMICOLON' => "No Semicolon" ],
['Test.NOSEMICOLON' => "No Semicolon"],
$c->collectFromTemplate($html, null, $mymodule)
);
}
Expand All @@ -161,7 +179,7 @@ public function testCollectFromTemplateAdvanced()
<%t Test.PRIOANDCOMMENT ' Prio and Value with "Double Quotes"' is 'Comment with "Double Quotes"' %>
SS;
$this->assertEquals(
[ 'Test.PRIOANDCOMMENT' => [
['Test.PRIOANDCOMMENT' => [
'default' => ' Prio and Value with "Double Quotes"',
'comment' => 'Comment with "Double Quotes"',
]],
Expand All @@ -172,7 +190,7 @@ public function testCollectFromTemplateAdvanced()
<%t Test.PRIOANDCOMMENT " Prio and Value with 'Single Quotes'" is "Comment with 'Single Quotes'" %>
SS;
$this->assertEquals(
[ 'Test.PRIOANDCOMMENT' => [
['Test.PRIOANDCOMMENT' => [
'default' => " Prio and Value with 'Single Quotes'",
'comment' => "Comment with 'Single Quotes'",
]],
Expand All @@ -190,10 +208,9 @@ public function testCollectFromTemplateAdvanced()

// Test warning is raised on empty default
$c->setWarnOnEmptyDefault(true);
$this->expectNotice();
$this->expectNoticeMessage('Missing localisation default for key Test.PRIOANDCOMMENT');

$c->collectFromTemplate($html, 'Test', $mymodule);
$this->assertThrowableMessage('Missing localisation default for key Test.PRIOANDCOMMENT', function () use ($c, $html, $mymodule) {
$c->collectFromTemplate($html, 'Test', $mymodule);
});
}


Expand All @@ -206,15 +223,15 @@ public function testCollectFromCodeSimple()
_t('Test.SINGLEQUOTE','Single Quote');
PHP;
$this->assertEquals(
[ 'Test.SINGLEQUOTE' => 'Single Quote' ],
['Test.SINGLEQUOTE' => 'Single Quote'],
$c->collectFromCode($php, null, $mymodule)
);

$php = <<<PHP
_t( "Test.DOUBLEQUOTE", "Double Quote and Spaces" );
PHP;
$this->assertEquals(
[ 'Test.DOUBLEQUOTE' => "Double Quote and Spaces" ],
['Test.DOUBLEQUOTE' => "Double Quote and Spaces"],
$c->collectFromCode($php, null, $mymodule)
);
}
Expand All @@ -231,7 +248,7 @@ public function testCollectFromCodeAdvanced()
);
PHP;
$this->assertEquals(
[ 'Test.NEWLINES' => "New Lines" ],
['Test.NEWLINES' => "New Lines"],
$c->collectFromCode($php, null, $mymodule)
);

Expand Down Expand Up @@ -262,10 +279,10 @@ public function testCollectFromCodeAdvanced()
);
PHP;
$this->assertEquals(
[ 'Test.PRIOANDCOMMENT' => [
['Test.PRIOANDCOMMENT' => [
'default' => " Value with 'Single Quotes'",
'comment' => "Comment with 'Single Quotes'"
] ],
]],
$c->collectFromCode($php, null, $mymodule)
);

Expand All @@ -276,7 +293,7 @@ public function testCollectFromCodeAdvanced()
);
PHP;
$this->assertEquals(
[ 'Test.PRIOANDCOMMENT' => "Value with 'Escaped Single Quotes'" ],
['Test.PRIOANDCOMMENT' => "Value with 'Escaped Single Quotes'"],
$c->collectFromCode($php, null, $mymodule)
);

Expand All @@ -289,7 +306,7 @@ public function testCollectFromCodeAdvanced()
);
PHP;
$this->assertEquals(
[ 'Test.PRIOANDCOMMENT' => "Doublequoted Value with 'Unescaped Single Quotes'"],
['Test.PRIOANDCOMMENT' => "Doublequoted Value with 'Unescaped Single Quotes'"],
$c->collectFromCode($php, null, $mymodule)
);
}
Expand Down Expand Up @@ -456,8 +473,9 @@ public function testNewlinesInEntityValues()
PHP;

$eol = PHP_EOL;
$php = str_replace("\n", PHP_EOL, $php);
$this->assertEquals(
[ 'Test.NEWLINESINGLEQUOTE' => "Line 1{$eol}Line 2" ],
['Test.NEWLINESINGLEQUOTE' => "Line 1{$eol}Line 2"],
$c->collectFromCode($php, null, $mymodule)
);

Expand All @@ -468,8 +486,9 @@ public function testNewlinesInEntityValues()
Line 2"
);
PHP;
$php = str_replace("\n", PHP_EOL, $php);
$this->assertEquals(
[ 'Test.NEWLINEDOUBLEQUOTE' => "Line 1{$eol}Line 2" ],
['Test.NEWLINEDOUBLEQUOTE' => "Line 1{$eol}Line 2"],
$c->collectFromCode($php, null, $mymodule)
);
}
Expand Down Expand Up @@ -523,14 +542,13 @@ public function testCollectFromCodeNewSignature()
$this->assertEquals($expectedArray, $collectedTranslatables);

// Test warning is raised on empty default
$this->expectNotice();
$this->expectNoticeMessage('Missing localisation default for key i18nTestModule.INJECTIONS4');

$php = <<<PHP
_t('i18nTestModule.INJECTIONS4', ["name"=>"Cat", "greeting"=>"meow", "goodbye"=>"meow"]);
$this->assertThrowableMessage('Missing localisation default for key i18nTestModule.INJECTIONS4', function () use ($c, $mymodule) {
$php = <<<PHP
_t('i18nTestModule.INJECTIONS4', ["name"=>"Cat", "greeting"=>"meow", "goodbye"=>"meow"]);
PHP;
$c->setWarnOnEmptyDefault(true);
$c->collectFromCode($php, null, $mymodule);
$c->setWarnOnEmptyDefault(true);
$c->collectFromCode($php, null, $mymodule);
});
}

public function testUncollectableCode()
Expand All @@ -547,7 +565,7 @@ public function testUncollectableCode()
$collectedTranslatables = $c->collectFromCode($php, null, $mymodule);

// Only one item is collectable
$expectedArray = [ 'Collectable.KEY4' => 'Default' ];
$expectedArray = ['Collectable.KEY4' => 'Default'];
$this->assertEquals($expectedArray, $collectedTranslatables);
}

Expand Down Expand Up @@ -945,34 +963,38 @@ public function testModuleFileList()

// Non-standard modules can't be safely filtered, so just index everything
$nonStandardFiles = $collector->getFileListForModule_Test('i18nnonstandardmodule');
$nonStandardRoot = $this->alternateBasePath . '/i18nnonstandardmodule';
$nonStandardRoot = $this->alternateBasePath . DIRECTORY_SEPARATOR . 'i18nnonstandardmodule';
$this->assertEquals(3, count($nonStandardFiles ?? []));
$this->assertArrayHasKey("{$nonStandardRoot}/_config.php", $nonStandardFiles);
$this->assertArrayHasKey("{$nonStandardRoot}/_config.php", $nonStandardFiles, "Files are :" . json_encode($nonStandardFiles));
$this->assertArrayHasKey("{$nonStandardRoot}/phpfile.php", $nonStandardFiles);
$this->assertArrayHasKey("{$nonStandardRoot}/template.ss", $nonStandardFiles);

// Normal module should have predictable dir structure
$testFiles = $collector->getFileListForModule_Test('i18ntestmodule');
$testRoot = $this->alternateBasePath . '/i18ntestmodule';
$testRoot = $this->alternateBasePath . DIRECTORY_SEPARATOR . 'i18ntestmodule';
$codeRoot = $testRoot . DIRECTORY_SEPARATOR . 'code';
$templatesRoot = $testRoot . DIRECTORY_SEPARATOR . 'templates';
$this->assertEquals(7, count($testFiles ?? []));
// Code in code folder is detected
$this->assertArrayHasKey("{$testRoot}/code/i18nTestModule.php", $testFiles);
$this->assertArrayHasKey("{$testRoot}/code/subfolder/_config.php", $testFiles);
$this->assertArrayHasKey("{$testRoot}/code/subfolder/i18nTestSubModule.php", $testFiles);
$this->assertArrayHasKey("{$testRoot}/code/subfolder/i18nTestNamespacedClass.php", $testFiles);
$this->assertArrayHasKey("{$codeRoot}/i18nTestModule.php", $testFiles, "Files are :" . json_encode($testFiles));
$this->assertArrayHasKey("{$codeRoot}/subfolder/_config.php", $testFiles);
$this->assertArrayHasKey("{$codeRoot}/subfolder/i18nTestSubModule.php", $testFiles);
$this->assertArrayHasKey("{$codeRoot}/subfolder/i18nTestNamespacedClass.php", $testFiles);
// Templates in templates folder is detected
$this->assertArrayHasKey("{$testRoot}/templates/Includes/i18nTestModuleInclude.ss", $testFiles);
$this->assertArrayHasKey("{$testRoot}/templates/Layout/i18nTestModule.ss", $testFiles);
$this->assertArrayHasKey("{$testRoot}/templates/i18nTestModule.ss", $testFiles);
$this->assertArrayHasKey("{$templatesRoot}/Includes/i18nTestModuleInclude.ss", $testFiles);
$this->assertArrayHasKey("{$templatesRoot}/Layout/i18nTestModule.ss", $testFiles);
$this->assertArrayHasKey("{$templatesRoot}/i18nTestModule.ss", $testFiles);

// Standard modules with code in odd places should only have code in those directories detected
$otherFiles = $collector->getFileListForModule_Test('i18nothermodule');
$otherRoot = $this->alternateBasePath . '/i18nothermodule';
$otherRoot = $this->alternateBasePath . DIRECTORY_SEPARATOR . 'i18nothermodule';
$codeRoot = $otherRoot . DIRECTORY_SEPARATOR . 'code';
$templatesRoot = $otherRoot . DIRECTORY_SEPARATOR . 'templates';
$this->assertEquals(4, count($otherFiles ?? []));
// Only detect well-behaved files
$this->assertArrayHasKey("{$otherRoot}/code/i18nOtherModule.php", $otherFiles);
$this->assertArrayHasKey("{$otherRoot}/code/i18nProviderClass.php", $otherFiles);
$this->assertArrayHasKey("{$otherRoot}/code/i18nTestModuleDecorator.php", $otherFiles);
$this->assertArrayHasKey("{$otherRoot}/templates/i18nOtherModule.ss", $otherFiles);
$this->assertArrayHasKey("{$codeRoot}/i18nOtherModule.php", $otherFiles, "Files are :" . json_encode($otherFiles));
$this->assertArrayHasKey("{$codeRoot}/i18nProviderClass.php", $otherFiles);
$this->assertArrayHasKey("{$codeRoot}/i18nTestModuleDecorator.php", $otherFiles);
$this->assertArrayHasKey("{$templatesRoot}/i18nOtherModule.ss", $otherFiles);
}
}
Loading