From 6fb7cb9eb2cefe39e1a032d54776f9609065263e Mon Sep 17 00:00:00 2001 From: Alexandre Daubois Date: Sat, 14 Jan 2023 17:44:03 +0100 Subject: [PATCH] [Tests] Remove `$this` occurrences in future static data providers --- .../Tests/Html5ParserCrawlerTest.php | 6 ++--- .../Tests/ExpressionLanguageTest.php | 15 +++++++++-- .../Component/Filesystem/Tests/PathTest.php | 27 ++++++++++++++++--- .../Iterator/DepthRangeFilterIteratorTest.php | 8 +++--- .../ExcludeDirectoryFilterIteratorTest.php | 6 ++--- .../Iterator/FileTypeFilterIteratorTest.php | 4 +-- .../Tests/Iterator/SortableIteratorTest.php | 14 +++++----- .../Tests/EventListener/ErrorListenerTest.php | 2 +- .../HandleMessageMiddlewareTest.php | 24 ++++++++++++----- 9 files changed, 74 insertions(+), 32 deletions(-) diff --git a/src/Symfony/Component/DomCrawler/Tests/Html5ParserCrawlerTest.php b/src/Symfony/Component/DomCrawler/Tests/Html5ParserCrawlerTest.php index 05d1bc76a9f1a..92370616598b0 100644 --- a/src/Symfony/Component/DomCrawler/Tests/Html5ParserCrawlerTest.php +++ b/src/Symfony/Component/DomCrawler/Tests/Html5ParserCrawlerTest.php @@ -52,7 +52,7 @@ public function testHtml5ParserWithInvalidHeadedContent(string $content) public function validHtml5Provider(): iterable { - $html = $this->getDoctype().'

Foo

'; + $html = static::getDoctype().'

Foo

'; $BOM = \chr(0xEF).\chr(0xBB).\chr(0xBF); yield 'BOM first' => [$BOM.$html]; @@ -60,12 +60,12 @@ public function validHtml5Provider(): iterable yield 'Multiline comment' => ["".$html]; yield 'Several comments' => [' '.$html]; yield 'Whitespaces' => [' '.$html]; - yield 'All together' => [$BOM.' '.''.$html]; + yield 'All together' => [$BOM.' '.$html]; } public function invalidHtml5Provider(): iterable { - $html = $this->getDoctype().'

Foo

'; + $html = static::getDoctype().'

Foo

'; yield 'Text' => ['hello world'.$html]; yield 'Text between comments' => [' test '.$html]; diff --git a/src/Symfony/Component/ExpressionLanguage/Tests/ExpressionLanguageTest.php b/src/Symfony/Component/ExpressionLanguage/Tests/ExpressionLanguageTest.php index c0bd560d31ba1..c4c98dd88006b 100644 --- a/src/Symfony/Component/ExpressionLanguage/Tests/ExpressionLanguageTest.php +++ b/src/Symfony/Component/ExpressionLanguage/Tests/ExpressionLanguageTest.php @@ -121,8 +121,19 @@ public function testParseThrowsInsteadOfNotice() public function shortCircuitProviderEvaluate() { - $object = $this->getMockBuilder(\stdClass::class)->setMethods(['foo'])->getMock(); - $object->expects($this->never())->method('foo'); + $object = new class(\Closure::fromCallable([static::class, 'fail'])) { + private $fail; + + public function __construct(callable $fail) + { + $this->fail = $fail; + } + + public function foo() + { + ($this->fail)(); + } + }; return [ ['false and object.foo()', ['object' => $object], false], diff --git a/src/Symfony/Component/Filesystem/Tests/PathTest.php b/src/Symfony/Component/Filesystem/Tests/PathTest.php index 2f04c790c396a..3d0f2beb11087 100644 --- a/src/Symfony/Component/Filesystem/Tests/PathTest.php +++ b/src/Symfony/Component/Filesystem/Tests/PathTest.php @@ -458,11 +458,30 @@ public function providePathTests(): \Generator yield ['..', '/webmozart/symfony', '/webmozart']; } + private static function getPathTests(): \Generator + { + yield from [ + // relative to absolute path + ['css/style.css', '/webmozart/symfony', '/webmozart/symfony/css/style.css'], + ['../css/style.css', '/webmozart/symfony', '/webmozart/css/style.css'], + ['../../css/style.css', '/webmozart/symfony', '/css/style.css'], + + // relative to root + ['css/style.css', '/', '/css/style.css'], + ['css/style.css', 'C:', 'C:/css/style.css'], + ['css/style.css', 'C:/', 'C:/css/style.css'], + + // same sub directories in different base directories + ['../../symfony/css/style.css', '/webmozart/css', '/symfony/css/style.css'], + + ['', '/webmozart/symfony', '/webmozart/symfony'], + ['..', '/webmozart/symfony', '/webmozart'], + ]; + } + public function provideMakeAbsoluteTests(): \Generator { - foreach ($this->providePathTests() as $set) { - yield $set; - } + yield from static::getPathTests(); // collapse dots yield ['css/./style.css', '/webmozart/symfony', '/webmozart/symfony/css/style.css']; @@ -589,7 +608,7 @@ public function testMakeAbsoluteDoesNotFailIfDifferentRoot(string $basePath, str public function provideMakeRelativeTests(): \Generator { - foreach ($this->providePathTests() as $set) { + foreach (static::getPathTests() as $set) { yield [$set[2], $set[1], $set[0]]; } diff --git a/src/Symfony/Component/Finder/Tests/Iterator/DepthRangeFilterIteratorTest.php b/src/Symfony/Component/Finder/Tests/Iterator/DepthRangeFilterIteratorTest.php index b2a5303b58d30..a66e50287e74f 100644 --- a/src/Symfony/Component/Finder/Tests/Iterator/DepthRangeFilterIteratorTest.php +++ b/src/Symfony/Component/Finder/Tests/Iterator/DepthRangeFilterIteratorTest.php @@ -93,11 +93,11 @@ public function getAcceptData() ]; return [ - [0, 0, $this->toAbsolute($lessThan1)], - [0, 1, $this->toAbsolute($lessThanOrEqualTo1)], + [0, 0, static::toAbsolute($lessThan1)], + [0, 1, static::toAbsolute($lessThanOrEqualTo1)], [2, \PHP_INT_MAX, []], - [1, \PHP_INT_MAX, $this->toAbsolute($graterThanOrEqualTo1)], - [1, 1, $this->toAbsolute($equalTo1)], + [1, \PHP_INT_MAX, static::toAbsolute($graterThanOrEqualTo1)], + [1, 1, static::toAbsolute($equalTo1)], ]; } } diff --git a/src/Symfony/Component/Finder/Tests/Iterator/ExcludeDirectoryFilterIteratorTest.php b/src/Symfony/Component/Finder/Tests/Iterator/ExcludeDirectoryFilterIteratorTest.php index 1729766fe4bd0..572029a17f8fa 100644 --- a/src/Symfony/Component/Finder/Tests/Iterator/ExcludeDirectoryFilterIteratorTest.php +++ b/src/Symfony/Component/Finder/Tests/Iterator/ExcludeDirectoryFilterIteratorTest.php @@ -99,9 +99,9 @@ public function getAcceptData() ]; return [ - [['foo'], $this->toAbsolute($foo)], - [['fo'], $this->toAbsolute($fo)], - [['toto/'], $this->toAbsolute($toto)], + [['foo'], static::toAbsolute($foo)], + [['fo'], static::toAbsolute($fo)], + [['toto/'], static::toAbsolute($toto)], ]; } } diff --git a/src/Symfony/Component/Finder/Tests/Iterator/FileTypeFilterIteratorTest.php b/src/Symfony/Component/Finder/Tests/Iterator/FileTypeFilterIteratorTest.php index e15c03521af90..be7beb1d146ad 100644 --- a/src/Symfony/Component/Finder/Tests/Iterator/FileTypeFilterIteratorTest.php +++ b/src/Symfony/Component/Finder/Tests/Iterator/FileTypeFilterIteratorTest.php @@ -57,8 +57,8 @@ public function getAcceptData() ]; return [ - [FileTypeFilterIterator::ONLY_FILES, $this->toAbsolute($onlyFiles)], - [FileTypeFilterIterator::ONLY_DIRECTORIES, $this->toAbsolute($onlyDirectories)], + [FileTypeFilterIterator::ONLY_FILES, static::toAbsolute($onlyFiles)], + [FileTypeFilterIterator::ONLY_DIRECTORIES, static::toAbsolute($onlyDirectories)], ]; } } diff --git a/src/Symfony/Component/Finder/Tests/Iterator/SortableIteratorTest.php b/src/Symfony/Component/Finder/Tests/Iterator/SortableIteratorTest.php index 098cd75674f3f..049961452b183 100644 --- a/src/Symfony/Component/Finder/Tests/Iterator/SortableIteratorTest.php +++ b/src/Symfony/Component/Finder/Tests/Iterator/SortableIteratorTest.php @@ -247,13 +247,13 @@ public function getAcceptData() ]; return [ - [SortableIterator::SORT_BY_NAME, $this->toAbsolute($sortByName)], - [SortableIterator::SORT_BY_TYPE, $this->toAbsolute($sortByType)], - [SortableIterator::SORT_BY_ACCESSED_TIME, $this->toAbsolute($sortByAccessedTime)], - [SortableIterator::SORT_BY_CHANGED_TIME, $this->toAbsolute($sortByChangedTime)], - [SortableIterator::SORT_BY_MODIFIED_TIME, $this->toAbsolute($sortByModifiedTime)], - [SortableIterator::SORT_BY_NAME_NATURAL, $this->toAbsolute($sortByNameNatural)], - [function (\SplFileInfo $a, \SplFileInfo $b) { return strcmp($a->getRealPath(), $b->getRealPath()); }, $this->toAbsolute($customComparison)], + [SortableIterator::SORT_BY_NAME, static::toAbsolute($sortByName)], + [SortableIterator::SORT_BY_TYPE, static::toAbsolute($sortByType)], + [SortableIterator::SORT_BY_ACCESSED_TIME, static::toAbsolute($sortByAccessedTime)], + [SortableIterator::SORT_BY_CHANGED_TIME, static::toAbsolute($sortByChangedTime)], + [SortableIterator::SORT_BY_MODIFIED_TIME, static::toAbsolute($sortByModifiedTime)], + [SortableIterator::SORT_BY_NAME_NATURAL, static::toAbsolute($sortByNameNatural)], + [function (\SplFileInfo $a, \SplFileInfo $b) { return strcmp($a->getRealPath(), $b->getRealPath()); }, static::toAbsolute($customComparison)], ]; } } diff --git a/src/Symfony/Component/HttpKernel/Tests/EventListener/ErrorListenerTest.php b/src/Symfony/Component/HttpKernel/Tests/EventListener/ErrorListenerTest.php index 00a6bde9004ce..73cc19afc8c6b 100644 --- a/src/Symfony/Component/HttpKernel/Tests/EventListener/ErrorListenerTest.php +++ b/src/Symfony/Component/HttpKernel/Tests/EventListener/ErrorListenerTest.php @@ -209,7 +209,7 @@ public function controllerProvider() }]; yield [function ($exception) { - $this->assertInstanceOf(FlattenException::class, $exception); + static::assertInstanceOf(FlattenException::class, $exception); return new Response('OK: '.$exception->getMessage()); }]; diff --git a/src/Symfony/Component/Messenger/Tests/Middleware/HandleMessageMiddlewareTest.php b/src/Symfony/Component/Messenger/Tests/Middleware/HandleMessageMiddlewareTest.php index 34b84596c67d7..eb0beaa8a5848 100644 --- a/src/Symfony/Component/Messenger/Tests/Middleware/HandleMessageMiddlewareTest.php +++ b/src/Symfony/Component/Messenger/Tests/Middleware/HandleMessageMiddlewareTest.php @@ -69,16 +69,28 @@ public function testItAddsHandledStamps(array $handlers, array $expectedStamps, public function itAddsHandledStampsProvider(): iterable { - $first = $this->createPartialMock(HandleMessageMiddlewareTestCallable::class, ['__invoke']); - $first->method('__invoke')->willReturn('first result'); + $first = new class() extends HandleMessageMiddlewareTestCallable { + public function __invoke() + { + return 'first result'; + } + }; $firstClass = \get_class($first); - $second = $this->createPartialMock(HandleMessageMiddlewareTestCallable::class, ['__invoke']); - $second->method('__invoke')->willReturn(null); + $second = new class() extends HandleMessageMiddlewareTestCallable { + public function __invoke() + { + return null; + } + }; $secondClass = \get_class($second); - $failing = $this->createPartialMock(HandleMessageMiddlewareTestCallable::class, ['__invoke']); - $failing->method('__invoke')->will($this->throwException(new \Exception('handler failed.'))); + $failing = new class() extends HandleMessageMiddlewareTestCallable { + public function __invoke() + { + throw new \Exception('handler failed.'); + } + }; yield 'A stamp is added' => [ [$first],