|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Codeception\Util\Shared; |
| 6 | + |
| 7 | +use PHPUnit\Framework\Assert; |
| 8 | +use PHPUnit\Framework\Constraint\Constraint as PHPUnitConstraint; |
| 9 | +use PHPUnit\Framework\Constraint\LogicalNot; |
| 10 | +use ReflectionClass; |
| 11 | + |
| 12 | +trait Asserts |
| 13 | +{ |
| 14 | + use InheritedAsserts; |
| 15 | + |
| 16 | + /** |
| 17 | + * @param array{0: string} $arguments |
| 18 | + */ |
| 19 | + protected function assert(array $arguments, bool $not = false): void |
| 20 | + { |
| 21 | + $not = $not ? 'Not' : ''; |
| 22 | + $method = ucfirst(array_shift($arguments)); |
| 23 | + if (($method === 'True') && $not) { |
| 24 | + $method = 'False'; |
| 25 | + $not = ''; |
| 26 | + } |
| 27 | + |
| 28 | + if (($method === 'False') && $not) { |
| 29 | + $method = 'True'; |
| 30 | + $not = ''; |
| 31 | + } |
| 32 | + |
| 33 | + $fullMethod = "assert{$not}{$method}"; |
| 34 | + |
| 35 | + $rc = new ReflectionClass(Assert::class); |
| 36 | + if ($rc->hasMethod($fullMethod) && $rc->getMethod($fullMethod)->isStatic()) { |
| 37 | + $rc->getMethod($fullMethod)->invokeArgs(null, $arguments); |
| 38 | + } else { |
| 39 | + throw new \RuntimeException("Method Assert::{$fullMethod} does not exist"); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * @param array{0: string} $arguments |
| 45 | + * @return void |
| 46 | + */ |
| 47 | + protected function assertNot(array $arguments): void |
| 48 | + { |
| 49 | + $this->assert($arguments, true); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Asserts that a file does not exist. |
| 54 | + */ |
| 55 | + protected function assertFileNotExists(string $filename, string $message = ''): void |
| 56 | + { |
| 57 | + Assert::assertFileDoesNotExist($filename, $message); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Asserts that a value is greater than or equal to another value. |
| 62 | + * |
| 63 | + * @param mixed $expected |
| 64 | + * @param mixed $actual |
| 65 | + */ |
| 66 | + protected function assertGreaterOrEquals($expected, $actual, string $message = ''): void |
| 67 | + { |
| 68 | + Assert::assertGreaterThanOrEqual($expected, $actual, $message); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Asserts that a variable is empty. |
| 73 | + */ |
| 74 | + protected function assertIsEmpty(mixed $actual, string $message = ''): void |
| 75 | + { |
| 76 | + Assert::assertEmpty($actual, $message); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Asserts that a value is smaller than or equal to another value. |
| 81 | + */ |
| 82 | + protected function assertLessOrEquals(mixed $expected, mixed $actual, string $message = ''): void |
| 83 | + { |
| 84 | + Assert::assertLessThanOrEqual($expected, $actual, $message); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Asserts that a string does not match a given regular expression. |
| 89 | + */ |
| 90 | + protected function assertNotRegExp(string $pattern, string $string, string $message = ''): void |
| 91 | + { |
| 92 | + Assert::assertDoesNotMatchRegularExpression($pattern, $string, $message); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Asserts that a string matches a given regular expression. |
| 97 | + */ |
| 98 | + protected function assertRegExp(string $pattern, string $string, string $message = ''): void |
| 99 | + { |
| 100 | + Assert::assertMatchesRegularExpression($pattern, $string, $message); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Evaluates a PHPUnit\Framework\Constraint matcher object. |
| 105 | + */ |
| 106 | + protected function assertThatItsNot(mixed $value, PHPUnitConstraint $constraint, string $message = ''): void |
| 107 | + { |
| 108 | + $constraint = new LogicalNot($constraint); |
| 109 | + Assert::assertThat($value, $constraint, $message); |
| 110 | + } |
| 111 | +} |
0 commit comments