diff --git a/.php_cs.dist b/.php_cs.dist index 1b31c0a3d..b81882fbf 100644 --- a/.php_cs.dist +++ b/.php_cs.dist @@ -4,6 +4,8 @@ return PhpCsFixer\Config::create() ->setRules([ '@Symfony' => true, '@Symfony:risky' => true, + '@PHPUnit75Migration:risky' => true, + 'php_unit_dedicate_assert' => ['target' => '5.6'], 'array_syntax' => ['syntax' => 'short'], 'php_unit_fqcn_annotation' => true, 'no_unreachable_default_argument_value' => false, diff --git a/.travis.yml b/.travis.yml index 7c43b1c27..659f9a806 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,6 +5,10 @@ cache: - vendor - $HOME/.composer/cache/files +env: + global: + - SYMFONY_PHPUNIT_REMOVE_RETURN_TYPEHINT=1 + before_install: - phpenv config-rm xdebug.ini || return 0 diff --git a/composer.json b/composer.json index 672177dc3..83f162736 100644 --- a/composer.json +++ b/composer.json @@ -29,7 +29,7 @@ "symfony/polyfill-ctype": "^1.8" }, "require-dev": { - "symfony/phpunit-bridge": "^3.4.19|^4.1.8|^5.0", + "symfony/phpunit-bridge": "^4.4@dev|^5.0", "symfony/debug": "^3.4|^4.2", "symfony/mime": "^4.3", "psr/container": "^1.0" diff --git a/tests/Cache/FilesystemTest.php b/tests/Cache/FilesystemTest.php index d3b895946..743561ca0 100644 --- a/tests/Cache/FilesystemTest.php +++ b/tests/Cache/FilesystemTest.php @@ -42,7 +42,7 @@ class FilesystemTest extends TestCase $dir = \dirname($key); @mkdir($dir, 0777, true); - $this->assertTrue(is_dir($dir)); + $this->assertDirectoryExists($dir); $this->assertFalse(class_exists($this->classname, false)); $content = $this->generateSource(); @@ -79,12 +79,11 @@ class FilesystemTest extends TestCase $this->assertSame(file_get_contents($key), $content); } - /** - * @expectedException \RuntimeException - * @expectedExceptionMessage Unable to create the cache directory - */ public function testWriteFailMkdir() { + $this->expectException('\RuntimeException'); + $this->expectExceptionMessage('Unable to create the cache directory'); + if (\defined('PHP_WINDOWS_VERSION_BUILD')) { $this->markTestSkipped('Read-only directories not possible on Windows.'); } @@ -96,17 +95,16 @@ class FilesystemTest extends TestCase // Create read-only root directory. @mkdir($this->directory, 0555, true); - $this->assertTrue(is_dir($this->directory)); + $this->assertDirectoryExists($this->directory); $this->cache->write($key, $content); } - /** - * @expectedException \RuntimeException - * @expectedExceptionMessage Unable to write in the cache directory - */ public function testWriteFailDirWritable() { + $this->expectException('\RuntimeException'); + $this->expectExceptionMessage('Unable to write in the cache directory'); + if (\defined('PHP_WINDOWS_VERSION_BUILD')) { $this->markTestSkipped('Read-only directories not possible on Windows.'); } @@ -120,17 +118,16 @@ class FilesystemTest extends TestCase @mkdir($this->directory, 0777, true); // Create read-only subdirectory. @mkdir($this->directory.'/cache', 0555); - $this->assertTrue(is_dir($this->directory.'/cache')); + $this->assertDirectoryExists($this->directory.'/cache'); $this->cache->write($key, $content); } - /** - * @expectedException \RuntimeException - * @expectedExceptionMessage Failed to write cache file - */ public function testWriteFailWriteFile() { + $this->expectException('\RuntimeException'); + $this->expectExceptionMessage('Failed to write cache file'); + $key = $this->directory.'/cache/cachefile.php'; $content = $this->generateSource(); @@ -138,7 +135,7 @@ class FilesystemTest extends TestCase // Create a directory in the place of the cache file. @mkdir($key, 0777, true); - $this->assertTrue(is_dir($key)); + $this->assertDirectoryExists($key); $this->cache->write($key, $content); } @@ -149,7 +146,7 @@ class FilesystemTest extends TestCase $dir = \dirname($key); @mkdir($dir, 0777, true); - $this->assertTrue(is_dir($dir)); + $this->assertDirectoryExists($dir); // Create the file with a specific modification time. touch($key, 1234567890); diff --git a/tests/CompilerTest.php b/tests/CompilerTest.php index ba230795d..31c462a37 100644 --- a/tests/CompilerTest.php +++ b/tests/CompilerTest.php @@ -33,7 +33,7 @@ class CompilerTest extends TestCase } $this->assertEquals('1.2', $compiler->repr(1.2)->getSource()); - $this->assertContains('fr', strtolower(setlocale(LC_NUMERIC, 0))); + $this->assertStringContainsString('fr', strtolower(setlocale(LC_NUMERIC, 0))); setlocale(LC_NUMERIC, $locale); } diff --git a/tests/CustomExtensionTest.php b/tests/CustomExtensionTest.php index b43e490eb..5de8e9e9e 100644 --- a/tests/CustomExtensionTest.php +++ b/tests/CustomExtensionTest.php @@ -23,12 +23,8 @@ class CustomExtensionTest extends TestCase */ public function testGetInvalidOperators(ExtensionInterface $extension, $expectedExceptionMessage) { - if (method_exists($this, 'expectException')) { - $this->expectException('InvalidArgumentException'); - $this->expectExceptionMessage($expectedExceptionMessage); - } else { - $this->setExpectedException('InvalidArgumentException', $expectedExceptionMessage); - } + $this->expectException('InvalidArgumentException'); + $this->expectExceptionMessage($expectedExceptionMessage); $env = new Environment($this->getMockBuilder(LoaderInterface::class)->getMock()); $env->addExtension($extension); diff --git a/tests/ErrorTest.php b/tests/ErrorTest.php index 2bf81fb3e..b94dee2af 100644 --- a/tests/ErrorTest.php +++ b/tests/ErrorTest.php @@ -26,7 +26,7 @@ class ErrorTest extends TestCase $error = new Error('foo'); $error->setSourceContext(new Source('', new \SplFileInfo(__FILE__))); - $this->assertContains('tests'.\DIRECTORY_SEPARATOR.'ErrorTest.php', $error->getMessage()); + $this->assertStringContainsString('tests'.\DIRECTORY_SEPARATOR.'ErrorTest.php', $error->getMessage()); } public function testTwigExceptionGuessWithMissingVarAndArrayLoader() diff --git a/tests/ExpressionParserTest.php b/tests/ExpressionParserTest.php index c38025603..9a2763ae5 100644 --- a/tests/ExpressionParserTest.php +++ b/tests/ExpressionParserTest.php @@ -24,14 +24,14 @@ use Twig\Source; class ExpressionParserTest extends TestCase { /** - * @expectedException \Twig\Error\SyntaxError * @dataProvider getFailingTestsForAssignment */ public function testCanOnlyAssignToNames($template) { + $this->expectException('\Twig\Error\SyntaxError'); + $env = new Environment($this->getMockBuilder(LoaderInterface::class)->getMock(), ['cache' => false, 'autoescape' => false]); $parser = new Parser($env); - $parser->parse($env->tokenize(new Source($template, 'index'))); } @@ -67,14 +67,14 @@ class ExpressionParserTest extends TestCase } /** - * @expectedException \Twig\Error\SyntaxError * @dataProvider getFailingTestsForArray */ public function testArraySyntaxError($template) { + $this->expectException('\Twig\Error\SyntaxError'); + $env = new Environment($this->getMockBuilder(LoaderInterface::class)->getMock(), ['cache' => false, 'autoescape' => false]); $parser = new Parser($env); - $parser->parse($env->tokenize(new Source($template, 'index'))); } @@ -162,11 +162,10 @@ class ExpressionParserTest extends TestCase ]; } - /** - * @expectedException \Twig\Error\SyntaxError - */ public function testStringExpressionDoesNotConcatenateTwoConsecutiveStrings() { + $this->expectException('\Twig\Error\SyntaxError'); + $env = new Environment($this->getMockBuilder(LoaderInterface::class)->getMock(), ['cache' => false, 'autoescape' => false, 'optimizations' => 0]); $stream = $env->tokenize(new Source('{{ "a" "b" }}', 'index')); $parser = new Parser($env); @@ -234,34 +233,31 @@ class ExpressionParserTest extends TestCase ]; } - /** - * @expectedException \Twig\Error\SyntaxError - */ public function testAttributeCallDoesNotSupportNamedArguments() { + $this->expectException('\Twig\Error\SyntaxError'); + $env = new Environment($this->getMockBuilder(LoaderInterface::class)->getMock(), ['cache' => false, 'autoescape' => false]); $parser = new Parser($env); $parser->parse($env->tokenize(new Source('{{ foo.bar(name="Foo") }}', 'index'))); } - /** - * @expectedException \Twig\Error\SyntaxError - */ public function testMacroCallDoesNotSupportNamedArguments() { + $this->expectException('\Twig\Error\SyntaxError'); + $env = new Environment($this->getMockBuilder(LoaderInterface::class)->getMock(), ['cache' => false, 'autoescape' => false]); $parser = new Parser($env); $parser->parse($env->tokenize(new Source('{% from _self import foo %}{% macro foo() %}{% endmacro %}{{ foo(name="Foo") }}', 'index'))); } - /** - * @expectedException \Twig\Error\SyntaxError - * @expectedExceptionMessage An argument must be a name. Unexpected token "string" of value "a" ("name" expected) in "index" at line 1. - */ public function testMacroDefinitionDoesNotSupportNonNameVariableName() { + $this->expectException('\Twig\Error\SyntaxError'); + $this->expectExceptionMessage('An argument must be a name. Unexpected token "string" of value "a" ("name" expected) in "index" at line 1.'); + $env = new Environment($this->getMockBuilder(LoaderInterface::class)->getMock(), ['cache' => false, 'autoescape' => false]); $parser = new Parser($env); @@ -269,12 +265,13 @@ class ExpressionParserTest extends TestCase } /** - * @expectedException \Twig\Error\SyntaxError - * @expectedExceptionMessage A default value for an argument must be a constant (a boolean, a string, a number, or an array) in "index" at line 1 * @dataProvider getMacroDefinitionDoesNotSupportNonConstantDefaultValues */ public function testMacroDefinitionDoesNotSupportNonConstantDefaultValues($template) { + $this->expectException('\Twig\Error\SyntaxError'); + $this->expectExceptionMessage('A default value for an argument must be a constant (a boolean, a string, a number, or an array) in "index" at line 1'); + $env = new Environment($this->getMockBuilder(LoaderInterface::class)->getMock(), ['cache' => false, 'autoescape' => false]); $parser = new Parser($env); @@ -317,72 +314,66 @@ class ExpressionParserTest extends TestCase ]; } - /** - * @expectedException \Twig\Error\SyntaxError - * @expectedExceptionMessage Unknown "cycl" function. Did you mean "cycle" in "index" at line 1? - */ public function testUnknownFunction() { + $this->expectException('\Twig\Error\SyntaxError'); + $this->expectExceptionMessage('Unknown "cycl" function. Did you mean "cycle" in "index" at line 1?'); + $env = new Environment($this->getMockBuilder(LoaderInterface::class)->getMock(), ['cache' => false, 'autoescape' => false]); $parser = new Parser($env); $parser->parse($env->tokenize(new Source('{{ cycl() }}', 'index'))); } - /** - * @expectedException \Twig\Error\SyntaxError - * @expectedExceptionMessage Unknown "foobar" function in "index" at line 1. - */ public function testUnknownFunctionWithoutSuggestions() { + $this->expectException('\Twig\Error\SyntaxError'); + $this->expectExceptionMessage('Unknown "foobar" function in "index" at line 1.'); + $env = new Environment($this->getMockBuilder(LoaderInterface::class)->getMock(), ['cache' => false, 'autoescape' => false]); $parser = new Parser($env); $parser->parse($env->tokenize(new Source('{{ foobar() }}', 'index'))); } - /** - * @expectedException \Twig\Error\SyntaxError - * @expectedExceptionMessage Unknown "lowe" filter. Did you mean "lower" in "index" at line 1? - */ public function testUnknownFilter() { + $this->expectException('\Twig\Error\SyntaxError'); + $this->expectExceptionMessage('Unknown "lowe" filter. Did you mean "lower" in "index" at line 1?'); + $env = new Environment($this->getMockBuilder(LoaderInterface::class)->getMock(), ['cache' => false, 'autoescape' => false]); $parser = new Parser($env); $parser->parse($env->tokenize(new Source('{{ 1|lowe }}', 'index'))); } - /** - * @expectedException \Twig\Error\SyntaxError - * @expectedExceptionMessage Unknown "foobar" filter in "index" at line 1. - */ public function testUnknownFilterWithoutSuggestions() { + $this->expectException('\Twig\Error\SyntaxError'); + $this->expectExceptionMessage('Unknown "foobar" filter in "index" at line 1.'); + $env = new Environment($this->getMockBuilder(LoaderInterface::class)->getMock(), ['cache' => false, 'autoescape' => false]); $parser = new Parser($env); $parser->parse($env->tokenize(new Source('{{ 1|foobar }}', 'index'))); } - /** - * @expectedException \Twig\Error\SyntaxError - * @expectedExceptionMessage Unknown "nul" test. Did you mean "null" in "index" at line 1 - */ public function testUnknownTest() { + $this->expectException('\Twig\Error\SyntaxError'); + $this->expectExceptionMessage('Unknown "nul" test. Did you mean "null" in "index" at line 1'); + $env = new Environment($this->getMockBuilder(LoaderInterface::class)->getMock(), ['cache' => false, 'autoescape' => false]); $parser = new Parser($env); $stream = $env->tokenize(new Source('{{ 1 is nul }}', 'index')); $parser->parse($stream); } - /** - * @expectedException \Twig\Error\SyntaxError - * @expectedExceptionMessage Unknown "foobar" test in "index" at line 1. - */ public function testUnknownTestWithoutSuggestions() { + $this->expectException('\Twig\Error\SyntaxError'); + $this->expectExceptionMessage('Unknown "foobar" test in "index" at line 1.'); + $env = new Environment($this->getMockBuilder(LoaderInterface::class)->getMock(), ['cache' => false, 'autoescape' => false]); $parser = new Parser($env); diff --git a/tests/Extension/CoreTest.php b/tests/Extension/CoreTest.php index d3d517ff8..7a73c1a06 100644 --- a/tests/Extension/CoreTest.php +++ b/tests/Extension/CoreTest.php @@ -97,11 +97,9 @@ class CoreTest extends TestCase $this->assertSame($instance, twig_random(new Environment($this->getMockBuilder(LoaderInterface::class)->getMock()), $instance)); } - /** - * @expectedException \Twig\Error\RuntimeError - */ public function testRandomFunctionOfEmptyArrayThrowsException() { + $this->expectException('\Twig\Error\RuntimeError'); twig_random(new Environment($this->getMockBuilder(LoaderInterface::class)->getMock()), []); } diff --git a/tests/Extension/SandboxTest.php b/tests/Extension/SandboxTest.php index 8385f546c..cbfa90d9e 100644 --- a/tests/Extension/SandboxTest.php +++ b/tests/Extension/SandboxTest.php @@ -53,12 +53,11 @@ class SandboxTest extends TestCase ]; } - /** - * @expectedException \Twig\Sandbox\SecurityError - * @expectedExceptionMessage Filter "json_encode" is not allowed in "1_child" at line 3. - */ public function testSandboxWithInheritance() { + $this->expectException('\Twig\Sandbox\SecurityError'); + $this->expectExceptionMessage('Filter "json_encode" is not allowed in "1_child" at line 3.'); + $twig = $this->getEnvironment(true, [], self::$templates, ['block']); $twig->load('1_child')->render([]); } diff --git a/tests/LexerTest.php b/tests/LexerTest.php index 15b03b5af..ccd992432 100644 --- a/tests/LexerTest.php +++ b/tests/LexerTest.php @@ -237,12 +237,11 @@ class LexerTest extends TestCase $this->addToAssertionCount(1); } - /** - * @expectedException \Twig\Error\SyntaxError - * @expectedExceptionMessage Unclosed """ - */ public function testStringWithUnterminatedInterpolation() { + $this->expectException('\Twig\Error\SyntaxError'); + $this->expectExceptionMessage('Unclosed """'); + $template = '{{ "bar #{x" }}'; $lexer = new Lexer(new Environment($this->getMockBuilder(LoaderInterface::class)->getMock())); @@ -307,12 +306,11 @@ class LexerTest extends TestCase $this->addToAssertionCount(1); } - /** - * @expectedException \Twig\Error\SyntaxError - * @expectedExceptionMessage Unclosed "variable" in "index" at line 3 - */ public function testUnterminatedVariable() { + $this->expectException('\Twig\Error\SyntaxError'); + $this->expectExceptionMessage('Unclosed "variable" in "index" at line 3'); + $template = ' {{ @@ -326,12 +324,11 @@ bar $lexer->tokenize(new Source($template, 'index')); } - /** - * @expectedException \Twig\Error\SyntaxError - * @expectedExceptionMessage Unclosed "block" in "index" at line 3 - */ public function testUnterminatedBlock() { + $this->expectException('\Twig\Error\SyntaxError'); + $this->expectExceptionMessage('Unclosed "block" in "index" at line 3'); + $template = ' {% diff --git a/tests/Loader/ArrayTest.php b/tests/Loader/ArrayTest.php index cf8194724..5d5a1d6db 100644 --- a/tests/Loader/ArrayTest.php +++ b/tests/Loader/ArrayTest.php @@ -21,6 +21,8 @@ class ArrayTest extends TestCase */ public function testGetSourceContextWhenTemplateDoesNotExist() { + $this->expectException('\Twig\Error\LoaderError'); + $loader = new ArrayLoader([]); $loader->getSourceContext('foo'); @@ -55,11 +57,10 @@ class ArrayTest extends TestCase $this->assertEquals('foo:__bar', $loader->getCacheKey('foo')); } - /** - * @expectedException \Twig\Error\LoaderError - */ public function testGetCacheKeyWhenTemplateDoesNotExist() { + $this->expectException('\Twig\Error\LoaderError'); + $loader = new ArrayLoader([]); $loader->getCacheKey('foo'); @@ -79,11 +80,10 @@ class ArrayTest extends TestCase $this->assertTrue($loader->isFresh('foo', time())); } - /** - * @expectedException \Twig\Error\LoaderError - */ public function testIsFreshWhenTemplateDoesNotExist() { + $this->expectException('\Twig\Error\LoaderError'); + $loader = new ArrayLoader([]); $loader->isFresh('foo', time()); diff --git a/tests/Loader/ChainTest.php b/tests/Loader/ChainTest.php index 075677e6e..21a3a5f67 100644 --- a/tests/Loader/ChainTest.php +++ b/tests/Loader/ChainTest.php @@ -40,11 +40,10 @@ class ChainTest extends TestCase $this->assertNotEquals('baz', $loader->getSourceContext('errors/base.html')->getCode()); } - /** - * @expectedException \Twig\Error\LoaderError - */ public function testGetSourceContextWhenTemplateDoesNotExist() { + $this->expectException('\Twig\Error\LoaderError'); + $loader = new ChainLoader([]); $loader->getSourceContext('foo'); @@ -61,11 +60,10 @@ class ChainTest extends TestCase $this->assertEquals('bar:foo', $loader->getCacheKey('bar')); } - /** - * @expectedException \Twig\Error\LoaderError - */ public function testGetCacheKeyWhenTemplateDoesNotExist() { + $this->expectException('\Twig\Error\LoaderError'); + $loader = new ChainLoader([]); $loader->getCacheKey('foo'); diff --git a/tests/Loader/FilesystemTest.php b/tests/Loader/FilesystemTest.php index af9d9d702..ca05e7664 100644 --- a/tests/Loader/FilesystemTest.php +++ b/tests/Loader/FilesystemTest.php @@ -37,7 +37,7 @@ class FilesystemTest extends TestCase $loader->getCacheKey($template); $this->fail(); } catch (LoaderError $e) { - $this->assertNotContains('Unable to find template', $e->getMessage()); + $this->assertStringNotContainsString('Unable to find template', $e->getMessage()); } } diff --git a/tests/Node/Expression/CallTest.php b/tests/Node/Expression/CallTest.php index 40278cbd3..4eab4022f 100644 --- a/tests/Node/Expression/CallTest.php +++ b/tests/Node/Expression/CallTest.php @@ -22,52 +22,47 @@ class CallTest extends TestCase $this->assertEquals(['U', null], $this->getArguments($node, ['date', ['format' => 'U', 'timestamp' => null]])); } - /** - * @expectedException \Twig\Error\SyntaxError - * @expectedExceptionMessage Positional arguments cannot be used after named arguments for function "date". - */ public function testGetArgumentsWhenPositionalArgumentsAfterNamedArguments() { + $this->expectException('\Twig\Error\SyntaxError'); + $this->expectExceptionMessage('Positional arguments cannot be used after named arguments for function "date".'); + $node = new Node_Expression_Call([], ['type' => 'function', 'name' => 'date']); $this->getArguments($node, ['date', ['timestamp' => 123456, 'Y-m-d']]); } - /** - * @expectedException \Twig\Error\SyntaxError - * @expectedExceptionMessage Argument "format" is defined twice for function "date". - */ public function testGetArgumentsWhenArgumentIsDefinedTwice() { + $this->expectException('\Twig\Error\SyntaxError'); + $this->expectExceptionMessage('Argument "format" is defined twice for function "date".'); + $node = new Node_Expression_Call([], ['type' => 'function', 'name' => 'date']); $this->getArguments($node, ['date', ['Y-m-d', 'format' => 'U']]); } - /** - * @expectedException \Twig\Error\SyntaxError - * @expectedExceptionMessage Unknown argument "unknown" for function "date(format, timestamp)". - */ public function testGetArgumentsWithWrongNamedArgumentName() { + $this->expectException('\Twig\Error\SyntaxError'); + $this->expectExceptionMessage('Unknown argument "unknown" for function "date(format, timestamp)".'); + $node = new Node_Expression_Call([], ['type' => 'function', 'name' => 'date']); $this->getArguments($node, ['date', ['Y-m-d', 'timestamp' => null, 'unknown' => '']]); } - /** - * @expectedException \Twig\Error\SyntaxError - * @expectedExceptionMessage Unknown arguments "unknown1", "unknown2" for function "date(format, timestamp)". - */ public function testGetArgumentsWithWrongNamedArgumentNames() { + $this->expectException('\Twig\Error\SyntaxError'); + $this->expectExceptionMessage('Unknown arguments "unknown1", "unknown2" for function "date(format, timestamp)".'); + $node = new Node_Expression_Call([], ['type' => 'function', 'name' => 'date']); $this->getArguments($node, ['date', ['Y-m-d', 'timestamp' => null, 'unknown1' => '', 'unknown2' => '']]); } - /** - * @expectedException \Twig\Error\SyntaxError - * @expectedExceptionMessage Argument "case_sensitivity" could not be assigned for function "substr_compare(main_str, str, offset, length, case_sensitivity)" because it is mapped to an internal PHP function which cannot determine default value for optional argument "length". - */ public function testResolveArgumentsWithMissingValueForOptionalArgument() { + $this->expectException('\Twig\Error\SyntaxError'); + $this->expectExceptionMessage('Argument "case_sensitivity" could not be assigned for function "substr_compare(main_str, str, offset, length, case_sensitivity)" because it is mapped to an internal PHP function which cannot determine default value for optional argument "length".'); + $node = new Node_Expression_Call([], ['type' => 'function', 'name' => 'substr_compare']); $this->getArguments($node, ['substr_compare', ['abcd', 'bc', 'offset' => 1, 'case_sensitivity' => true]]); } @@ -85,12 +80,11 @@ class CallTest extends TestCase $this->assertEquals(['arg1'], $this->getArguments($node, [__CLASS__.'::customStaticFunction', ['arg1' => 'arg1']])); } - /** - * @expectedException \LogicException - * @expectedExceptionMessage The last parameter of "Twig\Tests\Node\Expression\CallTest::customFunctionWithArbitraryArguments" for function "foo" must be an array with default value, eg. "array $arg = []". - */ public function testResolveArgumentsWithMissingParameterForArbitraryArguments() { + $this->expectException('\LogicException'); + $this->expectExceptionMessage('The last parameter of "Twig\\Tests\\Node\\Expression\\CallTest::customFunctionWithArbitraryArguments" for function "foo" must be an array with default value, eg. "array $arg = []".'); + $node = new Node_Expression_Call([], ['type' => 'function', 'name' => 'foo', 'is_variadic' => true]); $this->getArguments($node, [[$this, 'customFunctionWithArbitraryArguments'], []]); } @@ -115,22 +109,20 @@ class CallTest extends TestCase { } - /** - * @expectedException \LogicException - * @expectedExceptionMessageRegExp #^The last parameter of "Twig\\Tests\\Node\\Expression\\custom_Twig_Tests_Node_Expression_CallTest_function" for function "foo" must be an array with default value, eg\. "array \$arg \= \[\]"\.$# - */ public function testResolveArgumentsWithMissingParameterForArbitraryArgumentsOnFunction() { + $this->expectException('\LogicException'); + $this->expectExceptionMessageRegExp('#^The last parameter of "Twig\\\\Tests\\\\Node\\\\Expression\\\\custom_Twig_Tests_Node_Expression_CallTest_function" for function "foo" must be an array with default value, eg\\. "array \\$arg \\= \\[\\]"\\.$#'); + $node = new Node_Expression_Call([], ['type' => 'function', 'name' => 'foo', 'is_variadic' => true]); $node->getArguments('Twig\Tests\Node\Expression\custom_Twig_Tests_Node_Expression_CallTest_function', []); } - /** - * @expectedException \LogicException - * @expectedExceptionMessageRegExp #^The last parameter of "Twig\\Tests\\Node\\Expression\\CallableTestClass\:\:__invoke" for function "foo" must be an array with default value, eg\. "array \$arg \= \[\]"\.$# - */ public function testResolveArgumentsWithMissingParameterForArbitraryArgumentsOnObject() { + $this->expectException('\LogicException'); + $this->expectExceptionMessageRegExp('#^The last parameter of "Twig\\\\Tests\\\\Node\\\\Expression\\\\CallableTestClass\\:\\:__invoke" for function "foo" must be an array with default value, eg\\. "array \\$arg \\= \\[\\]"\\.$#'); + $node = new Node_Expression_Call([], ['type' => 'function', 'name' => 'foo', 'is_variadic' => true]); $node->getArguments(new CallableTestClass(), []); } diff --git a/tests/Node/Expression/FilterTest.php b/tests/Node/Expression/FilterTest.php index 652747730..e65c79acb 100644 --- a/tests/Node/Expression/FilterTest.php +++ b/tests/Node/Expression/FilterTest.php @@ -106,12 +106,11 @@ class FilterTest extends NodeTestCase return $tests; } - /** - * @expectedException \Twig\Error\SyntaxError - * @expectedExceptionMessage Unknown argument "foobar" for filter "date(format, timezone)" at line 1. - */ public function testCompileWithWrongNamedArgumentName() { + $this->expectException('\Twig\Error\SyntaxError'); + $this->expectExceptionMessage('Unknown argument "foobar" for filter "date(format, timezone)" at line 1.'); + $date = new ConstantExpression(0, 1); $node = $this->createFilter($date, 'date', [ 'foobar' => new ConstantExpression('America/Chicago', 1), @@ -121,12 +120,11 @@ class FilterTest extends NodeTestCase $compiler->compile($node); } - /** - * @expectedException \Twig\Error\SyntaxError - * @expectedExceptionMessage Value for argument "from" is required for filter "replace" at line 1. - */ public function testCompileWithMissingNamedArgument() { + $this->expectException('\Twig\Error\SyntaxError'); + $this->expectExceptionMessage('Value for argument "from" is required for filter "replace" at line 1.'); + $value = new ConstantExpression(0, 1); $node = $this->createFilter($value, 'replace', [ 'to' => new ConstantExpression('foo', 1), diff --git a/tests/ParserTest.php b/tests/ParserTest.php index 8920f9fca..9335fd394 100644 --- a/tests/ParserTest.php +++ b/tests/ParserTest.php @@ -31,6 +31,9 @@ class ParserTest extends TestCase */ public function testUnknownTag() { + $this->expectException('\Twig\Error\SyntaxError'); + $this->expectExceptionMessage('Unknown "foo" tag. Did you mean "for" at line 1?'); + $stream = new TokenStream([ new Token(Token::BLOCK_START_TYPE, '', 1), new Token(Token::NAME_TYPE, 'foo', 1), @@ -41,12 +44,11 @@ class ParserTest extends TestCase $parser->parse($stream); } - /** - * @expectedException \Twig\Error\SyntaxError - * @expectedExceptionMessage Unknown "foobar" tag at line 1. - */ public function testUnknownTagWithoutSuggestions() { + $this->expectException('\Twig\Error\SyntaxError'); + $this->expectExceptionMessage('Unknown "foobar" tag at line 1.'); + $stream = new TokenStream([ new Token(Token::BLOCK_START_TYPE, '', 1), new Token(Token::NAME_TYPE, 'foobar', 1), @@ -89,10 +91,11 @@ class ParserTest extends TestCase /** * @dataProvider getFilterBodyNodesDataThrowsException - * @expectedException \Twig\Error\SyntaxError */ public function testFilterBodyNodesThrowsException($input) { + $this->expectException('\Twig\Error\SyntaxError'); + $parser = $this->getParser(); $m = new \ReflectionMethod($parser, 'filterBodyNodes'); diff --git a/tests/TemplateTest.php b/tests/TemplateTest.php index e2afb7b24..3a408b9c7 100644 --- a/tests/TemplateTest.php +++ b/tests/TemplateTest.php @@ -23,11 +23,10 @@ use Twig\Template; class TemplateTest extends TestCase { - /** - * @expectedException \LogicException - */ public function testDisplayBlocksAcceptTemplateOnlyAsBlocks() { + $this->expectException('\LogicException'); + $twig = new Environment($this->getMockBuilder(LoaderInterface::class)->getMock()); $template = new TemplateForTest($twig); $template->displayBlock('foo', [], ['foo' => [new \stdClass(), 'foo']]); @@ -108,7 +107,7 @@ class TemplateTest extends TestCase $this->addToAssertionCount(1); } - $this->assertContains('is not allowed', $e->getMessage()); + $this->assertStringContainsString('is not allowed', $e->getMessage()); } } @@ -209,13 +208,9 @@ class TemplateTest extends TestCase if ($defined) { $this->assertEquals($value, twig_get_attribute($twig, $template->getSourceContext(), $object, $item, $arguments, $type)); } else { - if (method_exists($this, 'expectException')) { - $this->expectException(RuntimeError::class); - if (null !== $exceptionMessage) { - $this->expectExceptionMessage($exceptionMessage); - } - } else { - $this->setExpectedException(RuntimeError::class, $exceptionMessage); + $this->expectException(RuntimeError::class); + if (null !== $exceptionMessage) { + $this->expectExceptionMessage($exceptionMessage); } $this->assertEquals($value, twig_get_attribute($twig, $template->getSourceContext(), $object, $item, $arguments, $type)); } @@ -389,12 +384,10 @@ class TemplateTest extends TestCase return $tests; } - /** - * @expectedException \Twig\Error\RuntimeError - */ public function testGetIsMethods() { - $twig = new Environment($this->getMockBuilder(LoaderInterface::class)->getMock(), ['strict_variables' => true]); + $this->expectException('\Twig\Error\RuntimeError'); + $getIsObject = new TemplateGetIsMethods(); $template = new TemplateForTest($twig, 'index.twig'); // first time should not create a cache for "get" diff --git a/tests/TokenStreamTest.php b/tests/TokenStreamTest.php index a6440d6ca..38c4163bf 100644 --- a/tests/TokenStreamTest.php +++ b/tests/TokenStreamTest.php @@ -45,12 +45,11 @@ class TokenStreamTest extends TestCase $this->assertEquals('1, 2, 3, 4, 5, 6, 7', implode(', ', $repr), '->next() advances the pointer and returns the current token'); } - /** - * @expectedException \Twig\Error\SyntaxError - * @expectedExceptionMessage Unexpected end of template - */ public function testEndOfTemplateNext() { + $this->expectException('\Twig\Error\SyntaxError'); + $this->expectExceptionMessage('Unexpected end of template'); + $stream = new TokenStream([ new Token(Token::BLOCK_START_TYPE, 1, 1), ]); @@ -59,12 +58,11 @@ class TokenStreamTest extends TestCase } } - /** - * @expectedException \Twig\Error\SyntaxError - * @expectedExceptionMessage Unexpected end of template - */ public function testEndOfTemplateLook() { + $this->expectException('\Twig\Error\SyntaxError'); + $this->expectExceptionMessage('Unexpected end of template'); + $stream = new TokenStream([ new Token(Token::BLOCK_START_TYPE, 1, 1), ]);