From 762a6d89a1e8d8ff00812eeddc7a853152ec80ab Mon Sep 17 00:00:00 2001 From: Brad Kent Date: Mon, 18 Sep 2023 22:39:38 -0500 Subject: [PATCH] reduce complexity --- src/DOMAssertionTrait.php | 108 ++++++++++++++++++---------- src/DOMTestCase.php | 1 + tests/CssXpath/DOMAssertionTest.php | 15 +++- 3 files changed, 85 insertions(+), 39 deletions(-) diff --git a/src/DOMAssertionTrait.php b/src/DOMAssertionTrait.php index 3f43529..1182fe9 100644 --- a/src/DOMAssertionTrait.php +++ b/src/DOMAssertionTrait.php @@ -1,4 +1,5 @@ $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); } /** @@ -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 <='); + } + } } diff --git a/src/DOMTestCase.php b/src/DOMTestCase.php index 8d84a2e..36ec34e 100644 --- a/src/DOMTestCase.php +++ b/src/DOMTestCase.php @@ -1,4 +1,5 @@ ' => 0, '>=' => 0, ), '
Jimmy
'); + self::assertSelectEquals('.name', 'fred', 0, '
Jimmy
'); } public function testAssertSelectRegExp() @@ -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 { @@ -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); } }