Skip to content

Commit

Permalink
fixing phpstan
Browse files Browse the repository at this point in the history
  • Loading branch information
markhuot committed Nov 2, 2023
1 parent 97c256e commit e46cced
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 63 deletions.
17 changes: 2 additions & 15 deletions src/behaviors/ExpectableBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
// }
}
11 changes: 7 additions & 4 deletions src/behaviors/TestableElementBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

namespace markhuot\craftpest\behaviors;

use PHPUnit\Framework\Assert;
use yii\base\Behavior;

use function markhuot\craftpest\helpers\test\test;

/**
* # Elements
*
Expand All @@ -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;
}
Expand All @@ -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));
}


Expand Down
58 changes: 29 additions & 29 deletions src/behaviors/TestableResponseBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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;
}

Expand All @@ -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.'
);
Expand All @@ -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.'
);
Expand All @@ -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], " \"'")
Expand All @@ -368,7 +369,7 @@ function assertDownload(string $filename=null) {
return $this;
}
} else {
test()->assertTrue(true);
Assert::assertTrue(true);

return $this;
}
Expand All @@ -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;
}
Expand All @@ -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 . '`');
}
}

Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand All @@ -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);
Expand Down Expand Up @@ -779,16 +779,16 @@ function assertSessionMissing() {
* ```
*/
function assertStatus($code) {
test()->assertSame($code, $this->response->getStatusCode());
Assert::assertSame($code, $this->response->getStatusCode());
return $this->response;
}

/**
* 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;
}
Expand All @@ -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;
}
Expand Down
16 changes: 9 additions & 7 deletions src/dom/NodeList.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace markhuot\craftpest\dom;

use markhuot\craftpest\http\RequestBuilder;
use Pest\Expectation;
use PHPUnit\Framework\Assert;

/**
* # Node list
Expand Down Expand Up @@ -46,7 +48,7 @@ function querySelector(string $selector)
*/
function expect()
{
return test()->expect($this);
return new Expectation($this);
}

/**
Expand Down Expand Up @@ -174,16 +176,16 @@ 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) {
$keys = [];
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;
Expand All @@ -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;
}
Expand All @@ -213,7 +215,7 @@ public function assertText($expected) {
* ```
*/
public function assertContainsString($expected) {
test()->assertStringContainsString($expected, $this->getText());
Assert::assertStringContainsString($expected, $this->getText());

return $this;
}
Expand All @@ -226,7 +228,7 @@ public function assertContainsString($expected) {
* ```
*/
public function assertCount($expected) {
test()->assertCount($expected, $this);
Assert::assertCount($expected, $this);

return $this;
}
Expand Down
5 changes: 4 additions & 1 deletion src/helpers/Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
11 changes: 11 additions & 0 deletions src/helpers/Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
Expand Down
Loading

0 comments on commit e46cced

Please sign in to comment.