Skip to content

Commit

Permalink
major code clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
sinnbeck committed Oct 22, 2022
1 parent a069330 commit cfff92e
Show file tree
Hide file tree
Showing 11 changed files with 120 additions and 114 deletions.
47 changes: 44 additions & 3 deletions src/Asserts/BaseAssert.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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)
{
Expand All @@ -27,4 +31,41 @@ public function __construct($html, $element = null)
}
}

}
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);
}
}
10 changes: 2 additions & 8 deletions src/Asserts/ElementAssert.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{}
{
}
7 changes: 0 additions & 7 deletions src/Asserts/FormAssert.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -89,5 +83,4 @@ public function findSelect($selector = 'select', $callback = null): static

return $this;
}

}
6 changes: 0 additions & 6 deletions src/Asserts/SelectAssert.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
15 changes: 6 additions & 9 deletions src/Asserts/Traits/CanGatherAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Sinnbeck\DomAssertions\Asserts\Traits;

use Sinnbeck\DomAssertions\Formatters\Normalize;

trait CanGatherAttributes
{
public function gatherAttributes($type): void
Expand All @@ -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);
}
}
3 changes: 2 additions & 1 deletion src/Asserts/Traits/InteractsWithParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Sinnbeck\DomAssertions\Asserts\Traits;

use Sinnbeck\DomAssertions\Formatters\Normalize;
use Sinnbeck\DomAssertions\Parsers\DomParser;

trait InteractsWithParser
Expand All @@ -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)
Expand Down
38 changes: 0 additions & 38 deletions src/Asserts/Traits/NormalizesData.php

This file was deleted.

44 changes: 3 additions & 41 deletions src/Asserts/Traits/UsesElementAsserts.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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,
Expand Down Expand Up @@ -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])
Expand Down
45 changes: 45 additions & 0 deletions src/Formatters/Normalize.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace Sinnbeck\DomAssertions\Formatters;

use Illuminate\Support\Str;

class Normalize
{
public static function attributesArray(array $attributes): array
{
foreach ($attributes as $attribute => $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(' ');
}
}
1 change: 0 additions & 1 deletion src/Macros/AssertElementMacro.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,5 @@ public function __invoke()

return $this;
};

}
}
18 changes: 18 additions & 0 deletions src/ide-helper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Illuminate\Testing {
class TestResponse
{
public function assertElement($selector = 'body', $callback = null)
{
/** @var \Illuminate\Testing\TestResponse $instance */
return $instance;
}

public function assertForm($selector = 'form', $callback = null)
{
/** @var \Illuminate\Testing\TestResponse $instance */
return $instance;
}
}
}

0 comments on commit cfff92e

Please sign in to comment.