Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into chore/upgrade-phpstan
Browse files Browse the repository at this point in the history
  • Loading branch information
uuf6429 committed Jan 5, 2025
2 parents 602aa24 + f62470d commit d0795ad
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 30 deletions.
12 changes: 4 additions & 8 deletions tests/Custom/CapabilityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@

namespace Mink\WebdriverClassicDriver\Tests\Custom;

use Facebook\WebDriver\Remote\RemoteWebDriver;
use Facebook\WebDriver\WebDriverOptions;
use Facebook\WebDriver\WebDriverTimeouts;
use Mink\WebdriverClassicDriver\Tests\WebDriverMockingTrait;
use Mink\WebdriverClassicDriver\WebdriverClassicDriver;

/**
* @phpstan-import-type TCapabilities from WebdriverClassicDriver
*/
class CapabilityTest extends \PHPUnit\Framework\TestCase
{
use WebDriverMockingTrait;

/**
* @param TCapabilities $desiredCapabilities
* @param TCapabilities $expectedCapabilities
Expand All @@ -20,11 +20,7 @@ class CapabilityTest extends \PHPUnit\Framework\TestCase
*/
public function testThatCapabilitiesAreAsExpected(string $browserName, array $desiredCapabilities, array $expectedCapabilities): void
{
$mockWebDriver = $this->createMock(RemoteWebDriver::class);
$mockWebDriverOptions = $this->createMock(WebDriverOptions::class);
$mockWebDriverTimeouts = $this->createMock(WebDriverTimeouts::class);
$mockWebDriver->method('manage')->willReturn($mockWebDriverOptions);
$mockWebDriverOptions->method('timeouts')->willReturn($mockWebDriverTimeouts);
$mockWebDriver = $this->createMockWebDriver();

$actualCapabilities = null;
$driver = new WebdriverClassicDriver(
Expand Down
34 changes: 12 additions & 22 deletions tests/Custom/WebDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
namespace Mink\WebdriverClassicDriver\Tests\Custom;

use Behat\Mink\Exception\DriverException;
use Facebook\WebDriver\Remote\RemoteWebDriver;
use Facebook\WebDriver\Remote\RemoteWebElement;
use Facebook\WebDriver\WebDriverOptions;
use Facebook\WebDriver\WebDriverTimeouts;
use Mink\WebdriverClassicDriver\Tests\WebDriverMockingTrait;
use Mink\WebdriverClassicDriver\WebdriverClassicDriver;

class WebDriverTest extends TestCase
{
use WebDriverMockingTrait;

public function testDriverMustBeStartedBeforeUse(): void
{
$this->expectException(DriverException::class);
Expand Down Expand Up @@ -39,11 +39,9 @@ public function testStartedDriverCannotBeSubsequentlyStarted(): void

public function testDriverCatchesUpstreamErrorsDuringStart(): void
{
$driver = $this->createPartialMock(WebdriverClassicDriver::class, ['createWebDriver', 'getWebDriver']);
$driver
->expects($this->once())
->method('createWebDriver')
->willThrowException(new \RuntimeException('An upstream error'));
$driver = new WebdriverClassicDriver('fake browser', [], 'example.com', function () {
throw new \RuntimeException('An upstream error');
});

$this->expectException(DriverException::class);
$this->expectExceptionMessage('Could not start driver: An upstream error');
Expand All @@ -53,15 +51,11 @@ public function testDriverCatchesUpstreamErrorsDuringStart(): void

public function testDriverCatchesUpstreamErrorsDuringStop(): void
{
$driver = $this->createPartialMock(WebdriverClassicDriver::class, ['createWebDriver', 'isStarted', 'getWebDriver']);
$driver
->expects($this->once())
->method('isStarted')
->willReturn(true);
$driver
->expects($this->once())
->method('getWebDriver')
->willThrowException(new \RuntimeException('An upstream error'));
$mockWebDriver = $this->createMockWebDriver();
$mockWebDriver->method('quit')->willThrowException(new \RuntimeException('An upstream error'));
$driver = new WebdriverClassicDriver('fake browser', [], 'example.com', fn() => $mockWebDriver);

$driver->start();

$this->expectException(DriverException::class);
$this->expectExceptionMessage('Could not close connection: An upstream error');
Expand All @@ -79,12 +73,8 @@ public function testClassicDriverCanProvideBrowserName(): void

public function testThatDriverCatchesUnexpectedAttributeValueType(): void
{
$mockWebDriver = $this->createMock(RemoteWebDriver::class);
$mockWebDriverOptions = $this->createMock(WebDriverOptions::class);
$mockWebDriverTimeouts = $this->createMock(WebDriverTimeouts::class);
$mockWebDriver = $this->createMockWebDriver();
$mockElement = $this->createMock(RemoteWebElement::class);
$mockWebDriver->method('manage')->willReturn($mockWebDriverOptions);
$mockWebDriverOptions->method('timeouts')->willReturn($mockWebDriverTimeouts);
$mockWebDriver
->expects($this->once())
->method('findElement')
Expand Down
34 changes: 34 additions & 0 deletions tests/WebDriverMockingTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Mink\WebdriverClassicDriver\Tests;

use Facebook\WebDriver\Remote\RemoteWebDriver;
use Facebook\WebDriver\WebDriverOptions;
use Facebook\WebDriver\WebDriverTimeouts;
use PHPUnit\Framework\MockObject\MockObject;

trait WebDriverMockingTrait
{
/**
* @template T
* @param class-string<T> $class
* @return T&MockObject
*/
abstract function createMock(string $class): object;

/**
* @return RemoteWebDriver&MockObject
*/
private function createMockWebDriver(): RemoteWebDriver
{
$mockWebDriverTimeouts = $this->createMock(WebDriverTimeouts::class);

$mockWebDriverOptions = $this->createMock(WebDriverOptions::class);
$mockWebDriverOptions->method('timeouts')->willReturn($mockWebDriverTimeouts);

$mockWebDriver = $this->createMock(RemoteWebDriver::class);
$mockWebDriver->method('manage')->willReturn($mockWebDriverOptions);

return $mockWebDriver;
}
}

0 comments on commit d0795ad

Please sign in to comment.