diff --git a/src/Client.php b/src/Client.php index 84a5d2bd..0d0cb558 100644 --- a/src/Client.php +++ b/src/Client.php @@ -39,6 +39,8 @@ use Symfony\Component\Panther\DomCrawler\Crawler as PantherCrawler; use Symfony\Component\Panther\DomCrawler\Form as PantherForm; use Symfony\Component\Panther\DomCrawler\Link as PantherLink; +use Symfony\Component\Panther\Exception\InvalidArgumentException; +use Symfony\Component\Panther\Exception\LogicException; use Symfony\Component\Panther\ProcessManager\BrowserManagerInterface; use Symfony\Component\Panther\ProcessManager\ChromeManager; use Symfony\Component\Panther\ProcessManager\FirefoxManager; @@ -138,18 +140,18 @@ public function start(): void public function getRequest(): object { - throw new \LogicException('HttpFoundation Request object is not available when using WebDriver.'); + throw new LogicException('HttpFoundation Request object is not available when using WebDriver.'); } public function getResponse(): object { - throw new \LogicException('HttpFoundation Response object is not available when using WebDriver.'); + throw new LogicException('HttpFoundation Response object is not available when using WebDriver.'); } public function followRedirects($followRedirects = true): void { if (!$followRedirects) { - throw new \InvalidArgumentException('Redirects are always followed when using WebDriver.'); + throw new InvalidArgumentException('Redirects are always followed when using WebDriver.'); } } @@ -161,7 +163,7 @@ public function isFollowingRedirects(): bool public function setMaxRedirects($maxRedirects): void { if (-1 !== $maxRedirects) { - throw new \InvalidArgumentException('There are no max redirects when using WebDriver.'); + throw new InvalidArgumentException('There are no max redirects when using WebDriver.'); } } @@ -173,28 +175,28 @@ public function getMaxRedirects(): int public function insulate($insulated = true): void { if (!$insulated) { - throw new \InvalidArgumentException('Requests are always insulated when using WebDriver.'); + throw new InvalidArgumentException('Requests are always insulated when using WebDriver.'); } } public function setServerParameters(array $server): void { - throw new \InvalidArgumentException('Server parameters cannot be set when using WebDriver.'); + throw new InvalidArgumentException('Server parameters cannot be set when using WebDriver.'); } public function setServerParameter($key, $value): void { - throw new \InvalidArgumentException('Server parameters cannot be set when using WebDriver.'); + throw new InvalidArgumentException('Server parameters cannot be set when using WebDriver.'); } public function getServerParameter($key, $default = ''): mixed { - throw new \InvalidArgumentException('Server parameters cannot be retrieved when using WebDriver.'); + throw new InvalidArgumentException('Server parameters cannot be retrieved when using WebDriver.'); } public function getHistory(): History { - throw new \LogicException('History is not available when using WebDriver.'); + throw new LogicException('History is not available when using WebDriver.'); } public function click(Link $link, array $serverParameters = []): Crawler @@ -255,18 +257,18 @@ public function refreshCrawler(): PantherCrawler public function request(string $method, string $uri, array $parameters = [], array $files = [], array $server = [], ?string $content = null, bool $changeHistory = true): PantherCrawler { if ('GET' !== $method) { - throw new \InvalidArgumentException('Only the GET method is supported when using WebDriver.'); + throw new InvalidArgumentException('Only the GET method is supported when using WebDriver.'); } if (null !== $content) { - throw new \InvalidArgumentException('Setting a content is not supported when using WebDriver.'); + throw new InvalidArgumentException('Setting a content is not supported when using WebDriver.'); } if (!$changeHistory) { - throw new \InvalidArgumentException('The history always change when using WebDriver.'); + throw new InvalidArgumentException('The history always change when using WebDriver.'); } foreach (['parameters', 'files', 'server'] as $arg) { if ([] !== $$arg) { - throw new \InvalidArgumentException(\sprintf('The parameter "$%s" is not supported when using WebDriver.', $arg)); + throw new InvalidArgumentException(\sprintf('The parameter "$%s" is not supported when using WebDriver.', $arg)); } } @@ -286,7 +288,7 @@ protected function createCrawler(): PantherCrawler protected function doRequest($request) { - throw new \LogicException('Not useful in WebDriver mode.'); + throw new LogicException('Not useful in WebDriver mode.'); } public function back(): PantherCrawler @@ -315,7 +317,7 @@ public function reload(): PantherCrawler public function followRedirect(): PantherCrawler { - throw new \LogicException('Redirects are always followed when using WebDriver.'); + throw new LogicException('Redirects are always followed when using WebDriver.'); } public function restart(): void diff --git a/src/DomCrawler/Crawler.php b/src/DomCrawler/Crawler.php index 6e603208..06204336 100644 --- a/src/DomCrawler/Crawler.php +++ b/src/DomCrawler/Crawler.php @@ -19,6 +19,8 @@ use Facebook\WebDriver\WebDriverElement; use Symfony\Component\CssSelector\CssSelectorConverter; use Symfony\Component\DomCrawler\Crawler as BaseCrawler; +use Symfony\Component\Panther\Exception\InvalidArgumentException; +use Symfony\Component\Panther\Exception\LogicException; use Symfony\Component\Panther\ExceptionThrower; /** @@ -206,12 +208,12 @@ public function nodeName(): string public function text(?string $default = null, bool $normalizeWhitespace = true): string { if (!$normalizeWhitespace) { - throw new \InvalidArgumentException('Panther only supports getting normalized text.'); + throw new InvalidArgumentException('Panther only supports getting normalized text.'); } try { return $this->getElementOrThrow()->getText(); - } catch (\InvalidArgumentException $e) { + } catch (InvalidArgumentException $e) { if (null === $default) { throw $e; } @@ -230,7 +232,7 @@ public function html(?string $default = null): string } return $this->attr('outerHTML', (string) $default); - } catch (\InvalidArgumentException $e) { + } catch (InvalidArgumentException $e) { if (null === $default) { throw $e; } @@ -299,7 +301,7 @@ public function link($method = 'get'): Link { $element = $this->getElementOrThrow(); if ('get' !== $method) { - throw new \InvalidArgumentException('Only the "get" method is supported in WebDriver mode.'); + throw new InvalidArgumentException('Only the "get" method is supported in WebDriver mode.'); } return new Link($element, $this->webDriver->getCurrentURL()); @@ -352,7 +354,7 @@ public function registerNamespace($prefix, $namespace): void public function getNode($position): ?\DOMElement { - throw new \InvalidArgumentException('The "getNode" method cannot be used in WebDriver mode. Use "getElement" instead.'); + throw new InvalidArgumentException('The "getNode" method cannot be used in WebDriver mode. Use "getElement" instead.'); } public function getElement(int $position): ?WebDriverElement @@ -423,7 +425,7 @@ private function getElementOrThrow(): WebDriverElement { $element = $this->getElement(0); if (!$element) { - throw new \InvalidArgumentException('The current node list is empty.'); + throw new InvalidArgumentException('The current node list is empty.'); } return $element; @@ -510,12 +512,12 @@ public function findElements(WebDriverBy $locator): array } /** - * @throws \LogicException If the CssSelector Component is not available + * @throws LogicException If the CssSelector Component is not available */ private function createCssSelectorConverter(): CssSelectorConverter { if (!class_exists(CssSelectorConverter::class)) { - throw new \LogicException('To filter with a CSS selector, install the CssSelector component ("composer require symfony/css-selector"). Or use filterXpath instead.'); + throw new LogicException('To filter with a CSS selector, install the CssSelector component ("composer require symfony/css-selector"). Or use filterXpath instead.'); } return new CssSelectorConverter(); diff --git a/src/DomCrawler/Field/ChoiceFormField.php b/src/DomCrawler/Field/ChoiceFormField.php index 11d4cb32..0f456059 100644 --- a/src/DomCrawler/Field/ChoiceFormField.php +++ b/src/DomCrawler/Field/ChoiceFormField.php @@ -16,6 +16,8 @@ use Facebook\WebDriver\WebDriverSelect; use Facebook\WebDriver\WebDriverSelectInterface; use Symfony\Component\DomCrawler\Field\ChoiceFormField as BaseChoiceFormField; +use Symfony\Component\Panther\Exception\InvalidArgumentException; +use Symfony\Component\Panther\Exception\LogicException; use Symfony\Component\Panther\WebDriver\WebDriverCheckbox; /** @@ -47,12 +49,12 @@ public function select($value): void /** * Ticks a checkbox. * - * @throws \LogicException When the type provided is not correct + * @throws LogicException When the type provided is not correct */ public function tick(): void { if ('checkbox' !== $type = $this->element->getAttribute('type')) { - throw new \LogicException(\sprintf('You cannot tick "%s" as it is not a checkbox (%s).', $this->element->getAttribute('name'), $type)); + throw new LogicException(\sprintf('You cannot tick "%s" as it is not a checkbox (%s).', $this->element->getAttribute('name'), $type)); } $this->setValue(true); @@ -61,12 +63,12 @@ public function tick(): void /** * Ticks a checkbox. * - * @throws \LogicException When the type provided is not correct + * @throws LogicException When the type provided is not correct */ public function untick(): void { if ('checkbox' !== $type = $this->element->getAttribute('type')) { - throw new \LogicException(\sprintf('You cannot tick "%s" as it is not a checkbox (%s).', $this->element->getAttribute('name'), $type)); + throw new LogicException(\sprintf('You cannot tick "%s" as it is not a checkbox (%s).', $this->element->getAttribute('name'), $type)); } $this->setValue(false); @@ -108,13 +110,13 @@ public function getValue(): array|string|null * * @param string|array|bool $value The value of the field * - * @throws \InvalidArgumentException When value type provided is not correct + * @throws InvalidArgumentException When value type provided is not correct */ public function setValue($value): void { if (\is_bool($value)) { if ('checkbox' !== $this->type) { - throw new \InvalidArgumentException(\sprintf('Invalid argument of type "%s"', \gettype($value))); + throw new InvalidArgumentException(\sprintf('Invalid argument of type "%s"', \gettype($value))); } if ($value) { @@ -183,18 +185,18 @@ public function disableValidation(): static /** * Initializes the form field. * - * @throws \LogicException When node type is incorrect + * @throws LogicException When node type is incorrect */ protected function initialize(): void { $tagName = $this->element->getTagName(); if ('input' !== $tagName && 'select' !== $tagName) { - throw new \LogicException(\sprintf('A ChoiceFormField can only be created from an input or select tag (%s given).', $tagName)); + throw new LogicException(\sprintf('A ChoiceFormField can only be created from an input or select tag (%s given).', $tagName)); } $type = strtolower((string) $this->element->getAttribute('type')); if ('input' === $tagName && 'checkbox' !== $type && 'radio' !== $type) { - throw new \LogicException(\sprintf('A ChoiceFormField can only be created from an input tag with a type of checkbox or radio (given type is %s).', $type)); + throw new LogicException(\sprintf('A ChoiceFormField can only be created from an input tag with a type of checkbox or radio (given type is %s).', $type)); } $this->type = 'select' === $tagName ? 'select' : $type; diff --git a/src/DomCrawler/Field/FileFormField.php b/src/DomCrawler/Field/FileFormField.php index aa1dd2f3..ac948476 100644 --- a/src/DomCrawler/Field/FileFormField.php +++ b/src/DomCrawler/Field/FileFormField.php @@ -14,6 +14,7 @@ namespace Symfony\Component\Panther\DomCrawler\Field; use Symfony\Component\DomCrawler\Field\FileFormField as BaseFileFormField; +use Symfony\Component\Panther\Exception\LogicException; /** * @author Robert Freigang @@ -61,18 +62,18 @@ public function setFilePath(string $path): void /** * Initializes the form field. * - * @throws \LogicException When node type is incorrect + * @throws LogicException When node type is incorrect */ protected function initialize(): void { $tagName = $this->element->getTagName(); if ('input' !== $tagName) { - throw new \LogicException(\sprintf('A FileFormField can only be created from an input tag (%s given).', $tagName)); + throw new LogicException(\sprintf('A FileFormField can only be created from an input tag (%s given).', $tagName)); } $type = strtolower($this->element->getAttribute('type')); if ('file' !== $type) { - throw new \LogicException(\sprintf('A FileFormField can only be created from an input tag with a type of file (given type is %s).', $type)); + throw new LogicException(\sprintf('A FileFormField can only be created from an input tag with a type of file (given type is %s).', $type)); } $value = $this->element->getAttribute('value'); diff --git a/src/DomCrawler/Field/InputFormField.php b/src/DomCrawler/Field/InputFormField.php index 0853f732..5da1f6ef 100644 --- a/src/DomCrawler/Field/InputFormField.php +++ b/src/DomCrawler/Field/InputFormField.php @@ -14,6 +14,7 @@ namespace Symfony\Component\Panther\DomCrawler\Field; use Symfony\Component\DomCrawler\Field\InputFormField as BaseInputFormField; +use Symfony\Component\Panther\Exception\LogicException; /** * @author Kévin Dunglas @@ -42,22 +43,22 @@ public function setValue($value): void /** * Initializes the form field. * - * @throws \LogicException When node type is incorrect + * @throws LogicException When node type is incorrect */ protected function initialize(): void { $tagName = $this->element->getTagName(); if ('input' !== $tagName && 'button' !== $tagName) { - throw new \LogicException(\sprintf('An InputFormField can only be created from an input or button tag (%s given).', $tagName)); + throw new LogicException(\sprintf('An InputFormField can only be created from an input or button tag (%s given).', $tagName)); } $type = strtolower((string) $this->element->getAttribute('type')); if ('checkbox' === $type) { - throw new \LogicException('Checkboxes should be instances of ChoiceFormField.'); + throw new LogicException('Checkboxes should be instances of ChoiceFormField.'); } if ('file' === $type) { - throw new \LogicException('File inputs should be instances of FileFormField.'); + throw new LogicException('File inputs should be instances of FileFormField.'); } } } diff --git a/src/DomCrawler/Field/TextareaFormField.php b/src/DomCrawler/Field/TextareaFormField.php index 481bef77..715d3b24 100644 --- a/src/DomCrawler/Field/TextareaFormField.php +++ b/src/DomCrawler/Field/TextareaFormField.php @@ -14,6 +14,7 @@ namespace Symfony\Component\Panther\DomCrawler\Field; use Symfony\Component\DomCrawler\Field\TextareaFormField as BaseTextareaFormField; +use Symfony\Component\Panther\Exception\LogicException; /** * @author Kévin Dunglas @@ -30,13 +31,13 @@ public function setValue(?string $value): void /** * Initializes the form field. * - * @throws \LogicException When node type is incorrect + * @throws LogicException When node type is incorrect */ protected function initialize(): void { $tagName = $this->element->getTagName(); if ('textarea' !== $tagName) { - throw new \LogicException(\sprintf('A TextareaFormField can only be created from a textarea tag (%s given).', $tagName)); + throw new LogicException(\sprintf('A TextareaFormField can only be created from a textarea tag (%s given).', $tagName)); } } } diff --git a/src/DomCrawler/Form.php b/src/DomCrawler/Form.php index 2150a696..7fd1dd88 100644 --- a/src/DomCrawler/Form.php +++ b/src/DomCrawler/Form.php @@ -27,6 +27,8 @@ use Symfony\Component\Panther\DomCrawler\Field\FileFormField; use Symfony\Component\Panther\DomCrawler\Field\InputFormField; use Symfony\Component\Panther\DomCrawler\Field\TextareaFormField; +use Symfony\Component\Panther\Exception\LogicException; +use Symfony\Component\Panther\Exception\RuntimeException; use Symfony\Component\Panther\ExceptionThrower; use Symfony\Component\Panther\WebDriver\WebDriverCheckbox; @@ -60,7 +62,7 @@ private function setElement(WebDriverElement $element): void try { $form = $this->webDriver->findElement(WebDriverBy::id($formId)); } catch (NoSuchElementException $e) { - throw new \LogicException(\sprintf('The selected node has an invalid form attribute (%s).', $formId)); + throw new LogicException(\sprintf('The selected node has an invalid form attribute (%s).', $formId)); } $this->element = $form; @@ -72,11 +74,11 @@ private function setElement(WebDriverElement $element): void try { $element = $element->findElement(WebDriverBy::xpath('..')); } catch (NoSuchElementException $e) { - throw new \LogicException('The selected node does not have a form ancestor.'); + throw new LogicException('The selected node does not have a form ancestor.'); } } while ('form' !== $element->getTagName()); } elseif ('form' !== $tagName = $element->getTagName()) { - throw new \LogicException(\sprintf('Unable to submit on a "%s" tag.', $tagName)); + throw new LogicException(\sprintf('Unable to submit on a "%s" tag.', $tagName)); } $this->element = $element; @@ -323,7 +325,7 @@ private function getValue(WebDriverElement $element) { if (null === $webDriverSelect = $this->getWebDriverSelect($element)) { if (!$this->webDriver instanceof JavaScriptExecutor) { - throw new \RuntimeException('To retrieve this value, the browser must support JavaScript.'); + throw new RuntimeException('To retrieve this value, the browser must support JavaScript.'); } return $this->webDriver->executeScript('return arguments[0].value', [$element]); diff --git a/src/DomCrawler/Image.php b/src/DomCrawler/Image.php index 8884696d..c88ad40e 100644 --- a/src/DomCrawler/Image.php +++ b/src/DomCrawler/Image.php @@ -15,6 +15,7 @@ use Facebook\WebDriver\WebDriverElement; use Symfony\Component\DomCrawler\Image as BaseImage; +use Symfony\Component\Panther\Exception\LogicException; use Symfony\Component\Panther\ExceptionThrower; /** @@ -29,7 +30,7 @@ final class Image extends BaseImage public function __construct(WebDriverElement $element) { if ('img' !== $tagName = $element->getTagName()) { - throw new \LogicException(\sprintf('Unable to visualize a "%s" tag.', $tagName)); + throw new LogicException(\sprintf('Unable to visualize a "%s" tag.', $tagName)); } $this->element = $element; diff --git a/src/DomCrawler/Link.php b/src/DomCrawler/Link.php index 85ff859e..78cdc328 100644 --- a/src/DomCrawler/Link.php +++ b/src/DomCrawler/Link.php @@ -15,6 +15,7 @@ use Facebook\WebDriver\WebDriverElement; use Symfony\Component\DomCrawler\Link as BaseLink; +use Symfony\Component\Panther\Exception\LogicException; use Symfony\Component\Panther\ExceptionThrower; /** @@ -30,7 +31,7 @@ public function __construct(WebDriverElement $element, string $currentUri) { $tagName = $element->getTagName(); if ('a' !== $tagName && 'area' !== $tagName && 'link' !== $tagName) { - throw new \LogicException(\sprintf('Unable to navigate from a "%s" tag.', $tagName)); + throw new LogicException(\sprintf('Unable to navigate from a "%s" tag.', $tagName)); } $this->element = $element; diff --git a/src/Exception/ExceptionInterface.php b/src/Exception/ExceptionInterface.php new file mode 100644 index 00000000..3bbe2830 --- /dev/null +++ b/src/Exception/ExceptionInterface.php @@ -0,0 +1,21 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace Symfony\Component\Panther\Exception; + +/** + * Base ExceptionInterface for the Panther component. + */ +interface ExceptionInterface extends \Throwable +{ +} diff --git a/src/Exception/InvalidArgumentException.php b/src/Exception/InvalidArgumentException.php new file mode 100644 index 00000000..b1843a07 --- /dev/null +++ b/src/Exception/InvalidArgumentException.php @@ -0,0 +1,23 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace Symfony\Component\Panther\Exception; + +/** + * Base InvalidArgumentException for Panther component. + * + * @author Oskar Stark + */ +class InvalidArgumentException extends \InvalidArgumentException implements ExceptionInterface +{ +} diff --git a/src/Exception/LogicException.php b/src/Exception/LogicException.php new file mode 100644 index 00000000..f5ff4760 --- /dev/null +++ b/src/Exception/LogicException.php @@ -0,0 +1,21 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace Symfony\Component\Panther\Exception; + +/** + * Base LogicException for Panther component. + */ +class LogicException extends \LogicException implements ExceptionInterface +{ +} diff --git a/src/Exception/RuntimeException.php b/src/Exception/RuntimeException.php new file mode 100644 index 00000000..d75d3260 --- /dev/null +++ b/src/Exception/RuntimeException.php @@ -0,0 +1,21 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace Symfony\Component\Panther\Exception; + +/** + * Base RuntimeException for Panther component. + */ +class RuntimeException extends \RuntimeException implements ExceptionInterface +{ +} diff --git a/src/PantherTestCaseTrait.php b/src/PantherTestCaseTrait.php index cc84bb76..df730cee 100644 --- a/src/PantherTestCaseTrait.php +++ b/src/PantherTestCaseTrait.php @@ -18,6 +18,7 @@ use Symfony\Component\BrowserKit\HttpBrowser as HttpBrowserClient; use Symfony\Component\HttpClient\HttpClient; use Symfony\Component\Panther\Client as PantherClient; +use Symfony\Component\Panther\Exception\RuntimeException; use Symfony\Component\Panther\ProcessManager\ChromeManager; use Symfony\Component\Panther\ProcessManager\FirefoxManager; use Symfony\Component\Panther\ProcessManager\WebServerManager; @@ -188,7 +189,7 @@ protected static function createPantherClient(array $options = [], array $kernel } else { try { self::$pantherClients[0] = self::$pantherClient = PantherClient::createChromeClient(null, $browserArguments, $managerOptions, self::$baseUri); - } catch (\RuntimeException $e) { + } catch (RuntimeException $e) { if (PantherTestCase::CHROME === $browser) { throw $e; } diff --git a/src/ProcessManager/BrowserManagerInterface.php b/src/ProcessManager/BrowserManagerInterface.php index c1ea3bfc..bf6287b1 100644 --- a/src/ProcessManager/BrowserManagerInterface.php +++ b/src/ProcessManager/BrowserManagerInterface.php @@ -14,6 +14,7 @@ namespace Symfony\Component\Panther\ProcessManager; use Facebook\WebDriver\WebDriver; +use Symfony\Component\Panther\Exception\RuntimeException; /** * A browser manager (for instance using ChromeDriver or GeckoDriver). @@ -23,7 +24,7 @@ interface BrowserManagerInterface { /** - * @throws \RuntimeException + * @throws RuntimeException */ public function start(): WebDriver; diff --git a/src/ProcessManager/ChromeManager.php b/src/ProcessManager/ChromeManager.php index 50f90565..f9d6f604 100644 --- a/src/ProcessManager/ChromeManager.php +++ b/src/ProcessManager/ChromeManager.php @@ -17,6 +17,7 @@ use Facebook\WebDriver\Remote\DesiredCapabilities; use Facebook\WebDriver\Remote\RemoteWebDriver; use Facebook\WebDriver\WebDriver; +use Symfony\Component\Panther\Exception\RuntimeException; use Symfony\Component\Process\ExecutableFinder; use Symfony\Component\Process\Process; @@ -32,7 +33,7 @@ final class ChromeManager implements BrowserManagerInterface private array $options; /** - * @throws \RuntimeException + * @throws RuntimeException */ public function __construct(?string $chromeDriverBinary = null, ?array $arguments = null, array $options = []) { @@ -42,7 +43,7 @@ public function __construct(?string $chromeDriverBinary = null, ?array $argument } /** - * @throws \RuntimeException + * @throws RuntimeException */ public function start(): WebDriver { @@ -81,7 +82,7 @@ public function quit(): void } /** - * @throws \RuntimeException + * @throws RuntimeException */ private function findChromeDriverBinary(): string { @@ -89,7 +90,7 @@ private function findChromeDriverBinary(): string return $binary; } - throw new \RuntimeException('"chromedriver" binary not found. Install it using the package manager of your operating system or by running "composer require --dev dbrekelmans/bdi && vendor/bin/bdi detect drivers".'); + throw new RuntimeException('"chromedriver" binary not found. Install it using the package manager of your operating system or by running "composer require --dev dbrekelmans/bdi && vendor/bin/bdi detect drivers".'); } private function getDefaultArguments(): array diff --git a/src/ProcessManager/FirefoxManager.php b/src/ProcessManager/FirefoxManager.php index d9e31788..d7465da1 100644 --- a/src/ProcessManager/FirefoxManager.php +++ b/src/ProcessManager/FirefoxManager.php @@ -16,6 +16,7 @@ use Facebook\WebDriver\Remote\DesiredCapabilities; use Facebook\WebDriver\Remote\RemoteWebDriver; use Facebook\WebDriver\WebDriver; +use Symfony\Component\Panther\Exception\RuntimeException; use Symfony\Component\Process\ExecutableFinder; use Symfony\Component\Process\Process; @@ -31,7 +32,7 @@ final class FirefoxManager implements BrowserManagerInterface private array $options; /** - * @throws \RuntimeException + * @throws RuntimeException */ public function __construct(?string $geckodriverBinary = null, ?array $arguments = null, array $options = []) { @@ -41,7 +42,7 @@ public function __construct(?string $geckodriverBinary = null, ?array $arguments } /** - * @throws \RuntimeException + * @throws RuntimeException */ public function start(): WebDriver { @@ -76,7 +77,7 @@ public function quit(): void } /** - * @throws \RuntimeException + * @throws RuntimeException */ private function findGeckodriverBinary(): string { @@ -84,7 +85,7 @@ private function findGeckodriverBinary(): string return $binary; } - throw new \RuntimeException('"geckodriver" binary not found. Install it using the package manager of your operating system or by running "composer require --dev dbrekelmans/bdi && vendor/bin/bdi detect drivers".'); + throw new RuntimeException('"geckodriver" binary not found. Install it using the package manager of your operating system or by running "composer require --dev dbrekelmans/bdi && vendor/bin/bdi detect drivers".'); } private function getDefaultArguments(): array diff --git a/src/ProcessManager/WebServerManager.php b/src/ProcessManager/WebServerManager.php index df18ef37..9000a0ec 100644 --- a/src/ProcessManager/WebServerManager.php +++ b/src/ProcessManager/WebServerManager.php @@ -13,6 +13,7 @@ namespace Symfony\Component\Panther\ProcessManager; +use Symfony\Component\Panther\Exception\RuntimeException; use Symfony\Component\Process\PhpExecutableFinder; use Symfony\Component\Process\Process; @@ -29,7 +30,7 @@ final class WebServerManager private Process $process; /** - * @throws \RuntimeException + * @throws RuntimeException */ public function __construct(string $documentRoot, string $hostname, int $port, string $router = '', string $readinessPath = '', ?array $env = null) { @@ -39,7 +40,7 @@ public function __construct(string $documentRoot, string $hostname, int $port, s $finder = new PhpExecutableFinder(); if (false === $binary = $finder->find(false)) { - throw new \RuntimeException('Unable to find the PHP binary.'); + throw new RuntimeException('Unable to find the PHP binary.'); } if (isset($_SERVER['PANTHER_APP_ENV'])) { @@ -85,7 +86,7 @@ public function start(): void } /** - * @throws \RuntimeException + * @throws RuntimeException */ public function quit(): void { diff --git a/src/ProcessManager/WebServerReadinessProbeTrait.php b/src/ProcessManager/WebServerReadinessProbeTrait.php index f674b64f..668a0a8d 100644 --- a/src/ProcessManager/WebServerReadinessProbeTrait.php +++ b/src/ProcessManager/WebServerReadinessProbeTrait.php @@ -14,6 +14,7 @@ namespace Symfony\Component\Panther\ProcessManager; use Symfony\Component\HttpClient\HttpClient; +use Symfony\Component\Panther\Exception\RuntimeException; use Symfony\Component\Process\Process; use Symfony\Contracts\HttpClient\Exception\ExceptionInterface; @@ -25,7 +26,7 @@ trait WebServerReadinessProbeTrait { /** - * @throws \RuntimeException + * @throws RuntimeException */ private function checkPortAvailable(string $hostname, int $port, bool $throw = true): void { @@ -36,7 +37,7 @@ private function checkPortAvailable(string $hostname, int $port, bool $throw = t if (\is_resource($resource)) { fclose($resource); if ($throw) { - throw new \RuntimeException(\sprintf('The port %d is already in use.', $port)); + throw new RuntimeException(\sprintf('The port %d is already in use.', $port)); } } } @@ -50,12 +51,12 @@ public function waitUntilReady(Process $process, string $url, string $service, b while (true) { $status = $process->getStatus(); if (Process::STATUS_TERMINATED === $status) { - throw new \RuntimeException(\sprintf('Could not start %s. Exit code: %d (%s). Error output: %s', $service, $process->getExitCode(), $process->getExitCodeText(), $process->getErrorOutput())); + throw new RuntimeException(\sprintf('Could not start %s. Exit code: %d (%s). Error output: %s', $service, $process->getExitCode(), $process->getExitCodeText(), $process->getErrorOutput())); } if (Process::STATUS_STARTED !== $status) { if (microtime(true) - $start >= $timeout) { - throw new \RuntimeException("Could not start $service (or it crashed) after $timeout seconds."); + throw new RuntimeException("Could not start $service (or it crashed) after $timeout seconds."); } usleep(1000); @@ -79,7 +80,7 @@ public function waitUntilReady(Process $process, string $url, string $service, b } else { $message = "Status code: $statusCode"; } - throw new \RuntimeException("Could not connect to $service after $timeout seconds ($message)."); + throw new RuntimeException("Could not connect to $service after $timeout seconds ($message)."); } usleep(1000); diff --git a/src/WebDriver/WebDriverMouse.php b/src/WebDriver/WebDriverMouse.php index d4bd8054..f9cd7a58 100644 --- a/src/WebDriver/WebDriverMouse.php +++ b/src/WebDriver/WebDriverMouse.php @@ -17,6 +17,7 @@ use Facebook\WebDriver\Internal\WebDriverLocatable; use Facebook\WebDriver\WebDriverMouse as BaseWebDriverMouse; use Symfony\Component\Panther\Client; +use Symfony\Component\Panther\Exception\RuntimeException; /** * @author Dany Maillard @@ -109,7 +110,7 @@ private function toCoordinates($cssSelector): WebDriverCoordinates $element = $this->client->getCrawler()->filter($cssSelector)->getElement(0); if (!$element instanceof WebDriverLocatable) { - throw new \RuntimeException(\sprintf('The element of "%s" CSS selector does not implement "%s".', $cssSelector, WebDriverLocatable::class)); + throw new RuntimeException(\sprintf('The element of "%s" CSS selector does not implement "%s".', $cssSelector, WebDriverLocatable::class)); } return $element->getCoordinates(); diff --git a/src/WebTestAssertionsTrait.php b/src/WebTestAssertionsTrait.php index 6ebbd3a7..5e8098be 100644 --- a/src/WebTestAssertionsTrait.php +++ b/src/WebTestAssertionsTrait.php @@ -18,6 +18,7 @@ use Symfony\Bundle\FrameworkBundle\Test\WebTestAssertionsTrait as BaseWebTestAssertionsTrait; use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException; use Symfony\Component\Panther\Client as PantherClient; +use Symfony\Component\Panther\Exception\LogicException; /** * Tweaks Symfony's WebTestAssertionsTrait to be compatible with Panther. @@ -258,7 +259,7 @@ private static function findElement(string $locator): WebDriverElement { $client = self::getClient(); if (!$client instanceof PantherClient) { - throw new \LogicException(\sprintf('Using a client that is not an instance of "%s" is not supported.', PantherClient::class)); + throw new LogicException(\sprintf('Using a client that is not an instance of "%s" is not supported.', PantherClient::class)); } $by = $client::createWebDriverByFromLocator($locator); @@ -285,9 +286,9 @@ protected static function createClient(array $options = [], array $server = []): $client = $kernel->getContainer()->get('test.client'); } catch (ServiceNotFoundException $e) { if (class_exists(KernelBrowser::class)) { - throw new \LogicException('You cannot create the client used in functional tests if the "framework.test" config is not set to true.'); + throw new LogicException('You cannot create the client used in functional tests if the "framework.test" config is not set to true.'); } - throw new \LogicException('You cannot create the client used in functional tests if the BrowserKit component is not available. Try running "composer require symfony/browser-kit"'); + throw new LogicException('You cannot create the client used in functional tests if the BrowserKit component is not available. Try running "composer require symfony/browser-kit"'); } $client->setServerParameters($server); diff --git a/tests/ClientTest.php b/tests/ClientTest.php index bad02d69..844ed73b 100644 --- a/tests/ClientTest.php +++ b/tests/ClientTest.php @@ -26,6 +26,7 @@ use Symfony\Component\Panther\Client; use Symfony\Component\Panther\Cookie\CookieJar; use Symfony\Component\Panther\DomCrawler\Crawler; +use Symfony\Component\Panther\Exception\LogicException; use Symfony\Component\Panther\PantherTestCase; use Symfony\Component\Panther\ProcessManager\ChromeManager; use Symfony\Contracts\HttpClient\HttpClientInterface; @@ -197,7 +198,7 @@ public function testExecuteScript(): void public function testExecuteScriptLogicExceptionWhenDriverIsNotStartedYet(): void { - $this->expectException(\LogicException::class); + $this->expectException(LogicException::class); $client = Client::createChromeClient(); $client->executeScript('return document.querySelector(arguments[0]).innerText;', ['.p-1']); } @@ -436,7 +437,7 @@ public function testBrowserProvider(callable $clientFactory): void public function testGetHistory(): void { - $this->expectException(\LogicException::class); + $this->expectException(LogicException::class); $this->expectExceptionMessage('History is not available when using WebDriver.'); self::createPantherClient()->getHistory(); diff --git a/tests/DomCrawler/CrawlerTest.php b/tests/DomCrawler/CrawlerTest.php index 42d559c7..94b09d4a 100644 --- a/tests/DomCrawler/CrawlerTest.php +++ b/tests/DomCrawler/CrawlerTest.php @@ -19,6 +19,7 @@ use Symfony\Component\Panther\Client as PantherClient; use Symfony\Component\Panther\DomCrawler\Image; use Symfony\Component\Panther\DomCrawler\Link; +use Symfony\Component\Panther\Exception\InvalidArgumentException; use Symfony\Component\Panther\Tests\TestCase; /** @@ -418,7 +419,7 @@ public function testNormalizeText(callable $clientFactory, string $clientClass): public function testDoNotNormalizeText(): void { - $this->expectException(\InvalidArgumentException::class); + $this->expectException(InvalidArgumentException::class); self::createPantherClient()->request('GET', self::$baseUri.'/normalize.html')->filter('#normalize')->text(null, false); } diff --git a/tests/ProcessManager/ChromeManagerTest.php b/tests/ProcessManager/ChromeManagerTest.php index 471762e5..5d89128a 100644 --- a/tests/ProcessManager/ChromeManagerTest.php +++ b/tests/ProcessManager/ChromeManagerTest.php @@ -14,6 +14,7 @@ namespace Symfony\Component\Panther\Tests\ProcessManager; use PHPUnit\Framework\TestCase; +use Symfony\Component\Panther\Exception\RuntimeException; use Symfony\Component\Panther\ProcessManager\ChromeManager; /** @@ -31,7 +32,7 @@ public function testRun(): void public function testAlreadyRunning(): void { - $this->expectException(\RuntimeException::class); + $this->expectException(RuntimeException::class); $this->expectExceptionMessage('The port 9515 is already in use.'); $driver1 = new ChromeManager(); diff --git a/tests/ProcessManager/WebServerManagerTest.php b/tests/ProcessManager/WebServerManagerTest.php index 41d0e8cb..6f78a778 100644 --- a/tests/ProcessManager/WebServerManagerTest.php +++ b/tests/ProcessManager/WebServerManagerTest.php @@ -13,6 +13,7 @@ namespace Symfony\Component\Panther\Tests\ProcessManager; +use Symfony\Component\Panther\Exception\RuntimeException; use Symfony\Component\Panther\ProcessManager\WebServerManager; use Symfony\Component\Panther\Tests\TestCase; @@ -32,7 +33,7 @@ public function testRun(): void public function testAlreadyRunning(): void { - $this->expectException(\RuntimeException::class); + $this->expectException(RuntimeException::class); $this->expectExceptionMessage('The port 1234 is already in use.'); $server1 = new WebServerManager(__DIR__.'/../fixtures/', '127.0.0.1', 1234); @@ -77,7 +78,7 @@ public function testPassPantherAppEnv(): void public function testInvalidDocumentRoot(): void { - $this->expectException(\RuntimeException::class); + $this->expectException(RuntimeException::class); $this->expectExceptionMessageMatches('#/not-exists#'); $server = new WebServerManager('/not-exists', '127.0.0.1', 1234);