diff --git a/src/Symfony/Bundle/FrameworkBundle/Test/BrowserKitAssertionsTrait.php b/src/Symfony/Bundle/FrameworkBundle/Test/BrowserKitAssertionsTrait.php index 7d49aa61d22c6..6086e75ecec4c 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Test/BrowserKitAssertionsTrait.php +++ b/src/Symfony/Bundle/FrameworkBundle/Test/BrowserKitAssertionsTrait.php @@ -16,6 +16,7 @@ use PHPUnit\Framework\Constraint\LogicalNot; use PHPUnit\Framework\ExpectationFailedException; use Symfony\Component\BrowserKit\AbstractBrowser; +use Symfony\Component\BrowserKit\History; use Symfony\Component\BrowserKit\Test\Constraint as BrowserKitConstraint; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; @@ -122,6 +123,38 @@ public static function assertBrowserNotHasCookie(string $name, string $path = '/ self::assertThatForClient(new LogicalNot(new BrowserKitConstraint\BrowserHasCookie($name, $path, $domain)), $message); } + public static function assertBrowserHistoryIsOnFirstPage(string $message = ''): void + { + if (!method_exists(History::class, 'isFirstPage')) { + throw new \LogicException('The `assertBrowserHistoryIsOnFirstPage` method requires symfony/browser-kit >= 7.4.'); + } + self::assertThatForClient(new BrowserKitConstraint\BrowserHistoryIsOnFirstPage(), $message); + } + + public static function assertBrowserHistoryIsNotOnFirstPage(string $message = ''): void + { + if (!method_exists(History::class, 'isFirstPage')) { + throw new \LogicException('The `assertBrowserHistoryIsNotOnFirstPage` method requires symfony/browser-kit >= 7.4.'); + } + self::assertThatForClient(new LogicalNot(new BrowserKitConstraint\BrowserHistoryIsOnFirstPage()), $message); + } + + public static function assertBrowserHistoryIsOnLastPage(string $message = ''): void + { + if (!method_exists(History::class, 'isLastPage')) { + throw new \LogicException('The `assertBrowserHistoryIsOnLastPage` method requires symfony/browser-kit >= 7.4.'); + } + self::assertThatForClient(new BrowserKitConstraint\BrowserHistoryIsOnLastPage(), $message); + } + + public static function assertBrowserHistoryIsNotOnLastPage(string $message = ''): void + { + if (!method_exists(History::class, 'isLastPage')) { + throw new \LogicException('The `assertBrowserHistoryIsNotOnLastPage` method requires symfony/browser-kit >= 7.4.'); + } + self::assertThatForClient(new LogicalNot(new BrowserKitConstraint\BrowserHistoryIsOnLastPage()), $message); + } + public static function assertBrowserCookieValueSame(string $name, string $expectedValue, bool $raw = false, string $path = '/', ?string $domain = null, string $message = ''): void { self::assertThatForClient(LogicalAnd::fromConstraints( diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Test/WebTestCaseTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Test/WebTestCaseTest.php index 84f2ef0ef31f2..fd65b18d830cf 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Test/WebTestCaseTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Test/WebTestCaseTest.php @@ -13,12 +13,14 @@ use PHPUnit\Framework\AssertionFailedError; use PHPUnit\Framework\ExpectationFailedException; +use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; use Symfony\Bundle\FrameworkBundle\KernelBrowser; use Symfony\Bundle\FrameworkBundle\Test\WebTestAssertionsTrait; use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; use Symfony\Component\BrowserKit\Cookie; use Symfony\Component\BrowserKit\CookieJar; +use Symfony\Component\BrowserKit\History; use Symfony\Component\DomCrawler\Crawler; use Symfony\Component\HttpFoundation\Cookie as HttpFoundationCookie; use Symfony\Component\HttpFoundation\Request; @@ -190,6 +192,50 @@ public function testAssertBrowserCookieValueSame() $this->getClientTester()->assertBrowserCookieValueSame('foo', 'babar', false, '/path'); } + /** + * @requires function \Symfony\Component\BrowserKit\History::isFirstPage + */ + public function testAssertBrowserHistoryIsOnFirstPage() + { + $this->createHistoryTester('isFirstPage', true)->assertBrowserHistoryIsOnFirstPage(); + $this->expectException(AssertionFailedError::class); + $this->expectExceptionMessage('Failed asserting that the Browser history is on the first page.'); + $this->createHistoryTester('isFirstPage', false)->assertBrowserHistoryIsOnFirstPage(); + } + + /** + * @requires function \Symfony\Component\BrowserKit\History::isFirstPage + */ + public function testAssertBrowserHistoryIsNotOnFirstPage() + { + $this->createHistoryTester('isFirstPage', false)->assertBrowserHistoryIsNotOnFirstPage(); + $this->expectException(AssertionFailedError::class); + $this->expectExceptionMessage('Failed asserting that the Browser history is not on the first page.'); + $this->createHistoryTester('isFirstPage', true)->assertBrowserHistoryIsNotOnFirstPage(); + } + + /** + * @requires function \Symfony\Component\BrowserKit\History::isLastPage + */ + public function testAssertBrowserHistoryIsOnLastPage() + { + $this->createHistoryTester('isLastPage', true)->assertBrowserHistoryIsOnLastPage(); + $this->expectException(AssertionFailedError::class); + $this->expectExceptionMessage('Failed asserting that the Browser history is on the last page.'); + $this->createHistoryTester('isLastPage', false)->assertBrowserHistoryIsOnLastPage(); + } + + /** + * @requires function \Symfony\Component\BrowserKit\History::isLastPage + */ + public function testAssertBrowserHistoryIsNotOnLastPage() + { + $this->createHistoryTester('isLastPage', false)->assertBrowserHistoryIsNotOnLastPage(); + $this->expectException(AssertionFailedError::class); + $this->expectExceptionMessage('Failed asserting that the Browser history is not on the last page.'); + $this->createHistoryTester('isLastPage', true)->assertBrowserHistoryIsNotOnLastPage(); + } + public function testAssertSelectorExists() { $this->getCrawlerTester(new Crawler('

'))->assertSelectorExists('body > h1'); @@ -386,6 +432,19 @@ private function getRequestTester(): WebTestCase return $this->getTester($client); } + private function createHistoryTester(string $method, bool $returnValue): WebTestCase + { + /** @var KernelBrowser&MockObject $client */ + $client = $this->createMock(KernelBrowser::class); + /** @var History&MockObject $history */ + $history = $this->createMock(History::class); + + $history->method($method)->willReturn($returnValue); + $client->method('getHistory')->willReturn($history); + + return $this->getTester($client); + } + private function getTester(KernelBrowser $client): WebTestCase { $tester = new class(method_exists($this, 'name') ? $this->name() : $this->getName()) extends WebTestCase { diff --git a/src/Symfony/Component/BrowserKit/CHANGELOG.md b/src/Symfony/Component/BrowserKit/CHANGELOG.md index 2437bbc7ddfcc..d078c1068abf9 100644 --- a/src/Symfony/Component/BrowserKit/CHANGELOG.md +++ b/src/Symfony/Component/BrowserKit/CHANGELOG.md @@ -5,6 +5,7 @@ CHANGELOG --- * Add `isFirstPage()` and `isLastPage()` methods to the History class for checking navigation boundaries + * Add PHPUnit constraints: `BrowserHistoryIsOnFirstPage` and `BrowserHistoryIsOnLastPage` 6.4 --- diff --git a/src/Symfony/Component/BrowserKit/Test/Constraint/BrowserHistoryIsOnFirstPage.php b/src/Symfony/Component/BrowserKit/Test/Constraint/BrowserHistoryIsOnFirstPage.php new file mode 100644 index 0000000000000..be5be9a44e941 --- /dev/null +++ b/src/Symfony/Component/BrowserKit/Test/Constraint/BrowserHistoryIsOnFirstPage.php @@ -0,0 +1,37 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\BrowserKit\Test\Constraint; + +use PHPUnit\Framework\Constraint\Constraint; +use Symfony\Component\BrowserKit\AbstractBrowser; + +final class BrowserHistoryIsOnFirstPage extends Constraint +{ + public function toString(): string + { + return 'is on the first page'; + } + + protected function matches($other): bool + { + if (!$other instanceof AbstractBrowser) { + throw new \LogicException('Can only test on an AbstractBrowser instance.'); + } + + return $other->getHistory()->isFirstPage(); + } + + protected function failureDescription($other): string + { + return 'the Browser history '.$this->toString(); + } +} diff --git a/src/Symfony/Component/BrowserKit/Test/Constraint/BrowserHistoryIsOnLastPage.php b/src/Symfony/Component/BrowserKit/Test/Constraint/BrowserHistoryIsOnLastPage.php new file mode 100644 index 0000000000000..38658a0f0312b --- /dev/null +++ b/src/Symfony/Component/BrowserKit/Test/Constraint/BrowserHistoryIsOnLastPage.php @@ -0,0 +1,37 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\BrowserKit\Test\Constraint; + +use PHPUnit\Framework\Constraint\Constraint; +use Symfony\Component\BrowserKit\AbstractBrowser; + +final class BrowserHistoryIsOnLastPage extends Constraint +{ + public function toString(): string + { + return 'is on the last page'; + } + + protected function matches($other): bool + { + if (!$other instanceof AbstractBrowser) { + throw new \LogicException('Can only test on an AbstractBrowser instance.'); + } + + return $other->getHistory()->isLastPage(); + } + + protected function failureDescription($other): string + { + return 'the Browser history '.$this->toString(); + } +} pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy