-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathautoload.php
More file actions
131 lines (116 loc) · 3.4 KB
/
Copy pathautoload.php
File metadata and controls
131 lines (116 loc) · 3.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?php
/**
* Autoloaded File to support Testing
*
* phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped
* phpcs:disable WordPress.NamingConventions.PrefixAllGlobals
*
* @package Mantle
*/
namespace Mantle\Testing;
use Faker\Factory;
use Faker\Generator;
use Mantle\Container\Container;
use Mantle\Faker\Faker_Provider;
use Mantle\Support\HTML;
use PHPUnit\Framework\AssertionFailedError;
use function Mantle\Support\Helpers\tap;
require_once __DIR__ . '/preload.php';
require_once __DIR__ . '/Mail/helpers.php';
if ( ! function_exists( __NAMESPACE__ . '\\manager' ) ) {
/**
* Retrieve an instance of the Installation Manager
*
* The manager can install the Mantle Testing Framework but will not by default.
* Call {@see Installation_Manager::install()} to install or use the
* {@see install()} helper.
*/
function manager(): Installation_Manager {
return Installation_Manager::instance();
}
}
if ( ! function_exists( __NAMESPACE__ . '\\install' ) ) {
/**
* Install the Mantle Testing Framework
*
* @param callable|null $callback Callback to invoke once the installation has begun.
*/
function install( ?callable $callback = null ): Installation_Manager {
return tap(
manager(),
fn ( Installation_Manager $manager ) => $manager->before( $callback ),
)->install();
}
}
if ( ! function_exists( __NAMESPACE__ . '\\html_string' ) ) {
/**
* Create a new HTML_String instance.
*
* @param string $html The HTML string to test.
*/
function html_string( string $html ): HTML {
return new HTML( $html );
}
}
if ( ! function_exists( __NAMESPACE__ . '\\mock_http_response' ) ) {
/**
* Create a new Mock HTTP Response
*
* @param string $body Response body.
* @param array $headers Response headers.
*/
function mock_http_response( string $body = '', array $headers = [] ): Mock_Http_Response {
return new Mock_Http_Response( $body, $headers );
}
}
if ( ! function_exists( __NAMESPACE__ . '\\mock_http_sequence' ) ) {
/**
* Create a new Mock HTTP Response Sequence
*/
function mock_http_sequence(): Mock_Http_Sequence {
return new Mock_Http_Sequence();
}
}
if ( ! function_exists( __NAMESPACE__ . '\\block_factory' ) ) {
/**
* Create a new block factory instance.
*/
function block_factory(): Block_Factory {
$container = Container::get_instance();
// If the Generator is not bound to the container, bind it.
if ( ! $container->bound( Generator::class ) ) {
$container->singleton(
Generator::class,
fn () => tap(
Factory::create(),
fn ( Generator $generator ) => $generator->addProvider( new Faker_Provider( $generator ) ),
),
);
}
return $container->make( Block_Factory::class );
}
}
if ( ! function_exists( __NAMESPACE__ . '\\iterate_test' ) ) {
/**
* Iterate a test a number of times, catching any assertion failures and
* re-throwing them with the iteration number in the message.
*
* @throws AssertionFailedError Thrown when an assertion fails.
*
* @param \Closure $callback The callback to execute for each iteration.
* @param int $times The number of times to iterate (default: 3).
*/
function iterate_test( \Closure $callback, int $times = 3 ): void {
for ( $i = 0; $i < $times; $i++ ) {
try {
$callback( $i + 1 );
} catch ( AssertionFailedError $e ) {
throw new AssertionFailedError(
'Failed on iteration ' . $i . ': ' . $e->getMessage(),
$e->getCode(),
$e
);
}
}
}
}