This repository has been archived by the owner on Jun 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTest.php
165 lines (133 loc) · 4.48 KB
/
Test.php
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php
/*
* This file is part of RichPHPTests.
*
* Copyright (c) Richard Webster
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace RichPHPTests;
use RichPHPTests\Abstracts\AbstractTest;
final class Test extends AbstractTest
{
final public function __construct(
private mixed $value
) {}
public function isArray(): void
{
$actual = gettype($this->value);
$expected = 'array';
$this->assert($actual, $expected, "Value is of type '$actual', not an array.");
}
public function isNotArray(mixed $value): void
{
$this->assert(is_array($this->value), false, 'Value is an array.');
}
public function arrayHasKey(mixed $key): void
{
$this->assert(array_key_exists($key, $this->value), true, 'Key not found in array.');
}
public function arrayNotHasKey(mixed $key): void
{
$this->assert(array_key_exists($key, $this->value), false, 'Key was found in array.');
}
public function arrayHasValue(mixed $needle): void
{
$has_value = in_array($needle, $this->value, true);
$this->assert($has_value, true, 'Value not found in array.');
}
public function arrayNotHasValue(mixed $needle): void
{
$has_value = in_array($needle, $this->value, true);
$this->assert($has_value, false, 'Value was found in array.');
}
public function arrayIsSize(int $size): void
{
$this->assert(count($this->value), $size, 'Array is not the right size.');
}
public function isTrue(): void
{
$this->assert($this->value, true, 'Value is not TRUE.');
}
public function isFalse(): void
{
$this->assert($this->value, false, 'Value is not FALSE.');
}
public function isBool(): void
{
$this->assert(is_bool($this->value), true, 'Value is not a boolean.');
}
public function isNotBool(): void
{
$this->assert(is_bool($this->value), false, 'Value is a boolean.');
}
public function equals(mixed $condition): void
{
$this->assert($this->value, $condition, 'Values are not the same.');
}
public function notEquals(mixed $condition): void
{
$not_equals = ($this->value !== $condition);
$this->assert($not_equals, true, 'Values are the same.');
}
public function isInt(): void
{
$this->assert(is_int($this->value), true, 'Value is not an integer.');
}
public function isNotInt(): void
{
$this->assert(is_int($this->value), false, 'Value is an integer.');
}
public function isFloat(): void
{
$this->assert(is_float($this->value), true, 'Value is not a float.');
}
public function isNotFloat(): void
{
$this->assert(is_float($this->value), false, 'Value is a float.');
}
public function isObject(): void
{
$this->assert(is_object($this->value), true, 'Value is not an object.');
}
public function isNotObject(): void
{
$this->assert(is_object($this->value), false, 'Value is an object.');
}
public function isObjectOfType(string $type): void
{
$this->assert($this->value instanceof $type, true, 'Object is instance of "' . get_class($this->value) . '", not an instance of "' . (string) $type . '".');
}
public function isString(): void
{
$this->assert(is_string($this->value), true, 'Value is not a string.');
}
public function isNotString(): void
{
$this->assert(is_string($this->value), false, 'Value is a string.');
}
public function stringMatches(string $other): void
{
$this->assert($this->value, $other, 'Strings are not the same.');
}
public function stringNotMatch(string $other): void
{
$not_match = ($this->value !== $other);
$this->assert($not_match, true, 'Strings are the same.');
}
public function stringContains(string $needle): void
{
$this->assert(str_contains($this->value, $needle), true, 'String does not contain needle.');
}
public function stringNotContains(string $needle): void
{
$this->assert(str_contains($this->value, $needle), false, 'String does contain needle.');
}
private function assert(mixed $value, mixed $expected, string $error_message): void
{
$pass = ($value === $expected);
(new TestResult($value, $expected, $error_message, $pass));
}
}