From cfff92e2897d3a83e04903756a2b80e1d646fa81 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Sinnbeck?= Date: Sat, 22 Oct 2022 21:14:33 +0200 Subject: [PATCH] major code clean up --- src/Asserts/BaseAssert.php | 47 ++++++++++++++++++++-- src/Asserts/ElementAssert.php | 10 +---- src/Asserts/FormAssert.php | 7 ---- src/Asserts/SelectAssert.php | 6 --- src/Asserts/Traits/CanGatherAttributes.php | 15 +++---- src/Asserts/Traits/InteractsWithParser.php | 3 +- src/Asserts/Traits/NormalizesData.php | 38 ----------------- src/Asserts/Traits/UsesElementAsserts.php | 44 ++------------------ src/Formatters/Normalize.php | 45 +++++++++++++++++++++ src/Macros/AssertElementMacro.php | 1 - src/ide-helper.php | 18 +++++++++ 11 files changed, 120 insertions(+), 114 deletions(-) delete mode 100644 src/Asserts/Traits/NormalizesData.php create mode 100644 src/Formatters/Normalize.php create mode 100644 src/ide-helper.php diff --git a/src/Asserts/BaseAssert.php b/src/Asserts/BaseAssert.php index 98832d2..3bcfb05 100644 --- a/src/Asserts/BaseAssert.php +++ b/src/Asserts/BaseAssert.php @@ -2,10 +2,12 @@ namespace Sinnbeck\DomAssertions\Asserts; +use Carbon\Exceptions\UnknownMethodException; +use Illuminate\Support\Str; +use Illuminate\Support\Traits\Macroable; use Sinnbeck\DomAssertions\Asserts\Traits\CanGatherAttributes; use Sinnbeck\DomAssertions\Asserts\Traits\Debugging; use Sinnbeck\DomAssertions\Asserts\Traits\InteractsWithParser; -use Sinnbeck\DomAssertions\Asserts\Traits\NormalizesData; use Sinnbeck\DomAssertions\Asserts\Traits\UsesElementAsserts; use Sinnbeck\DomAssertions\Parsers\DomParser; @@ -14,8 +16,10 @@ abstract class BaseAssert use UsesElementAsserts; use CanGatherAttributes; use InteractsWithParser; - use NormalizesData; use Debugging; + use Macroable { + __call as protected callMacro; + } public function __construct($html, $element = null) { @@ -27,4 +31,41 @@ public function __construct($html, $element = null) } } -} \ No newline at end of file + public function __call($method, $arguments) + { + if (static::hasMacro($method)) { + return $this->macroCall($method, $arguments); + } + if (Str::startsWith($method, 'has')) { + $property = Str::of($method)->after('has')->snake()->slug(); + + return $this->has($property, $arguments[0] ?? null); + } + + if (Str::startsWith($method, 'is')) { + $property = Str::of($method)->after('is')->snake()->slug(); + + return $this->is($property); + } + + if (Str::startsWith($method, 'find')) { + $property = Str::of($method)->after('find')->snake()->slug(); + + return $this->find($property, $arguments[0] ?? null); + } + + if (Str::startsWith($method, 'contains')) { + $elementName = Str::of($method)->after('contains')->camel(); + + return $this->contains($elementName, ...$arguments); + } + + if (Str::startsWith($method, 'doesntContain')) { + $elementName = Str::of($method)->after('doesntContain')->camel(); + + return $this->doesntContain($elementName, ...$arguments); + } + + throw new UnknownMethodException($method); + } +} diff --git a/src/Asserts/ElementAssert.php b/src/Asserts/ElementAssert.php index 58e3d0c..ba1ed1f 100644 --- a/src/Asserts/ElementAssert.php +++ b/src/Asserts/ElementAssert.php @@ -2,12 +2,6 @@ namespace Sinnbeck\DomAssertions\Asserts; -use Sinnbeck\DomAssertions\Asserts\Traits\CanGatherAttributes; -use Sinnbeck\DomAssertions\Asserts\Traits\Debugging; -use Sinnbeck\DomAssertions\Asserts\Traits\NormalizesData; -use Sinnbeck\DomAssertions\Asserts\Traits\UsesElementAsserts; -use Sinnbeck\DomAssertions\Asserts\Traits\InteractsWithParser; -use Sinnbeck\DomAssertions\Parsers\DomParser; - class ElementAssert extends BaseAssert -{} +{ +} diff --git a/src/Asserts/FormAssert.php b/src/Asserts/FormAssert.php index 46f12f2..55f00b5 100644 --- a/src/Asserts/FormAssert.php +++ b/src/Asserts/FormAssert.php @@ -5,12 +5,6 @@ use Illuminate\Support\Str; use Illuminate\Testing\Assert as PHPUnit; use PHPUnit\Framework\Assert; -use Sinnbeck\DomAssertions\Asserts\Traits\CanGatherAttributes; -use Sinnbeck\DomAssertions\Asserts\Traits\Debugging; -use Sinnbeck\DomAssertions\Asserts\Traits\NormalizesData; -use Sinnbeck\DomAssertions\Asserts\Traits\UsesElementAsserts; -use Sinnbeck\DomAssertions\Asserts\Traits\InteractsWithParser; -use Sinnbeck\DomAssertions\Parsers\DomParser; class FormAssert extends BaseAssert { @@ -89,5 +83,4 @@ public function findSelect($selector = 'select', $callback = null): static return $this; } - } diff --git a/src/Asserts/SelectAssert.php b/src/Asserts/SelectAssert.php index d617296..d4680ee 100644 --- a/src/Asserts/SelectAssert.php +++ b/src/Asserts/SelectAssert.php @@ -3,12 +3,6 @@ namespace Sinnbeck\DomAssertions\Asserts; use PHPUnit\Framework\Assert; -use Sinnbeck\DomAssertions\Asserts\Traits\CanGatherAttributes; -use Sinnbeck\DomAssertions\Asserts\Traits\Debugging; -use Sinnbeck\DomAssertions\Asserts\Traits\NormalizesData; -use Sinnbeck\DomAssertions\Asserts\Traits\UsesElementAsserts; -use Sinnbeck\DomAssertions\Asserts\Traits\InteractsWithParser; -use Sinnbeck\DomAssertions\Parsers\DomParser; class SelectAssert extends BaseAssert { diff --git a/src/Asserts/Traits/CanGatherAttributes.php b/src/Asserts/Traits/CanGatherAttributes.php index c569499..ca19e32 100644 --- a/src/Asserts/Traits/CanGatherAttributes.php +++ b/src/Asserts/Traits/CanGatherAttributes.php @@ -2,6 +2,8 @@ namespace Sinnbeck\DomAssertions\Asserts\Traits; +use Sinnbeck\DomAssertions\Formatters\Normalize; + trait CanGatherAttributes { public function gatherAttributes($type): void @@ -19,21 +21,16 @@ public function gatherAttributes($type): void foreach ($elements as $element) { $attributes = []; foreach ($element->attributes as $attribute) { - $attributes[$attribute->nodeName] = $this->extractAttribute($attribute); + $attributes[$attribute->nodeName] = Normalize::attributeValue($attribute->nodeName, $attribute->value); } + $extra['text'] = Normalize::attributeValue('text', $element->nodeValue); + if ($type === 'textarea') { - $extra['value'] = trim($element->nodeValue); + $extra['value'] = $extra['text']; } - $extra['text'] = trim($element->nodeValue); - $this->attributes[$type][] = $attributes + $extra; } } - - protected function extractAttribute(mixed $attribute): mixed - { - return $this->normalizeAttributeValue($attribute->nodeName, $attribute->value); - } } diff --git a/src/Asserts/Traits/InteractsWithParser.php b/src/Asserts/Traits/InteractsWithParser.php index f638996..8a5cc05 100644 --- a/src/Asserts/Traits/InteractsWithParser.php +++ b/src/Asserts/Traits/InteractsWithParser.php @@ -2,6 +2,7 @@ namespace Sinnbeck\DomAssertions\Asserts\Traits; +use Sinnbeck\DomAssertions\Formatters\Normalize; use Sinnbeck\DomAssertions\Parsers\DomParser; trait InteractsWithParser @@ -22,7 +23,7 @@ protected function getAttribute(string $attribute) return $this->getParser()->getText(); } - return $this->normalizeAttributeValue($attribute, $this->getParser()->getAttributeForRoot($attribute)); + return Normalize::attributeValue($attribute, $this->getParser()->getAttributeForRoot($attribute)); } protected function hasAttribute(string $attribute) diff --git a/src/Asserts/Traits/NormalizesData.php b/src/Asserts/Traits/NormalizesData.php deleted file mode 100644 index 8fac229..0000000 --- a/src/Asserts/Traits/NormalizesData.php +++ /dev/null @@ -1,38 +0,0 @@ - $value) { - $attributes[$attribute] = $this->normalizeAttributeValue($attribute, $value); - } - - return $attributes; - } - - protected function normalizeAttributeValue($attribute, $value): mixed - { - if ($attribute === 'class') { - return $this->normalizeClass($value); - } - - if (in_array($attribute, ['readonly', 'required']) && ! $value) { - return true; - } - - return $value; - } - - protected function normalizeClass(string $class): string - { - return Str::of($class) - ->explode(' ') - ->sort() - ->implode(' '); - } -} diff --git a/src/Asserts/Traits/UsesElementAsserts.php b/src/Asserts/Traits/UsesElementAsserts.php index b2f1f76..93329f5 100644 --- a/src/Asserts/Traits/UsesElementAsserts.php +++ b/src/Asserts/Traits/UsesElementAsserts.php @@ -2,51 +2,13 @@ namespace Sinnbeck\DomAssertions\Asserts\Traits; -use Illuminate\Support\Str; -use Illuminate\Support\Traits\Macroable; use Illuminate\Testing\Assert as PHPUnit; use PHPUnit\Framework\Assert; use Sinnbeck\DomAssertions\Asserts\ElementAssert; +use Sinnbeck\DomAssertions\Formatters\Normalize; trait UsesElementAsserts { - use Macroable { - __call as protected callMacro; - } - - public function __call(string $method, array $arguments) - { - if (static::hasMacro($method)) { - return $this->macroCall($method, $arguments); - } - if (Str::startsWith($method, 'has')) { - $property = Str::of($method)->after('has')->snake()->slug(); - $this->has($property, $arguments[0] ?? null); - } - - if (Str::startsWith($method, 'is')) { - $property = Str::of($method)->after('is')->snake()->slug(); - $this->is($property); - } - - if (Str::startsWith($method, 'find')) { - $property = Str::of($method)->after('find')->snake()->slug(); - $this->find($property, $arguments[0] ?? null); - } - - if (Str::startsWith($method, 'contains')) { - $elementName = Str::of($method)->after('contains')->camel(); - $this->contains($elementName, ...$arguments); - } - - if (Str::startsWith($method, 'doesntContain')) { - $elementName = Str::of($method)->after('doesntContain')->camel(); - $this->doesntContain($elementName, ...$arguments); - } - - return $this; - } - public function has(string $attribute, mixed $value = null): self { if (! $value) { @@ -58,7 +20,7 @@ public function has(string $attribute, mixed $value = null): self return $this; } - $value = $this->normalizeAttributeValue($attribute, $value); + $value = Normalize::attributeValue($attribute, $value); PHPUnit::assertEquals( $value, @@ -111,7 +73,7 @@ public function contains(string $elementName, $attributes = null, $count = 0): s } $this->gatherAttributes($elementName); - $attributes = $this->normalizeAttributesArray($attributes); + $attributes = Normalize::attributesArray($attributes); if ($count) { $found = collect($this->attributes[$elementName]) diff --git a/src/Formatters/Normalize.php b/src/Formatters/Normalize.php new file mode 100644 index 0000000..c2f2f06 --- /dev/null +++ b/src/Formatters/Normalize.php @@ -0,0 +1,45 @@ + $value) { + $attributes[$attribute] = self::attributeValue($attribute, $value); + } + + return $attributes; + } + + public static function attributeValue($attribute, $value): mixed + { + if ($attribute === 'class') { + return Normalize::className($value); + } + + if ($attribute === 'text') { + return trim($value); + } + + if (in_array($attribute, [ + 'readonly', + 'required', + ]) && ! $value) { + return true; + } + + return $value; + } + + protected static function className(string $class): string + { + return Str::of($class) + ->explode(' ') + ->sort() + ->implode(' '); + } +} diff --git a/src/Macros/AssertElementMacro.php b/src/Macros/AssertElementMacro.php index a2dcc97..f0dc268 100644 --- a/src/Macros/AssertElementMacro.php +++ b/src/Macros/AssertElementMacro.php @@ -34,6 +34,5 @@ public function __invoke() return $this; }; - } } diff --git a/src/ide-helper.php b/src/ide-helper.php new file mode 100644 index 0000000..e54107b --- /dev/null +++ b/src/ide-helper.php @@ -0,0 +1,18 @@ +