Skip to content

Commit

Permalink
reduce complexity
Browse files Browse the repository at this point in the history
  • Loading branch information
bkdotcom committed Sep 19, 2023
1 parent 9545731 commit 762a6d8
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 39 deletions.
108 changes: 70 additions & 38 deletions src/DOMAssertionTrait.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* This file is part of CssXpath
*
Expand Down Expand Up @@ -80,47 +81,19 @@ public static function assertSelectCount($selector, $count, $actual, $message =
public static function assertSelectEquals($selector, $content, $count, $actual, $message = '', $isHtml = true)
{
$found = CssSelect::select($actual, $selector);
if (\is_string($content)) {
foreach ($found as $k => $node) {
$keep = $content === ''
? $node['innerHTML'] === ''
: \strstr($node['innerHTML'], $content) !== false;
if (\preg_match('/^regexp\s*:\s*(.*)/i', $content, $matches)) {
$keep = (bool) \preg_match($matches[1], $node['innerHTML']);
}
if (!$keep) {
unset($found[$k]);
}
}
}
$countFound = \count($found);
if (\is_numeric($count)) {
self::assertEquals($count, $countFound, $message);
return;
}
if (\is_bool($count)) {
$isFound = \count($found) > 0;
$count
? self::assertTrue($isFound, $message)
: self::assertFalse($isFound, $message);
return;
}
if (\is_array($count) && \array_intersect_key($count, \array_flip(array('>', '<', '>=', '<=')))) {
if (isset($count['>'])) {
self::assertTrue($countFound > $count['>'], $message);
}
if (isset($count['>='])) {
self::assertTrue($countFound >= $count['>='], $message);
$found = \array_filter($found, static function ($node) use ($content) {
if (\is_string($content) === false) {
return true;
}
if (isset($count['<'])) {
self::assertTrue($countFound < $count['<'], $message);
if ($content === '') {
return $node['innerHTML'] === '';
}
if (isset($count['<='])) {
self::assertTrue($countFound <= $count['<='], $message);
if (\preg_match('/^regexp\s*:\s*(.*)/i', $content, $matches)) {
return \preg_match($matches[1], $node['innerHTML']) === 1;
}
return;
}
throw new \PHPUnit\Framework\Exception('Invalid count format');
return \strstr($node['innerHTML'], $content) !== false;
});
self::assertDomCount(\count($found), $count, $message);
}

/**
Expand All @@ -143,4 +116,63 @@ public static function assertSelectRegExp($selector, $pattern, $count, $actual,
{
self::assertSelectEquals($selector, 'regexp:' . $pattern, $count, $actual, $message, $isHtml);
}

/**
* Assert number found elements match expected
*
* @param int $countActual Num of matches actually found
* @param int|bool|array $count bool, count, or array('>'=5, <=10)
* @param string $message Exception message
*
* @return void
*
* @throws \PHPUnit\Framework\Exception Invalid count format.
*/
private static function assertDomCount($countActual, $count, $message)
{
self::assertValidDomCount($count);
if ($count === true) {
$count = array('>' => 0);
} elseif ($count === false) {
$count = 0;
}
if (\is_numeric($count)) {
self::assertEquals($count, $countActual, $message);
return;
}
$countVals = \array_merge(array(
'<' => 0,
'<=' => 0,
'>' => 0,
'>=' => 0,
), $count);
$count = \array_intersect_key(array(
'<' => $countActual < $countVals['<'],
'<=' => $countActual <= $countVals['<='],
'>' => $countActual > $countVals['>'],
'>=' => $countActual >= $countVals['>='],
), $count);
\array_walk($count, static function ($val) use ($message) {
self::assertTrue($val, $message);
});
}

/**
* Assert bool, int, or array supplied for count
*
* @param mixed $count Count value
*
* @return void
*
* @throws \PHPUnit\Framework\Exception
*/
private static function assertValidDomCount($count)
{
if (\is_bool($count) === false && \is_numeric($count) === false && \is_array($count) === false) {
throw new \PHPUnit\Framework\Exception('Invalid count format. Expected bool, int, or array()');
}
if (\is_array($count) && \array_intersect_key($count, \array_flip(array('>', '<', '>=', '<='))) === array()) {
throw new \PHPUnit\Framework\Exception('Invalid count. Array should contain >, >=, <, and/or <=');
}
}
}
1 change: 1 addition & 0 deletions src/DOMTestCase.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* This file is part of CssXpath
*
Expand Down
15 changes: 14 additions & 1 deletion tests/CssXpath/DOMAssertionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public function testAssertSelectEquals()
'>' => 0,
'>=' => 0,
), '<div class="name"></div><div class="name">Jimmy</div>');
self::assertSelectEquals('.name', 'fred', 0, '<div class="name"></div><div class="name">Jimmy</div>');
}

public function testAssertSelectRegExp()
Expand All @@ -37,6 +38,18 @@ public function testAssertSelectRegExp()

public function testInvalidCountArg()
{
$caughtException = false;
$message = null;
try {
self::assertSelectEquals('.name', '', 'zero', '');
} catch (\PHPUnit\Framework\Exception $e) {
$caughtException = true;
$message = $e->getMessage();
}
self::assertTrue($caughtException);
self::assertSame('Invalid count format. Expected bool, int, or array()', $message);


$caughtException = false;
$message = null;
try {
Expand All @@ -46,6 +59,6 @@ public function testInvalidCountArg()
$message = $e->getMessage();
}
self::assertTrue($caughtException);
self::assertSame('Invalid count format', $message);
self::assertSame('Invalid count. Array should contain >, >=, <, and/or <=', $message);
}
}

0 comments on commit 762a6d8

Please sign in to comment.