From e46ccedd0c4f6c15de4558242c9905968e0f44cd Mon Sep 17 00:00:00 2001 From: markhuot Date: Thu, 2 Nov 2023 09:56:18 -0400 Subject: [PATCH] fixing phpstan --- src/behaviors/ExpectableBehavior.php | 17 +------ src/behaviors/TestableElementBehavior.php | 11 ++-- src/behaviors/TestableResponseBehavior.php | 58 +++++++++++----------- src/dom/NodeList.php | 16 +++--- src/helpers/Http.php | 5 +- src/helpers/Test.php | 11 ++++ src/http/RequestHandler.php | 2 + src/http/requests/WebRequest.php | 8 ++- src/web/BenchmarkResult.php | 11 ++-- 9 files changed, 76 insertions(+), 63 deletions(-) diff --git a/src/behaviors/ExpectableBehavior.php b/src/behaviors/ExpectableBehavior.php index 3eff0c8..d0d30d8 100644 --- a/src/behaviors/ExpectableBehavior.php +++ b/src/behaviors/ExpectableBehavior.php @@ -2,26 +2,13 @@ namespace markhuot\craftpest\behaviors; +use Pest\Expectation; use yii\base\Behavior; class ExpectableBehavior extends Behavior { function expect() { - return test()->expect($this->owner); + return new Expectation($this->owner); } - - // function __isset($name) - // { - // return $name === 'expect'; - // } - // - // function __get($name) - // { - // if ($name === 'expect') { - // return test()->expect($this->owner); - // } - // - // throw new \InvalidArgumentException("Could not find {$name} on ".get_class($this->owner)); - // } } diff --git a/src/behaviors/TestableElementBehavior.php b/src/behaviors/TestableElementBehavior.php index 5701416..567b87a 100644 --- a/src/behaviors/TestableElementBehavior.php +++ b/src/behaviors/TestableElementBehavior.php @@ -2,8 +2,11 @@ namespace markhuot\craftpest\behaviors; +use PHPUnit\Framework\Assert; use yii\base\Behavior; +use function markhuot\craftpest\helpers\test\test; + /** * # Elements * @@ -28,7 +31,7 @@ class TestableElementBehavior extends Behavior */ function assertValid(array $keys = []) { - test()->assertCount(0, $this->owner->errors); + Assert::assertCount(0, $this->owner->errors); return $this->owner; } @@ -50,14 +53,14 @@ function assertInvalid(array $keys = []) ->mapWithKeys(fn ($key) => [$key => $this->owner->getErrors($key)]) ->filter(fn ($errors) => count($errors) === 0); if ($errors->count()) { - test()->fail('The following keys were expected to be invalid but were not: '.implode(', ', $errors->keys()->all())); + Assert::fail('The following keys were expected to be invalid but were not: '.implode(', ', $errors->keys()->all())); } else { - test()->assertTrue(true); + Assert::assertTrue(true); } } else { - test()->assertGreaterThanOrEqual(1, count($this->owner->errors)); + Assert::assertGreaterThanOrEqual(1, count($this->owner->errors)); } diff --git a/src/behaviors/TestableResponseBehavior.php b/src/behaviors/TestableResponseBehavior.php index da25715..36db9de 100644 --- a/src/behaviors/TestableResponseBehavior.php +++ b/src/behaviors/TestableResponseBehavior.php @@ -8,6 +8,7 @@ use markhuot\craftpest\http\requests\WebRequest; use markhuot\craftpest\test\Benchmark; use markhuot\craftpest\web\TestableResponse; +use PHPUnit\Framework\Assert; use Symfony\Component\DomCrawler\Crawler; use yii\base\Behavior; @@ -222,10 +223,10 @@ function assertCacheTag(string ...$tags) */ function assertCookie(string $name, string $value=null) { if ($value === null) { - test()->assertContains($name, array_keys($this->response->cookies->toArray())); + Assert::assertContains($name, array_keys($this->response->cookies->toArray())); } else { - test()->assertSame($this->response->cookies->getValue($name), $value); + Assert::assertSame($this->response->cookies->getValue($name), $value); } return $this->response; @@ -247,7 +248,7 @@ function assertCookieExpired(string $name) { // Then check the expiration of it $cookie = $this->response->cookies->get($name); if ($cookie->expire === 0 || $cookie->expire >= time()) { - test()->fail('Cookie `' . $name . '` does not have an expiration in the past.'); + Assert::fail('Cookie `' . $name . '` does not have an expiration in the past.'); } return $this->response; @@ -267,7 +268,7 @@ function assertCookieNotExpired(string $name) { // Then check the expiration of it $cookie = $this->response->cookies->get($name); if ($cookie->expire !== 0 && $cookie->expire < time()) { - test()->fail('Cookie `' . $name . '` does not have an expiration in the future.'); + Assert::fail('Cookie `' . $name . '` does not have an expiration in the future.'); } return $this->response; @@ -282,7 +283,7 @@ function assertCookieNotExpired(string $name) { */ function assertCookieMissing(string $name) { // First check that the cookie exists - test()->assertNotContains($name, array_keys($this->response->cookies->toArray())); + Assert::assertNotContains($name, array_keys($this->response->cookies->toArray())); return $this->response; } @@ -306,7 +307,7 @@ function assertCreated() { * ``` */ function assertDontSee(string $text) { - test()->assertStringNotContainsString($text, $this->response->content); + Assert::assertStringNotContainsString($text, $this->response->content); return $this->response; } @@ -320,7 +321,7 @@ function assertDontSee(string $text) { * ``` */ function assertDontSeeText(string $text) { - test()->assertStringNotContainsString($text, preg_replace('/\s+/', ' ', strip_tags($this->response->data))); + Assert::assertStringNotContainsString($text, preg_replace('/\s+/', ' ', strip_tags($this->response->data))); return $this->response; } @@ -337,7 +338,7 @@ function assertDownload(string $filename=null) { $contentDisposition = explode(';', $this->response->headers->get('content-disposition')); if (trim($contentDisposition[0]) !== 'attachment') { - test()->fail( + Assert::fail( 'Response does not offer a file download.'.PHP_EOL. 'Disposition ['.trim($contentDisposition[0]).'] found in header, [attachment] expected.' ); @@ -346,7 +347,7 @@ function assertDownload(string $filename=null) { if (! is_null($filename)) { if (isset($contentDisposition[1]) && trim(explode('=', $contentDisposition[1])[0]) !== 'filename') { - test()->fail( + Assert::fail( 'Unsupported Content-Disposition header provided.'.PHP_EOL. 'Disposition ['.trim(explode('=', $contentDisposition[1])[0]).'] found in header, [filename] expected.' ); @@ -355,9 +356,9 @@ function assertDownload(string $filename=null) { $message = "Expected file [{$filename}] is not present in Content-Disposition header."; if (! isset($contentDisposition[1])) { - test()->fail($message); + Assert::fail($message); } else { - test()->assertSame( + Assert::assertSame( $filename, isset(explode('=', $contentDisposition[1])[1]) ? trim(explode('=', $contentDisposition[1])[1], " \"'") @@ -368,7 +369,7 @@ function assertDownload(string $filename=null) { return $this; } } else { - test()->assertTrue(true); + Assert::assertTrue(true); return $this; } @@ -383,7 +384,7 @@ function assertDownload(string $filename=null) { * ``` */ function assertExactJson(array $json) { - test()->assertEqualsCanonicalizing($json, json_decode($this->response->content, true)); + Assert::assertEqualsCanonicalizing($json, json_decode($this->response->content, true)); return $this->response; } @@ -410,15 +411,15 @@ function assertForbidden() { */ function assertHeader(string $name, string $expected=null) { if ($expected === null) { - test()->assertContains($name, array_keys($this->response->headers->toArray())); + Assert::assertContains($name, array_keys($this->response->headers->toArray())); } else { $value = $this->response->headers->get($name); if ($expected === $value) { - test()->assertTrue(true); + Assert::assertTrue(true); } else { - test()->fail('Response header `' . $name . '` with value `' . $value . '` does not match `' . $expected . '`'); + Assert::fail('Response header `' . $name . '` with value `' . $value . '` does not match `' . $expected . '`'); } } @@ -433,7 +434,7 @@ function assertHeader(string $name, string $expected=null) { * ``` */ function assertHeaderMissing(string $name) { - test()->assertNotContains($name, array_keys($this->response->headers->toArray())); + Assert::assertNotContains($name, array_keys($this->response->headers->toArray())); return $this->response; } @@ -520,7 +521,7 @@ function assertLocation(string $location, array $checkParts=null) { $locationParts = collect($locationParts)->only($checkParts)->toArray(); } - test()->assertSame($locationParts, $headerParts); + Assert::assertSame($locationParts, $headerParts); return $this->response; } @@ -572,7 +573,7 @@ function assertFlash(?string $message = null, ?string $key = null) function assertNoContent($status=204) { $this->assertStatus($status); - test()->assertEmpty($this->response->content, 'Response content is not empty.'); + Assert::assertEmpty($this->response->content, 'Response content is not empty.'); return $this->response; } @@ -607,9 +608,9 @@ function assertOk() { * ``` */ function assertRedirect() { - test()->assertGreaterThanOrEqual(300, $this->response->getStatusCode()); - test()->assertLessThan(400, $this->response->getStatusCode()); - test()->assertContains('location', array_keys($this->response->headers->toArray()), 'The response does not contain a location header.'); + Assert::assertGreaterThanOrEqual(300, $this->response->getStatusCode()); + Assert::assertLessThan(400, $this->response->getStatusCode()); + Assert::assertContains('location', array_keys($this->response->headers->toArray()), 'The response does not contain a location header.'); return $this->response; } @@ -674,7 +675,7 @@ function followRedirects() * ``` */ function assertSee(string $text) { - test()->assertStringContainsString($text, $this->response->content); + Assert::assertStringContainsString($text, $this->response->content); return $this->response; } @@ -685,8 +686,7 @@ protected function seeInOrder(string $haystack, array $needles) foreach ($needles as $needle) { $lastPos = strpos($haystack, $needle, $lastPos); if ($lastPos === false) { - test()->fail('The text `' . $needle . '` was not found in order'); - return; + Assert::fail('The text `' . $needle . '` was not found in order'); } } expect(true)->toBe(true); @@ -779,7 +779,7 @@ function assertSessionMissing() { * ``` */ function assertStatus($code) { - test()->assertSame($code, $this->response->getStatusCode()); + Assert::assertSame($code, $this->response->getStatusCode()); return $this->response; } @@ -787,8 +787,8 @@ function assertStatus($code) { * Asserts a successfull (200-class) response code. */ function assertSuccessful() { - test()->assertGreaterThanOrEqual(200, $this->response->getStatusCode()); - test()->assertLessThan(300, $this->response->getStatusCode()); + Assert::assertGreaterThanOrEqual(200, $this->response->getStatusCode()); + Assert::assertLessThan(300, $this->response->getStatusCode()); return $this->response; } @@ -803,7 +803,7 @@ function assertSuccessful() { function assertTitle(string $title) { $actualTitle = $this->querySelector('title')->text; - test()->assertSame($title, $actualTitle, 'The given title did not match `' . $actualTitle . '`'); + Assert::assertSame($title, $actualTitle, 'The given title did not match `' . $actualTitle . '`'); return $this->response; } diff --git a/src/dom/NodeList.php b/src/dom/NodeList.php index fc9ae3b..942998e 100644 --- a/src/dom/NodeList.php +++ b/src/dom/NodeList.php @@ -3,6 +3,8 @@ namespace markhuot\craftpest\dom; use markhuot\craftpest\http\RequestBuilder; +use Pest\Expectation; +use PHPUnit\Framework\Assert; /** * # Node list @@ -46,7 +48,7 @@ function querySelector(string $selector) */ function expect() { - return test()->expect($this); + return new Expectation($this); } /** @@ -174,7 +176,7 @@ function click() function assertAttribute(string $key, string $value) { if ($this->crawler->count() === 0) { - test()->fail('No matching elements to assert against attribute `' . $key . '`'); + Assert::fail('No matching elements to assert against attribute `' . $key . '`'); } $this->each(function ($node) use ($key, $value) { @@ -182,8 +184,8 @@ function assertAttribute(string $key, string $value) foreach ($node->getNode(0)->attributes as $attr) { $keys[] = $attr->name; } - test()->assertContains($key, $keys); - test()->assertSame($value, $node->attr($key)); + Assert::assertContains($key, $keys); + Assert::assertSame($value, $node->attr($key)); }); return $this; @@ -200,7 +202,7 @@ function assertAttribute(string $key, string $value) * ``` */ public function assertText($expected) { - test()->assertSame($expected, $this->getText()); + Assert::assertSame($expected, $this->getText()); return $this; } @@ -213,7 +215,7 @@ public function assertText($expected) { * ``` */ public function assertContainsString($expected) { - test()->assertStringContainsString($expected, $this->getText()); + Assert::assertStringContainsString($expected, $this->getText()); return $this; } @@ -226,7 +228,7 @@ public function assertContainsString($expected) { * ``` */ public function assertCount($expected) { - test()->assertCount($expected, $this); + Assert::assertCount($expected, $this); return $this; } diff --git a/src/helpers/Http.php b/src/helpers/Http.php index 1f415cb..d9cc1cc 100644 --- a/src/helpers/Http.php +++ b/src/helpers/Http.php @@ -3,12 +3,15 @@ namespace markhuot\craftpest\helpers\http; use markhuot\craftpest\web\TestableResponse; +use Pest\Expectation; use Pest\PendingCalls\TestCall; +use function markhuot\craftpest\helpers\test\test; + function get(string $uri='/'): TestableResponse|TestCall { return test()->get($uri); } function expectGet($uri='/') { - return test()->expect(fn () => test()->get($uri)); + return new Expectation(fn () => test()->get($uri)); } diff --git a/src/helpers/Test.php b/src/helpers/Test.php index 87023b3..eb0ae37 100644 --- a/src/helpers/Test.php +++ b/src/helpers/Test.php @@ -2,7 +2,10 @@ namespace markhuot\craftpest\helpers\test; +use markhuot\craftpest\test\TestCase; use Mockery; +use Pest\Concerns\Expectable; +use Pest\PendingCalls\TestCall; if (!function_exists('mock')) { function mock($className) { @@ -20,6 +23,14 @@ function spy($className) { } } +/** + * @return Expectable|TestCall|TestCase|mixed + */ +function test() +{ + return \test(); +} + // The default dump() and dd() methods that ship with Craft don't play well with Pest so // set the correct versions early function dump($args) diff --git a/src/http/RequestHandler.php b/src/http/RequestHandler.php index 7a572a2..250fa98 100644 --- a/src/http/RequestHandler.php +++ b/src/http/RequestHandler.php @@ -8,6 +8,8 @@ use Twig\Error\RuntimeError; use yii\base\ExitException; +use function markhuot\craftpest\helpers\test\test; + class RequestHandler { private \craft\web\Application $app; diff --git a/src/http/requests/WebRequest.php b/src/http/requests/WebRequest.php index 9835431..f596a3a 100644 --- a/src/http/requests/WebRequest.php +++ b/src/http/requests/WebRequest.php @@ -4,8 +4,12 @@ use craft\helpers\App; use markhuot\craftpest\test\Dd; +use Pest\Expectation; +use PHPUnit\Framework\Assert; use yii\web\NotFoundHttpException; +use function markhuot\craftpest\helpers\test\test; + abstract class WebRequest extends \craft\web\Request { use Dd; @@ -41,7 +45,7 @@ function __get($key) function expect() { - return test()->expect($this); + return new Expectation($this); } /** @@ -157,7 +161,7 @@ protected function uriContainsAdminSlug(string $uri): bool function assertMethod($method) { - test()->assertSame(strtoupper($method), $this->getMethod()); + Assert::assertSame(strtoupper($method), $this->getMethod()); return $this; } diff --git a/src/web/BenchmarkResult.php b/src/web/BenchmarkResult.php index f2e0574..3442091 100644 --- a/src/web/BenchmarkResult.php +++ b/src/web/BenchmarkResult.php @@ -3,6 +3,7 @@ namespace markhuot\craftpest\web; use craft\debug\Module; +use PHPUnit\Framework\Assert; /** * # Benchmarks @@ -56,7 +57,7 @@ public function assertQueryCount(int $expected) $queries = $this->getQueries(); $actual = $queries->count(); - test()->assertEquals($expected, $actual, 'The expected query count, ' . $expected . ' did not match the actual query count, ' . $actual . PHP_EOL . '- ' . $queries->pluck('info')->join(PHP_EOL.'- ')); + Assert::assertEquals($expected, $actual, 'The expected query count, ' . $expected . ' did not match the actual query count, ' . $actual . PHP_EOL . '- ' . $queries->pluck('info')->join(PHP_EOL.'- ')); } function getQueryTiming() @@ -103,7 +104,7 @@ function assertNoDuplicateQueries() { $duplicates = $this->getDuplicateQueries(); - test()->assertSame( + Assert::assertSame( 0, $duplicates->count(), 'Duplicate queries were found during the test. ' . "\n" . $duplicates->first() @@ -135,7 +136,7 @@ function assertLoadTimeLessThan(float $expectedLoadTime) { $actualLoadTime = $this->getPanels()['profiling']->data['time']; - test()->assertLessThan($expectedLoadTime, $actualLoadTime); + Assert::assertLessThan($expectedLoadTime, $actualLoadTime); return $this; } @@ -159,7 +160,7 @@ function assertMemoryLoadLessThan(float $expectedMemoryLoad) $actualMemoryLoadBytes = $this->getPanels()['profiling']->data['memory']; $actualMemoryLoadMb = $actualMemoryLoadBytes/1024/1024; - test()->assertLessThan($expectedMemoryLoad, $actualMemoryLoadMb); + Assert::assertLessThan($expectedMemoryLoad, $actualMemoryLoadMb); return $this; } @@ -184,7 +185,7 @@ function assertAllQueriesFasterThan(float $expectedQueryTime) }); if ($failing->count()) { - test()->fail($failing->count() . ' queries were slower than ' . $expectedQueryTime); + Assert::fail($failing->count() . ' queries were slower than ' . $expectedQueryTime); } expect(true)->toBe(true);