Skip to content

[BrowserKit] Add PHPUnit constraints: BrowserHistoryIsOnFirstPage and BrowserHistoryIsOnLastPage #60955

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

Open
wants to merge 1 commit into
base: 7.4
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
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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')) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Instead of these checks, what about raising the requirement to 7.4 ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi 👋, thanks for your comment! 😀

Personally, I don't have a strong preference, but in a previous PR, @nicolas-grekas mentioned not bumping versions 😅. If you'd prefer, I can raise the version to 7.4 🚀

Thanks again for your input! 🙏

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(
Expand Down
59 changes: 59 additions & 0 deletions src/Symfony/Bundle/FrameworkBundle/Tests/Test/WebTestCaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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('<html><body><h1>'))->assertSelectorExists('body > h1');
Expand Down Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/BrowserKit/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
---
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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();
}
}
Loading