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

Improve PHP 8 compatibility and testing #421

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
8 changes: 5 additions & 3 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,17 @@ jobs:
fail-fast: false
matrix:
php:
- 5.3
- 5.4
- 5.5
- 5.6
- 7.0
- 7.1
- 7.2
- 7.3
- 7.4
- 8.0
- 8.1
- 8.2
- 8.3
- 8.4

name: PHP ${{ matrix.php }}

Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
composer.lock
mustache.php
vendor
.phpunit.result.cache
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
}
],
"require": {
"php": ">=5.2.4"
"php": ">=5.6"
},
"require-dev": {
"phpunit/phpunit": "~3.7|~4.0|~5.0",
"friendsofphp/php-cs-fixer": "~1.11"
"friendsofphp/php-cs-fixer": "~2.19.3",
"yoast/phpunit-polyfills": "^2.0"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My biggest question here is whether we need to keep such old PHP version support, a such, such old PHPUnit version support... and therefore whether we actually need the polyfills at all

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For consuming projects like WP-CLI, PHP 5.6 is still required this year. After that, 7.0 or 7.2.

But even then, there is no single PHPUnit version that covers all PHP versions and is fully supported, see https://phpunit.de/supported-versions.html.

Plus, the polyfills provide forward compatibility, so that you can always use the latest PHPUnit features, even if you still need to use PHPUnit 8 for older PHP versions. Thus, this package is still very useful even if the project requires more modern PHP versions.

},
"autoload": {
"psr-0": { "Mustache": "src/" }
Expand Down
18 changes: 10 additions & 8 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false" colors="true" bootstrap="./test/bootstrap.php">
<testsuite name="Mustache">
<directory suffix="Test.php">./test</directory>
<exclude>./test/Mustache/Test/FiveThree</exclude>
</testsuite>
<testsuites>
<testsuite name="Mustache">
<directory suffix="Test.php">./test</directory>
<exclude>./test/Mustache/Test/FiveThree</exclude>
</testsuite>

<testsuite name="Mustache FiveThree">
<directory suffix="Test.php" phpVersion="5.3.0" phpVersionOperator=">=">./test/Mustache/Test/FiveThree</directory>
</testsuite>
<testsuite name="Mustache FiveThree">
<directory suffix="Test.php" phpVersion="5.3.0" phpVersionOperator=">=">./test/Mustache/Test/FiveThree</directory>
</testsuite>
</testsuites>

<filter>
<whitelist>
<directory suffix=".php">./src/Mustache</directory>
</whitelist>
</filter>
</phpunit>
</phpunit>
4 changes: 2 additions & 2 deletions src/Mustache/Engine.php
Original file line number Diff line number Diff line change
Expand Up @@ -741,12 +741,12 @@ public function loadLambda($source, $delims = null)
*
* @return Mustache_Template
*/
private function loadSource($source, Mustache_Cache $cache = null)
private function loadSource($source, $cache = null)
{
$className = $this->getTemplateClassName($source);

if (!isset($this->templates[$className])) {
if ($cache === null) {
if ($cache === null || ! $cache instanceof Mustache_Cache) {
$cache = $this->getCache();
}

Expand Down
2 changes: 1 addition & 1 deletion src/Mustache/Exception/SyntaxException.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Mustache_Exception_SyntaxException extends LogicException implements Musta
* @param array $token
* @param Exception $previous
*/
public function __construct($msg, array $token, Exception $previous = null)
public function __construct($msg, array $token, $previous = null)
{
$this->token = $token;
if (version_compare(PHP_VERSION, '5.3.0', '>=')) {
Expand Down
2 changes: 1 addition & 1 deletion src/Mustache/Exception/UnknownFilterException.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Mustache_Exception_UnknownFilterException extends UnexpectedValueException
* @param string $filterName
* @param Exception $previous
*/
public function __construct($filterName, Exception $previous = null)
public function __construct($filterName, $previous = null)
{
$this->filterName = $filterName;
$message = sprintf('Unknown filter: %s', $filterName);
Expand Down
2 changes: 1 addition & 1 deletion src/Mustache/Exception/UnknownHelperException.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Mustache_Exception_UnknownHelperException extends InvalidArgumentException
* @param string $helperName
* @param Exception $previous
*/
public function __construct($helperName, Exception $previous = null)
public function __construct($helperName, $previous = null)
{
$this->helperName = $helperName;
$message = sprintf('Unknown helper: %s', $helperName);
Expand Down
2 changes: 1 addition & 1 deletion src/Mustache/Exception/UnknownTemplateException.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Mustache_Exception_UnknownTemplateException extends InvalidArgumentExcepti
* @param string $templateName
* @param Exception $previous
*/
public function __construct($templateName, Exception $previous = null)
public function __construct($templateName, $previous = null)
{
$this->templateName = $templateName;
$message = sprintf('Unknown template: %s', $templateName);
Expand Down
2 changes: 1 addition & 1 deletion src/Mustache/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public function setPragmas(array $pragmas)
*
* @return array Mustache Token parse tree
*/
private function buildTree(array &$tokens, array $parent = null)
private function buildTree(array &$tokens, $parent = null)
{
$nodes = array();

Expand Down
2 changes: 1 addition & 1 deletion test/Mustache/Test/AutoloaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
/**
* @group unit
*/
class Mustache_Test_AutoloaderTest extends PHPUnit_Framework_TestCase
class Mustache_Test_AutoloaderTest extends Yoast\PHPUnitPolyfills\TestCases\TestCase
{
public function testRegister()
{
Expand Down
6 changes: 2 additions & 4 deletions test/Mustache/Test/Cache/AbstractCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* file that was distributed with this source code.
*/

class Mustache_Test_Cache_AbstractCacheTest extends PHPUnit_Framework_TestCase
class Mustache_Test_Cache_AbstractCacheTest extends Yoast\PHPUnitPolyfills\TestCases\TestCase
{
public function testGetSetLogger()
{
Expand All @@ -19,11 +19,9 @@ public function testGetSetLogger()
$this->assertSame($logger, $cache->getLogger());
}

/**
* @expectedException Mustache_Exception_InvalidArgumentException
*/
public function testSetLoggerThrowsExceptions()
{
$this->expectException(Mustache_Exception_InvalidArgumentException::class);
$cache = new CacheStub();
$logger = new StdClass();
$cache->setLogger($logger);
Expand Down
8 changes: 3 additions & 5 deletions test/Mustache/Test/CompilerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
/**
* @group unit
*/
class Mustache_Test_CompilerTest extends PHPUnit_Framework_TestCase
class Mustache_Test_CompilerTest extends Yoast\PHPUnitPolyfills\TestCases\TestCase
{
/**
* @dataProvider getCompileValues
Expand All @@ -23,7 +23,7 @@ public function testCompile($source, array $tree, $name, $customEscaper, $entity

$compiled = $compiler->compile($source, $tree, $name, $customEscaper, $charset, false, $entityFlags);
foreach ($expected as $contains) {
$this->assertContains($contains, $compiled);
$this->assertStringContainsString($contains, $compiled);
}
}

Expand Down Expand Up @@ -132,11 +132,9 @@ public function getCompileValues()
);
}

/**
* @expectedException Mustache_Exception_SyntaxException
*/
public function testCompilerThrowsSyntaxException()
{
$this->expectException(Mustache_Exception_SyntaxException::class);
$compiler = new Mustache_Compiler();
$compiler->compile('', array(array(Mustache_Tokenizer::TYPE => 'invalid')), 'SomeClass');
}
Expand Down
14 changes: 10 additions & 4 deletions test/Mustache/Test/ContextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
/**
* @group unit
*/
class Mustache_Test_ContextTest extends PHPUnit_Framework_TestCase
class Mustache_Test_ContextTest extends Yoast\PHPUnitPolyfills\TestCases\TestCase
{
public function testConstructor()
{
Expand Down Expand Up @@ -171,11 +171,9 @@ public function testAnchoredDotNotation()
$this->assertEquals('', $context->findAnchoredDot('.child.name'));
}

/**
* @expectedException Mustache_Exception_InvalidArgumentException
*/
public function testAnchoredDotNotationThrowsExceptions()
{
$this->expectException(Mustache_Exception_InvalidArgumentException::class);
$context = new Mustache_Context();
$context->push(array('a' => 1));
$context->findAnchoredDot('a');
Expand Down Expand Up @@ -264,6 +262,7 @@ public function __construct($array)
}
}

#[\ReturnTypeWillChange]
public function offsetSet($offset, $value)
{
if (is_null($offset)) {
Expand All @@ -273,16 +272,19 @@ public function offsetSet($offset, $value)
}
}

#[\ReturnTypeWillChange]
public function offsetExists($offset)
{
return isset($this->container[$offset]);
}

#[\ReturnTypeWillChange]
public function offsetUnset($offset)
{
unset($this->container[$offset]);
}

#[\ReturnTypeWillChange]
public function offsetGet($offset)
{
return isset($this->container[$offset]) ? $this->container[$offset] : null;
Expand All @@ -300,11 +302,13 @@ public function foo()
return 'win';
}

#[\ReturnTypeWillChange]
public function offsetExists($offset)
{
return true;
}

#[\ReturnTypeWillChange]
public function offsetGet($offset)
{
switch ($offset) {
Expand All @@ -321,11 +325,13 @@ public function offsetGet($offset)
}
}

#[\ReturnTypeWillChange]
public function offsetSet($offset, $value)
{
// nada
}

#[\ReturnTypeWillChange]
public function offsetUnset($offset)
{
// nada
Expand Down
32 changes: 11 additions & 21 deletions test/Mustache/Test/EngineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function testConstructor()
$this->assertSame($loader, $mustache->getLoader());
$this->assertSame($partialsLoader, $mustache->getPartialsLoader());
$this->assertEquals('{{ foo }}', $partialsLoader->load('foo'));
$this->assertContains('__whot__', $mustache->getTemplateClassName('{{ foo }}'));
$this->assertStringContainsString('__whot__', $mustache->getTemplateClassName('{{ foo }}'));
$this->assertEquals('strtoupper', $mustache->getEscape());
$this->assertEquals(ENT_QUOTES, $mustache->getEntityFlags());
$this->assertEquals('ISO-8859-1', $mustache->getCharset());
Expand Down Expand Up @@ -158,22 +158,20 @@ public function testWithoutLambdaCache()
$this->assertNotSame($mustache->getCache(), $mustache->getProtectedLambdaCache());
}

/**
* @expectedException Mustache_Exception_InvalidArgumentException
*/
public function testEmptyTemplatePrefixThrowsException()
{
$this->expectException(Mustache_Exception_InvalidArgumentException::class);
new Mustache_Engine(array(
'template_class_prefix' => '',
));
}

/**
* @expectedException Mustache_Exception_InvalidArgumentException
* @dataProvider getBadEscapers
*/
public function testNonCallableEscapeThrowsException($escape)
{
$this->expectException(Mustache_Exception_InvalidArgumentException::class);
new Mustache_Engine(array('escape' => $escape));
}

Expand All @@ -185,11 +183,9 @@ public function getBadEscapers()
);
}

/**
* @expectedException Mustache_Exception_RuntimeException
*/
public function testImmutablePartialsLoadersThrowException()
{
$this->expectException(Mustache_Exception_RuntimeException::class);
$mustache = new Mustache_Engine(array(
'partials_loader' => new Mustache_Loader_StringLoader(),
));
Expand Down Expand Up @@ -250,20 +246,16 @@ public static function wrapWithUnderscores($text)
return '__' . $text . '__';
}

/**
* @expectedException Mustache_Exception_InvalidArgumentException
*/
public function testSetHelpersThrowsExceptions()
{
$this->expectException(Mustache_Exception_InvalidArgumentException::class);
$mustache = new Mustache_Engine();
$mustache->setHelpers('monkeymonkeymonkey');
}

/**
* @expectedException Mustache_Exception_InvalidArgumentException
*/
public function testSetLoggerThrowsExceptions()
{
$this->expectException(Mustache_Exception_InvalidArgumentException::class);
$mustache = new Mustache_Engine();
$mustache->setLogger(new StdClass());
}
Expand Down Expand Up @@ -305,14 +297,14 @@ public function testPartialLoadFailLogging()
$result = $mustache->render('{{> foo }}{{> bar }}{{> baz }}', array());
$this->assertEquals('FOOBAR', $result);

$this->assertContains('WARNING: Partial not found: "baz"', file_get_contents($name));
$this->assertStringContainsString('WARNING: Partial not found: "baz"', file_get_contents($name));
}

public function testCacheWarningLogging()
{
list($name, $mustache) = $this->getLoggedMustache(Mustache_Logger::WARNING);
$mustache->render('{{ foo }}', array('foo' => 'FOO'));
$this->assertContains('WARNING: Template cache disabled, evaluating', file_get_contents($name));
$this->assertStringContainsString('WARNING: Template cache disabled, evaluating', file_get_contents($name));
}

public function testLoggingIsNotTooAnnoying()
Expand All @@ -327,15 +319,13 @@ public function testVerboseLoggingIsVerbose()
list($name, $mustache) = $this->getLoggedMustache(Mustache_Logger::DEBUG);
$mustache->render('{{ foo }}{{> bar }}', array('foo' => 'FOO'));
$log = file_get_contents($name);
$this->assertContains('DEBUG: Instantiating template: ', $log);
$this->assertContains('WARNING: Partial not found: "bar"', $log);
$this->assertStringContainsString('DEBUG: Instantiating template: ', $log);
$this->assertStringContainsString('WARNING: Partial not found: "bar"', $log);
}

/**
* @expectedException Mustache_Exception_InvalidArgumentException
*/
public function testUnknownPragmaThrowsException()
{
$this->expectException(Mustache_Exception_InvalidArgumentException::class);
new Mustache_Engine(array(
'pragmas' => array('UNKNOWN'),
));
Expand Down
2 changes: 1 addition & 1 deletion test/Mustache/Test/Exception/SyntaxExceptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* file that was distributed with this source code.
*/

class Mustache_Test_Exception_SyntaxExceptionTest extends PHPUnit_Framework_TestCase
class Mustache_Test_Exception_SyntaxExceptionTest extends Yoast\PHPUnitPolyfills\TestCases\TestCase
{
public function testInstance()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* file that was distributed with this source code.
*/

class Mustache_Test_Exception_UnknownFilterExceptionTest extends PHPUnit_Framework_TestCase
class Mustache_Test_Exception_UnknownFilterExceptionTest extends Yoast\PHPUnitPolyfills\TestCases\TestCase
{
public function testInstance()
{
Expand Down
Loading