Skip to content

Commit 2c36290

Browse files
authored
Adding factory and other helper assertions (#34)
* WIP on upgrade to pest 4.0 * Prepare for upgrade * Enable for branch * Fix static * Fix static * Drop node * Adjust workflow * Drop 8.2 * Drop tests * Adding more helper assertions * Appeasing phpstan * Appease pint * CHANGELOG * Removing old * Removing .phpunit.cache/test-results * Rector fix
1 parent b0d956c commit 2c36290

File tree

16 files changed

+287
-49
lines changed

16 files changed

+287
-49
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ coverage.xml
1111
*.swo
1212
.vscode
1313
.DS_Store
14+
.phpunit.cache

.php-cs-fixer.dist.php

Lines changed: 0 additions & 29 deletions
This file was deleted.

.phpunit.cache/test-results

Lines changed: 0 additions & 1 deletion
This file was deleted.

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
77
## Unreleased
88

99
- Upgrade to Pest 4.0. Requires PHP 8.3+ and `mantle-framework/testkit` 1.9+.
10+
- Added multiple new helpers including `factory()`, `actingAs()`, and `fakeRequest()`.
1011

1112
## v0.5.0
1213

composer.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@
3232
}
3333
},
3434
"require-dev": {
35-
"pestphp/pest-dev-tools": "^4.0"
35+
"pestphp/pest-dev-tools": "^4.0",
36+
"szepeviktor/phpstan-wordpress": "^2.0"
3637
},
3738
"extra": {
3839
"branch-alias": {
@@ -59,10 +60,12 @@
5960
"scripts": {
6061
"rector": "rector",
6162
"lint": "pint",
63+
"phpstan": "phpstan --memory-limit=512M",
6264
"test:rector": "rector --dry-run",
6365
"test:lint": "pint --test",
64-
"test:types": "phpstan analyse --ansi",
66+
"test:types": "@phpstan",
6567
"test:unit": "pest --colors=always",
68+
"test:watch": "npx nodemon --watch tests --exec \"composer test:unit\" -e php",
6669
"test": [
6770
"@test:rector",
6871
"@test:lint",

phpstan.neon

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
includes:
2+
- vendor/szepeviktor/phpstan-wordpress/extension.neon
3+
14
parameters:
25
level: max
36
paths:

src/Autoload.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,15 @@
22

33
declare(strict_types=1);
44

5-
require_once __DIR__.'/Http.php';
5+
(static function (): void {
6+
$files = [
7+
'Factory.php',
8+
'Http.php',
9+
'RemoteRequest.php',
10+
'User.php',
11+
];
12+
13+
foreach ($files as $file) {
14+
require_once __DIR__."/{$file}";
15+
}
16+
})();

src/Factory.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Pest\PestPluginWordPress;
6+
7+
use Mantle\Database\Factory\Factory_Container;
8+
use Mantle\Testing\TestCase;
9+
10+
/**
11+
* Get the factory container instance.
12+
*/
13+
function factory(): Factory_Container
14+
{
15+
return TestCase::factory();
16+
}

src/Http.php

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@ function from(string $url): Pending_Testable_Request
2929
return test()->with_header('referer', $url);
3030
}
3131

32+
/**
33+
* Start a fluent request to the application.
34+
*/
35+
function request(): Pending_Testable_Request
36+
{
37+
return test()->with_headers([]);
38+
}
39+
3240
/**
3341
* Visit the given URI with a GET request.
3442
*
@@ -39,3 +47,84 @@ function get(mixed $uri, array $headers = []): Test_Response
3947
{
4048
return test()->get($uri, $headers);
4149
}
50+
51+
/**
52+
* Visit the given URI with a POST request.
53+
*
54+
* @param mixed $uri request URI
55+
* @param array<string, mixed> $data request data
56+
* @param array<string, string> $headers request Headers to load
57+
*/
58+
function post(mixed $uri, array $data = [], array $headers = []): Test_Response
59+
{
60+
return test()->post($uri, $data, $headers);
61+
}
62+
63+
/**
64+
* Visit the given URI with a PUT request.
65+
*
66+
* @param mixed $uri request URI
67+
* @param array<string, mixed> $data request data
68+
* @param array<string, string> $headers request Headers to load
69+
*/
70+
function put(mixed $uri, array $data = [], array $headers = []): Test_Response
71+
{
72+
return test()->put($uri, $data, $headers);
73+
}
74+
75+
/**
76+
* Visit the given URI with a PATCH request.
77+
*
78+
* @param mixed $uri request URI
79+
* @param array<string, mixed> $data request data
80+
* @param array<string, string> $headers request Headers to load
81+
*/
82+
function patch(mixed $uri, array $data = [], array $headers = []): Test_Response
83+
{
84+
return test()->patch($uri, $data, $headers);
85+
}
86+
87+
/**
88+
* Visit the given URI with a DELETE request.
89+
*
90+
* @param mixed $uri request URI
91+
* @param array<string, mixed> $data request data
92+
* @param array<string, string> $headers request Headers to load
93+
*/
94+
function delete(mixed $uri, array $data = [], array $headers = []): Test_Response
95+
{
96+
return test()->delete($uri, $data, $headers);
97+
}
98+
99+
/**
100+
* Visit the given URI with an OPTIONS request.
101+
*
102+
* @param mixed $uri request URI
103+
* @param array<string, mixed> $data request data
104+
* @param array<string, string> $headers request Headers to load
105+
*/
106+
function options(mixed $uri, array $data = [], array $headers = []): Test_Response
107+
{
108+
return test()->options($uri, $data, $headers);
109+
}
110+
111+
/**
112+
* Visit the given URI with a HEAD request.
113+
*
114+
* @param mixed $uri request URI
115+
* @param array<string, string> $headers request Headers to load
116+
*/
117+
function head(mixed $uri, array $headers = []): Test_Response
118+
{
119+
return test()->head($uri, $headers);
120+
}
121+
122+
/**
123+
* Create a WordPress post and visit its permalink with a GET request.
124+
*
125+
* @param array<string, mixed> $arguments request data
126+
*/
127+
function fetchPost(array $arguments = []): Test_Response
128+
{
129+
return test()->get(factory()->post->create_and_get($arguments));
130+
}

src/RemoteRequest.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Pest\PestPluginWordPress;
6+
7+
use Closure;
8+
use Mantle\Contracts\Support\Arrayable;
9+
use Mantle\Http_Client\Http_Method;
10+
use Mantle\Http_Client\Request;
11+
use Mantle\Testing\Mock_Http_Response;
12+
use Mantle\Testing\Mock_Http_Sequence;
13+
14+
/**
15+
* Fake a remote HTTP request.
16+
*
17+
* @template TCallableReturn of Mock_Http_Sequence|Mock_Http_Response|Arrayable|null
18+
*
19+
* @param (callable(string|Request, ?array<mixed>): TCallableReturn)|Mock_Http_Response|string|array<string, Mock_Http_Response|callable> $url_or_callback URL to fake, array of URL and response pairs, or a closure
20+
* that will return a faked response.
21+
* @param Mock_Http_Response|array<mixed>|callable $response Optional response object, defaults to a 200 response with no body.
22+
* @param Http_Method|string|null $method Optional request method to apply to, defaults to all. Does not apply to array of URL and response pairs OR callbacks.
23+
*/
24+
function fakeRequest(
25+
Mock_Http_Response|callable|string|array|null $url_or_callback = null,
26+
Mock_Http_Response|array|callable|null $response = null,
27+
Http_Method|string|null $method = null
28+
): ?Mock_Http_Response {
29+
return test()->fake_request($url_or_callback, $response, $method);
30+
}
31+
32+
/**
33+
* Prevent any stray HTTP requests.
34+
*
35+
* @param Mock_Http_Response|Closure|bool $response Optional response or boolean to enable/disable. Defaults to true, which will return a 500 response for any stray requests.
36+
*/
37+
function preventStrayRequests(Mock_Http_Response|Closure|bool $response = true): void
38+
{
39+
test()->prevent_stray_requests($response);
40+
}
41+
42+
/**
43+
* Allow stray HTTP requests.
44+
*/
45+
function allowStrayRequests(): void
46+
{
47+
test()->allow_stray_requests();
48+
}

0 commit comments

Comments
 (0)