1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the ASSERTS package. |
5
|
|
|
* |
6
|
|
|
* (c) Jitendra Adhikari <[email protected]> |
7
|
|
|
* <https://github.com/adhocore> |
8
|
|
|
* |
9
|
|
|
* Licensed under MIT license. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Ahc\Asserts; |
13
|
|
|
|
14
|
|
|
trait Asserts |
15
|
|
|
{ |
16
|
|
|
public function assertJsonSubset($expected, $actual, string $message = '') |
17
|
|
|
{ |
18
|
|
|
$actual = \json_encode($actual); |
19
|
|
|
$expected = \json_encode($expected); |
20
|
|
|
$expected = \trim($expected, '{}'); |
21
|
|
|
|
22
|
|
|
$this->assertContains($expected, $actual, "$message$actual doesnot contain $expected"); |
|
|
|
|
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function assertJsonSubsets($expected, /* more expected ...*/ $actual) |
26
|
|
|
{ |
27
|
|
|
$expected = \func_get_args(); |
28
|
|
|
$actual = \array_pop($expected); |
29
|
|
|
|
30
|
|
|
foreach ($expected as $i => $expect) { |
31
|
|
|
$this->assertJsonSubset($expect, $actual, "Data set #$i: "); |
32
|
|
|
} |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function assertFloatEquals(float $expected, float $actual, int $precision = 6, string $message = '') |
36
|
|
|
{ |
37
|
|
|
if (\function_exists('bccomp')) { |
38
|
|
|
$this->assertSame(0, \bccomp($expected, $actual, $precision), $message); |
|
|
|
|
39
|
|
|
|
40
|
|
|
return; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
$this->assertEquals($expected, $actual, $message, \pow(10, 0 - \abs($precision))); |
|
|
|
|
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function assertArrayHasKeys(array $expectedKeys, array $actualArray, string $message = '') |
47
|
|
|
{ |
48
|
|
|
foreach ($expectedKeys as $key) { |
49
|
|
|
$this->assertArrayHasKey($key, $actualArray, $message); |
|
|
|
|
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function assertAll(array $expectations, $actual, array $messages = []) |
54
|
|
|
{ |
55
|
|
|
foreach ($expectations as $assert => $expected) { |
56
|
|
|
if (\strpos($assert, 'assert') === false) { |
57
|
|
|
$assert = 'assert' . \ucfirst($assert); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$msgKey = \lcfirst(\str_replace('assert', '', $assert)); |
61
|
|
|
|
62
|
|
|
$this->{$assert}($expected, $actual, $messages[$assert] ?? $messages[$msgKey] ?? null); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function assertIsAssocArray(array $array, string $message = '') |
67
|
|
|
{ |
68
|
|
|
$this->assertTrue(\array_keys($array) !== \range(0, \count($array) - 1), $message); |
|
|
|
|
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function assertIsNotAssocArray(array $array, string $message = '') |
72
|
|
|
{ |
73
|
|
|
$this->assertFalse(\array_keys($array) !== \range(0, \count($array) - 1), $message); |
|
|
|
|
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function assertFileIsExecutable(string $filePath, string $message = '') |
77
|
|
|
{ |
78
|
|
|
\clearstatcache(); |
79
|
|
|
|
80
|
|
|
$this->assertTrue(\is_executable($filePath), $message); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function assertFileIsNotExecutable(string $filePath, string $message = '') |
84
|
|
|
{ |
85
|
|
|
\clearstatcache(); |
86
|
|
|
|
87
|
|
|
$this->assertFalse(\is_executable($filePath), $message); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|