Skip to content

Commit 167c497

Browse files
authored
Use own exception hierarchy (#642)
1 parent 6c44d86 commit 167c497

25 files changed

+185
-74
lines changed

src/Client.php

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
use Symfony\Component\Panther\DomCrawler\Crawler as PantherCrawler;
4040
use Symfony\Component\Panther\DomCrawler\Form as PantherForm;
4141
use Symfony\Component\Panther\DomCrawler\Link as PantherLink;
42+
use Symfony\Component\Panther\Exception\InvalidArgumentException;
43+
use Symfony\Component\Panther\Exception\LogicException;
4244
use Symfony\Component\Panther\ProcessManager\BrowserManagerInterface;
4345
use Symfony\Component\Panther\ProcessManager\ChromeManager;
4446
use Symfony\Component\Panther\ProcessManager\FirefoxManager;
@@ -138,18 +140,18 @@ public function start(): void
138140

139141
public function getRequest(): object
140142
{
141-
throw new \LogicException('HttpFoundation Request object is not available when using WebDriver.');
143+
throw new LogicException('HttpFoundation Request object is not available when using WebDriver.');
142144
}
143145

144146
public function getResponse(): object
145147
{
146-
throw new \LogicException('HttpFoundation Response object is not available when using WebDriver.');
148+
throw new LogicException('HttpFoundation Response object is not available when using WebDriver.');
147149
}
148150

149151
public function followRedirects($followRedirects = true): void
150152
{
151153
if (!$followRedirects) {
152-
throw new \InvalidArgumentException('Redirects are always followed when using WebDriver.');
154+
throw new InvalidArgumentException('Redirects are always followed when using WebDriver.');
153155
}
154156
}
155157

@@ -161,7 +163,7 @@ public function isFollowingRedirects(): bool
161163
public function setMaxRedirects($maxRedirects): void
162164
{
163165
if (-1 !== $maxRedirects) {
164-
throw new \InvalidArgumentException('There are no max redirects when using WebDriver.');
166+
throw new InvalidArgumentException('There are no max redirects when using WebDriver.');
165167
}
166168
}
167169

@@ -173,28 +175,28 @@ public function getMaxRedirects(): int
173175
public function insulate($insulated = true): void
174176
{
175177
if (!$insulated) {
176-
throw new \InvalidArgumentException('Requests are always insulated when using WebDriver.');
178+
throw new InvalidArgumentException('Requests are always insulated when using WebDriver.');
177179
}
178180
}
179181

180182
public function setServerParameters(array $server): void
181183
{
182-
throw new \InvalidArgumentException('Server parameters cannot be set when using WebDriver.');
184+
throw new InvalidArgumentException('Server parameters cannot be set when using WebDriver.');
183185
}
184186

185187
public function setServerParameter($key, $value): void
186188
{
187-
throw new \InvalidArgumentException('Server parameters cannot be set when using WebDriver.');
189+
throw new InvalidArgumentException('Server parameters cannot be set when using WebDriver.');
188190
}
189191

190192
public function getServerParameter($key, $default = ''): mixed
191193
{
192-
throw new \InvalidArgumentException('Server parameters cannot be retrieved when using WebDriver.');
194+
throw new InvalidArgumentException('Server parameters cannot be retrieved when using WebDriver.');
193195
}
194196

195197
public function getHistory(): History
196198
{
197-
throw new \LogicException('History is not available when using WebDriver.');
199+
throw new LogicException('History is not available when using WebDriver.');
198200
}
199201

200202
public function click(Link $link, array $serverParameters = []): Crawler
@@ -255,18 +257,18 @@ public function refreshCrawler(): PantherCrawler
255257
public function request(string $method, string $uri, array $parameters = [], array $files = [], array $server = [], ?string $content = null, bool $changeHistory = true): PantherCrawler
256258
{
257259
if ('GET' !== $method) {
258-
throw new \InvalidArgumentException('Only the GET method is supported when using WebDriver.');
260+
throw new InvalidArgumentException('Only the GET method is supported when using WebDriver.');
259261
}
260262
if (null !== $content) {
261-
throw new \InvalidArgumentException('Setting a content is not supported when using WebDriver.');
263+
throw new InvalidArgumentException('Setting a content is not supported when using WebDriver.');
262264
}
263265
if (!$changeHistory) {
264-
throw new \InvalidArgumentException('The history always change when using WebDriver.');
266+
throw new InvalidArgumentException('The history always change when using WebDriver.');
265267
}
266268

267269
foreach (['parameters', 'files', 'server'] as $arg) {
268270
if ([] !== $$arg) {
269-
throw new \InvalidArgumentException(\sprintf('The parameter "$%s" is not supported when using WebDriver.', $arg));
271+
throw new InvalidArgumentException(\sprintf('The parameter "$%s" is not supported when using WebDriver.', $arg));
270272
}
271273
}
272274

@@ -286,7 +288,7 @@ protected function createCrawler(): PantherCrawler
286288

287289
protected function doRequest($request)
288290
{
289-
throw new \LogicException('Not useful in WebDriver mode.');
291+
throw new LogicException('Not useful in WebDriver mode.');
290292
}
291293

292294
public function back(): PantherCrawler
@@ -315,7 +317,7 @@ public function reload(): PantherCrawler
315317

316318
public function followRedirect(): PantherCrawler
317319
{
318-
throw new \LogicException('Redirects are always followed when using WebDriver.');
320+
throw new LogicException('Redirects are always followed when using WebDriver.');
319321
}
320322

321323
public function restart(): void

src/DomCrawler/Crawler.php

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
use Facebook\WebDriver\WebDriverElement;
2020
use Symfony\Component\CssSelector\CssSelectorConverter;
2121
use Symfony\Component\DomCrawler\Crawler as BaseCrawler;
22+
use Symfony\Component\Panther\Exception\InvalidArgumentException;
23+
use Symfony\Component\Panther\Exception\LogicException;
2224
use Symfony\Component\Panther\ExceptionThrower;
2325

2426
/**
@@ -206,12 +208,12 @@ public function nodeName(): string
206208
public function text(?string $default = null, bool $normalizeWhitespace = true): string
207209
{
208210
if (!$normalizeWhitespace) {
209-
throw new \InvalidArgumentException('Panther only supports getting normalized text.');
211+
throw new InvalidArgumentException('Panther only supports getting normalized text.');
210212
}
211213

212214
try {
213215
return $this->getElementOrThrow()->getText();
214-
} catch (\InvalidArgumentException $e) {
216+
} catch (InvalidArgumentException $e) {
215217
if (null === $default) {
216218
throw $e;
217219
}
@@ -230,7 +232,7 @@ public function html(?string $default = null): string
230232
}
231233

232234
return $this->attr('outerHTML', (string) $default);
233-
} catch (\InvalidArgumentException $e) {
235+
} catch (InvalidArgumentException $e) {
234236
if (null === $default) {
235237
throw $e;
236238
}
@@ -299,7 +301,7 @@ public function link($method = 'get'): Link
299301
{
300302
$element = $this->getElementOrThrow();
301303
if ('get' !== $method) {
302-
throw new \InvalidArgumentException('Only the "get" method is supported in WebDriver mode.');
304+
throw new InvalidArgumentException('Only the "get" method is supported in WebDriver mode.');
303305
}
304306

305307
return new Link($element, $this->webDriver->getCurrentURL());
@@ -352,7 +354,7 @@ public function registerNamespace($prefix, $namespace): void
352354

353355
public function getNode($position): ?\DOMElement
354356
{
355-
throw new \InvalidArgumentException('The "getNode" method cannot be used in WebDriver mode. Use "getElement" instead.');
357+
throw new InvalidArgumentException('The "getNode" method cannot be used in WebDriver mode. Use "getElement" instead.');
356358
}
357359

358360
public function getElement(int $position): ?WebDriverElement
@@ -423,7 +425,7 @@ private function getElementOrThrow(): WebDriverElement
423425
{
424426
$element = $this->getElement(0);
425427
if (!$element) {
426-
throw new \InvalidArgumentException('The current node list is empty.');
428+
throw new InvalidArgumentException('The current node list is empty.');
427429
}
428430

429431
return $element;
@@ -510,12 +512,12 @@ public function findElements(WebDriverBy $locator): array
510512
}
511513

512514
/**
513-
* @throws \LogicException If the CssSelector Component is not available
515+
* @throws LogicException If the CssSelector Component is not available
514516
*/
515517
private function createCssSelectorConverter(): CssSelectorConverter
516518
{
517519
if (!class_exists(CssSelectorConverter::class)) {
518-
throw new \LogicException('To filter with a CSS selector, install the CssSelector component ("composer require symfony/css-selector"). Or use filterXpath instead.');
520+
throw new LogicException('To filter with a CSS selector, install the CssSelector component ("composer require symfony/css-selector"). Or use filterXpath instead.');
519521
}
520522

521523
return new CssSelectorConverter();

src/DomCrawler/Field/ChoiceFormField.php

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
use Facebook\WebDriver\WebDriverSelect;
1717
use Facebook\WebDriver\WebDriverSelectInterface;
1818
use Symfony\Component\DomCrawler\Field\ChoiceFormField as BaseChoiceFormField;
19+
use Symfony\Component\Panther\Exception\InvalidArgumentException;
20+
use Symfony\Component\Panther\Exception\LogicException;
1921
use Symfony\Component\Panther\WebDriver\WebDriverCheckbox;
2022

2123
/**
@@ -47,12 +49,12 @@ public function select($value): void
4749
/**
4850
* Ticks a checkbox.
4951
*
50-
* @throws \LogicException When the type provided is not correct
52+
* @throws LogicException When the type provided is not correct
5153
*/
5254
public function tick(): void
5355
{
5456
if ('checkbox' !== $type = $this->element->getAttribute('type')) {
55-
throw new \LogicException(\sprintf('You cannot tick "%s" as it is not a checkbox (%s).', $this->element->getAttribute('name'), $type));
57+
throw new LogicException(\sprintf('You cannot tick "%s" as it is not a checkbox (%s).', $this->element->getAttribute('name'), $type));
5658
}
5759

5860
$this->setValue(true);
@@ -61,12 +63,12 @@ public function tick(): void
6163
/**
6264
* Ticks a checkbox.
6365
*
64-
* @throws \LogicException When the type provided is not correct
66+
* @throws LogicException When the type provided is not correct
6567
*/
6668
public function untick(): void
6769
{
6870
if ('checkbox' !== $type = $this->element->getAttribute('type')) {
69-
throw new \LogicException(\sprintf('You cannot tick "%s" as it is not a checkbox (%s).', $this->element->getAttribute('name'), $type));
71+
throw new LogicException(\sprintf('You cannot tick "%s" as it is not a checkbox (%s).', $this->element->getAttribute('name'), $type));
7072
}
7173

7274
$this->setValue(false);
@@ -108,13 +110,13 @@ public function getValue(): array|string|null
108110
*
109111
* @param string|array|bool $value The value of the field
110112
*
111-
* @throws \InvalidArgumentException When value type provided is not correct
113+
* @throws InvalidArgumentException When value type provided is not correct
112114
*/
113115
public function setValue($value): void
114116
{
115117
if (\is_bool($value)) {
116118
if ('checkbox' !== $this->type) {
117-
throw new \InvalidArgumentException(\sprintf('Invalid argument of type "%s"', \gettype($value)));
119+
throw new InvalidArgumentException(\sprintf('Invalid argument of type "%s"', \gettype($value)));
118120
}
119121

120122
if ($value) {
@@ -183,18 +185,18 @@ public function disableValidation(): static
183185
/**
184186
* Initializes the form field.
185187
*
186-
* @throws \LogicException When node type is incorrect
188+
* @throws LogicException When node type is incorrect
187189
*/
188190
protected function initialize(): void
189191
{
190192
$tagName = $this->element->getTagName();
191193
if ('input' !== $tagName && 'select' !== $tagName) {
192-
throw new \LogicException(\sprintf('A ChoiceFormField can only be created from an input or select tag (%s given).', $tagName));
194+
throw new LogicException(\sprintf('A ChoiceFormField can only be created from an input or select tag (%s given).', $tagName));
193195
}
194196

195197
$type = strtolower((string) $this->element->getAttribute('type'));
196198
if ('input' === $tagName && 'checkbox' !== $type && 'radio' !== $type) {
197-
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));
199+
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));
198200
}
199201

200202
$this->type = 'select' === $tagName ? 'select' : $type;

src/DomCrawler/Field/FileFormField.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
namespace Symfony\Component\Panther\DomCrawler\Field;
1515

1616
use Symfony\Component\DomCrawler\Field\FileFormField as BaseFileFormField;
17+
use Symfony\Component\Panther\Exception\LogicException;
1718

1819
/**
1920
* @author Robert Freigang <[email protected]>
@@ -61,18 +62,18 @@ public function setFilePath(string $path): void
6162
/**
6263
* Initializes the form field.
6364
*
64-
* @throws \LogicException When node type is incorrect
65+
* @throws LogicException When node type is incorrect
6566
*/
6667
protected function initialize(): void
6768
{
6869
$tagName = $this->element->getTagName();
6970
if ('input' !== $tagName) {
70-
throw new \LogicException(\sprintf('A FileFormField can only be created from an input tag (%s given).', $tagName));
71+
throw new LogicException(\sprintf('A FileFormField can only be created from an input tag (%s given).', $tagName));
7172
}
7273

7374
$type = strtolower($this->element->getAttribute('type'));
7475
if ('file' !== $type) {
75-
throw new \LogicException(\sprintf('A FileFormField can only be created from an input tag with a type of file (given type is %s).', $type));
76+
throw new LogicException(\sprintf('A FileFormField can only be created from an input tag with a type of file (given type is %s).', $type));
7677
}
7778

7879
$value = $this->element->getAttribute('value');

src/DomCrawler/Field/InputFormField.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
namespace Symfony\Component\Panther\DomCrawler\Field;
1515

1616
use Symfony\Component\DomCrawler\Field\InputFormField as BaseInputFormField;
17+
use Symfony\Component\Panther\Exception\LogicException;
1718

1819
/**
1920
* @author Kévin Dunglas <[email protected]>
@@ -42,22 +43,22 @@ public function setValue($value): void
4243
/**
4344
* Initializes the form field.
4445
*
45-
* @throws \LogicException When node type is incorrect
46+
* @throws LogicException When node type is incorrect
4647
*/
4748
protected function initialize(): void
4849
{
4950
$tagName = $this->element->getTagName();
5051
if ('input' !== $tagName && 'button' !== $tagName) {
51-
throw new \LogicException(\sprintf('An InputFormField can only be created from an input or button tag (%s given).', $tagName));
52+
throw new LogicException(\sprintf('An InputFormField can only be created from an input or button tag (%s given).', $tagName));
5253
}
5354

5455
$type = strtolower((string) $this->element->getAttribute('type'));
5556
if ('checkbox' === $type) {
56-
throw new \LogicException('Checkboxes should be instances of ChoiceFormField.');
57+
throw new LogicException('Checkboxes should be instances of ChoiceFormField.');
5758
}
5859

5960
if ('file' === $type) {
60-
throw new \LogicException('File inputs should be instances of FileFormField.');
61+
throw new LogicException('File inputs should be instances of FileFormField.');
6162
}
6263
}
6364
}

src/DomCrawler/Field/TextareaFormField.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
namespace Symfony\Component\Panther\DomCrawler\Field;
1515

1616
use Symfony\Component\DomCrawler\Field\TextareaFormField as BaseTextareaFormField;
17+
use Symfony\Component\Panther\Exception\LogicException;
1718

1819
/**
1920
* @author Kévin Dunglas <[email protected]>
@@ -30,13 +31,13 @@ public function setValue(?string $value): void
3031
/**
3132
* Initializes the form field.
3233
*
33-
* @throws \LogicException When node type is incorrect
34+
* @throws LogicException When node type is incorrect
3435
*/
3536
protected function initialize(): void
3637
{
3738
$tagName = $this->element->getTagName();
3839
if ('textarea' !== $tagName) {
39-
throw new \LogicException(\sprintf('A TextareaFormField can only be created from a textarea tag (%s given).', $tagName));
40+
throw new LogicException(\sprintf('A TextareaFormField can only be created from a textarea tag (%s given).', $tagName));
4041
}
4142
}
4243
}

src/DomCrawler/Form.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
use Symfony\Component\Panther\DomCrawler\Field\FileFormField;
2828
use Symfony\Component\Panther\DomCrawler\Field\InputFormField;
2929
use Symfony\Component\Panther\DomCrawler\Field\TextareaFormField;
30+
use Symfony\Component\Panther\Exception\LogicException;
31+
use Symfony\Component\Panther\Exception\RuntimeException;
3032
use Symfony\Component\Panther\ExceptionThrower;
3133
use Symfony\Component\Panther\WebDriver\WebDriverCheckbox;
3234

@@ -60,7 +62,7 @@ private function setElement(WebDriverElement $element): void
6062
try {
6163
$form = $this->webDriver->findElement(WebDriverBy::id($formId));
6264
} catch (NoSuchElementException $e) {
63-
throw new \LogicException(\sprintf('The selected node has an invalid form attribute (%s).', $formId));
65+
throw new LogicException(\sprintf('The selected node has an invalid form attribute (%s).', $formId));
6466
}
6567

6668
$this->element = $form;
@@ -72,11 +74,11 @@ private function setElement(WebDriverElement $element): void
7274
try {
7375
$element = $element->findElement(WebDriverBy::xpath('..'));
7476
} catch (NoSuchElementException $e) {
75-
throw new \LogicException('The selected node does not have a form ancestor.');
77+
throw new LogicException('The selected node does not have a form ancestor.');
7678
}
7779
} while ('form' !== $element->getTagName());
7880
} elseif ('form' !== $tagName = $element->getTagName()) {
79-
throw new \LogicException(\sprintf('Unable to submit on a "%s" tag.', $tagName));
81+
throw new LogicException(\sprintf('Unable to submit on a "%s" tag.', $tagName));
8082
}
8183

8284
$this->element = $element;
@@ -323,7 +325,7 @@ private function getValue(WebDriverElement $element)
323325
{
324326
if (null === $webDriverSelect = $this->getWebDriverSelect($element)) {
325327
if (!$this->webDriver instanceof JavaScriptExecutor) {
326-
throw new \RuntimeException('To retrieve this value, the browser must support JavaScript.');
328+
throw new RuntimeException('To retrieve this value, the browser must support JavaScript.');
327329
}
328330

329331
return $this->webDriver->executeScript('return arguments[0].value', [$element]);

0 commit comments

Comments
 (0)