Skip to content

Commit

Permalink
clean up classes
Browse files Browse the repository at this point in the history
  • Loading branch information
sinnbeck committed Oct 21, 2022
1 parent 52d16de commit a069330
Show file tree
Hide file tree
Showing 12 changed files with 57 additions and 103 deletions.
30 changes: 30 additions & 0 deletions src/Asserts/BaseAssert.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Sinnbeck\DomAssertions\Asserts;

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;

abstract class BaseAssert
{
use UsesElementAsserts;
use CanGatherAttributes;
use InteractsWithParser;
use NormalizesData;
use Debugging;

public function __construct($html, $element = null)
{
$this->html = $html;
$this->parser = DomParser::new($html);

if (! is_null($element)) {
$this->parser->setRoot($element);
}
}

}
33 changes: 5 additions & 28 deletions src/Asserts/ElementAssert.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,10 @@

use Sinnbeck\DomAssertions\Asserts\Traits\CanGatherAttributes;
use Sinnbeck\DomAssertions\Asserts\Traits\Debugging;
use Sinnbeck\DomAssertions\Asserts\Traits\HasElementAsserts;
use Sinnbeck\DomAssertions\Asserts\Traits\NormalizesData;
use Sinnbeck\DomAssertions\Asserts\Traits\UsesElementAsserts;
use Sinnbeck\DomAssertions\Asserts\Traits\InteractsWithParser;
use Sinnbeck\DomAssertions\DomParser;
use Sinnbeck\DomAssertions\Parsers\DomParser;

class ElementAssert
{
use HasElementAsserts;
use CanGatherAttributes;
use InteractsWithParser;
use Debugging;

/**
* @var \Sinnbeck\DomAssertions\DomParser
*/
protected DomParser $parser;

protected string $html;

protected array $attributes = [];

public function __construct($html, $element = null)
{
$this->html = $html;
$this->parser = DomParser::new($html);

if (! is_null($element)) {
$this->parser->setRoot($element);
}
}
}
class ElementAssert extends BaseAssert
{}
45 changes: 4 additions & 41 deletions src/Asserts/FormAssert.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,40 +7,13 @@
use PHPUnit\Framework\Assert;
use Sinnbeck\DomAssertions\Asserts\Traits\CanGatherAttributes;
use Sinnbeck\DomAssertions\Asserts\Traits\Debugging;
use Sinnbeck\DomAssertions\Asserts\Traits\HasElementAsserts;
use Sinnbeck\DomAssertions\Asserts\Traits\NormalizesData;
use Sinnbeck\DomAssertions\Asserts\Traits\UsesElementAsserts;
use Sinnbeck\DomAssertions\Asserts\Traits\InteractsWithParser;
use Sinnbeck\DomAssertions\DomParser;
use Sinnbeck\DomAssertions\Parsers\DomParser;

class FormAssert
class FormAssert extends BaseAssert
{
use HasElementAsserts;
use CanGatherAttributes;
use InteractsWithParser;
use Debugging;

/**
* @var \Sinnbeck\DomAssertions\DomParser
*/
protected DomParser $parser;

protected string $html;

protected array $attributes = [];

public function __construct($html, $form)
{
$this->html = $html;
$this->parser = DomParser::new($html)
->setRoot($form);
}

public function setParser($parser): static
{
$this->parser = $parser;

return $this;
}

public function hasAction(string $action): self
{
PHPUnit::assertEquals(
Expand Down Expand Up @@ -117,14 +90,4 @@ public function findSelect($selector = 'select', $callback = null): static
return $this;
}

// protected function getSelectorFromAttributes($type, array $attributes): string
// {
// $selector = $type;
//
// foreach ($attributes as $attribute => $value) {
// $selector .= sprintf('[%s="%s"]', $attribute, $value);
// }
//
// return $selector;
// }
}
22 changes: 4 additions & 18 deletions src/Asserts/SelectAssert.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,13 @@
use PHPUnit\Framework\Assert;
use Sinnbeck\DomAssertions\Asserts\Traits\CanGatherAttributes;
use Sinnbeck\DomAssertions\Asserts\Traits\Debugging;
use Sinnbeck\DomAssertions\Asserts\Traits\HasElementAsserts;
use Sinnbeck\DomAssertions\Asserts\Traits\NormalizesData;
use Sinnbeck\DomAssertions\Asserts\Traits\UsesElementAsserts;
use Sinnbeck\DomAssertions\Asserts\Traits\InteractsWithParser;
use Sinnbeck\DomAssertions\DomParser;
use Sinnbeck\DomAssertions\Parsers\DomParser;

class SelectAssert
class SelectAssert extends BaseAssert
{
use HasElementAsserts;
use CanGatherAttributes;
use InteractsWithParser;
use Debugging;

protected DomParser $parser;

protected array $attributes = [];

public function __construct(string $html, $root)
{
$this->parser = DomParser::new($html)
->setRoot($root);
}

public function containsOption(mixed $attributes): self
{
return $this->contains('option', $attributes);
Expand Down
3 changes: 0 additions & 3 deletions src/Asserts/Traits/CanGatherAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@

trait CanGatherAttributes
{
use NormalizesData;
use InteractsWithParser;

public function gatherAttributes($type): void
{
if (isset($this->attributes[$type])) {
Expand Down
4 changes: 1 addition & 3 deletions src/Asserts/Traits/InteractsWithParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@

namespace Sinnbeck\DomAssertions\Asserts\Traits;

use Sinnbeck\DomAssertions\DomParser;
use Sinnbeck\DomAssertions\Parsers\DomParser;

trait InteractsWithParser
{
use NormalizesData;

protected function getParser(): DomParser
{
return $this->parser;
Expand Down
11 changes: 7 additions & 4 deletions src/Asserts/Traits/NormalizesData.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

trait NormalizesData
{
protected function normalizeAttributesArray(array $attributes)
protected function normalizeAttributesArray(array $attributes): array
{
foreach ($attributes as $attribute => $value) {
$attributes[$attribute] = $this->normalizeAttributeValue($attribute, $value);
Expand All @@ -15,7 +15,7 @@ protected function normalizeAttributesArray(array $attributes)
return $attributes;
}

protected function normalizeAttributeValue($attribute, $value)
protected function normalizeAttributeValue($attribute, $value): mixed
{
if ($attribute === 'class') {
return $this->normalizeClass($value);
Expand All @@ -28,8 +28,11 @@ protected function normalizeAttributeValue($attribute, $value)
return $value;
}

protected function normalizeClass(string $class)
protected function normalizeClass(string $class): string
{
return Str::of($class)->explode(' ')->sort()->implode(' ');
return Str::of($class)
->explode(' ')
->sort()
->implode(' ');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@
use PHPUnit\Framework\Assert;
use Sinnbeck\DomAssertions\Asserts\ElementAssert;

trait HasElementAsserts
trait UsesElementAsserts
{
use NormalizesData;
use Macroable {
__call as protected callMacro;
}
Expand Down
3 changes: 2 additions & 1 deletion src/Macros/AssertElementMacro.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Illuminate\Testing\TestResponse;
use PHPUnit\Framework\Assert;
use Sinnbeck\DomAssertions\Asserts\ElementAssert;
use Sinnbeck\DomAssertions\DomParser;
use Sinnbeck\DomAssertions\Parsers\DomParser;

class AssertElementMacro
{
Expand Down Expand Up @@ -34,5 +34,6 @@ public function __invoke()

return $this;
};

}
}
2 changes: 1 addition & 1 deletion src/Macros/AssertFormMacro.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Illuminate\Testing\TestResponse;
use PHPUnit\Framework\Assert;
use Sinnbeck\DomAssertions\Asserts\FormAssert;
use Sinnbeck\DomAssertions\DomParser;
use Sinnbeck\DomAssertions\Parsers\DomParser;

class AssertFormMacro
{
Expand Down
2 changes: 1 addition & 1 deletion src/DomParser.php → src/Parsers/DomParser.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Sinnbeck\DomAssertions;
namespace Sinnbeck\DomAssertions\Parsers;

use DOMDocument;
use DOMElement;
Expand Down
2 changes: 1 addition & 1 deletion tests/DomParserTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

use Sinnbeck\DomAssertions\DomParser;
use Sinnbeck\DomAssertions\Parsers\DomParser;

it('can find a form', function () {
$html = <<<'HTML'
Expand Down

0 comments on commit a069330

Please sign in to comment.