Last active 1740032735

php

anthony's Avatar anthony revised this gist 1740032734. Go to revision

1 file changed, 134 insertions

BasicTestCase.php(file created)

@@ -0,0 +1,134 @@
1 + <?php
2 +
3 + declare(strict_types=1);
4 +
5 + namespace Tests;
6 +
7 + use Illuminate\Support\Collection;
8 + use PHPUnit\Framework\TestCase;
9 +
10 + class BasicTestCase extends TestCase
11 + {
12 + /**
13 + * Asserts that $actual object or class is same as $expected one
14 + *
15 + * @param object|string $expected Object of any class or class name to check against to
16 + * @param object|string $actual Object of any class or class name to check
17 + */
18 + public function assertIsSameClass(object|string $expected, object|string $actual): void
19 + {
20 + $this->assertTrue($this->checkIsSameClass($expected, $actual));
21 + }
22 +
23 + /**
24 + * Checks that $actual object or class is same as $expected one
25 + *
26 + * @param object|string $class1 Object of any class or class name to check against to
27 + * @param object|string $class2 Object of any class or class name to check
28 + * @return bool
29 + */
30 + protected function checkIsSameClass(object|string $class1, object|string $class2): bool
31 + {
32 + return (is_object($class1) ? $class1::class : $class1) === (is_object($class2) ? $class2::class : $class2);
33 + }
34 +
35 + /**
36 + * Asserts that $actual object or class extends $expected one
37 + *
38 + * @param array $expected Array of classes to check against to
39 + * @param object|string $actual Object of any class or class name to check
40 + */
41 + public function assertExtendsClasses(array $expected, object|string $actual): void
42 + {
43 + $this->assertTrue($this->checkExtendsClasses($expected, $actual));
44 + }
45 +
46 + /**
47 + * Checks that $actual object or class extends $expected one
48 + *
49 + * @param string[] $parents Array of parent classes to check against to
50 + * @param object|string $class Object of any class or class name to check
51 + * @see https://www.php.net/manual/en/function.class-parents.php
52 + */
53 + protected function checkExtendsClasses(array $parents, object|string $class): bool
54 + {
55 + return !empty(array_intersect($parents, is_object($class) ? class_parents($class) : [$class]));
56 + }
57 +
58 + /**
59 + * Asserts that $actual object or class implements $expected interfaces
60 + *
61 + * @param string[] $expected Array of interface classes to check against to
62 + * @param object|string $actual Object of any class or class name to check
63 + * @see https://www.php.net/manual/en/function.class-implements.php
64 + */
65 + public function assertImplementsInterfaces(array $expected, object|string $actual): void
66 + {
67 + $this->assertNotEmpty(array_intersect($expected, is_object($actual) ? class_implements($actual) : [$actual]));
68 + }
69 +
70 + /**
71 + * Asserts that $actual object or class uses $expected traits
72 + *
73 + * @param string[] $expected Array of trait classes to check against to
74 + * @param object|string $actual Object of any class or class name to check
75 + * @see https://www.php.net/manual/en/function.class-uses.php#110752
76 + */
77 + public function assertUsesTraits(array $expected, object|string $actual): void
78 + {
79 + $found_traits = [];
80 + $check_class = is_object($actual) ? $actual::class : $actual;
81 + do {
82 + $found_traits = array_merge(class_uses($check_class, true), $found_traits);
83 + } while ($check_class = get_parent_class($check_class));
84 + foreach ($found_traits as $trait => $same) {
85 + $found_traits = array_merge(class_uses($trait, true), $found_traits);
86 + }
87 + $this->assertNotEmpty(array_intersect(array_unique($found_traits), $expected));
88 + }
89 +
90 + /**
91 + * Asserts that $value is instance Laravel Collection
92 + *
93 + * @param mixed $value
94 + */
95 + public function assertIsCollection(mixed $value): void
96 + {
97 + $this->assertIsObject($value);
98 + $this->assertIsIterable($value);
99 + $this->assertTrue(
100 + $this->checkIsSameClass(Collection::class, $value)
101 + || $this->checkExtendsClasses([Collection::class], $value)
102 + );
103 + }
104 +
105 + /**
106 + * Asserts that $array has all the $keys
107 + *
108 + * @param array $keys Keys to check
109 + * @param array|ArrayAccess|Arrayable $array Array in which to check keys presence
110 + * @return void
111 + */
112 + public function assertArrayHasKeys(array $keys, array|ArrayAccess|Arrayable $array): void
113 + {
114 + $array instanceof Arrayable && $array = $array->toArray();
115 + foreach ($keys as $key) {
116 + $this->assertArrayHasKey($key, $array);
117 + }
118 + }
119 +
120 + /**
121 + * Asserts that $array has not all the $keys
122 + *
123 + * @param array $keys Keys to check
124 + * @param array|ArrayAccess|Arrayable $array Array in which to check keys absence
125 + * @return void
126 + */
127 + public function assertArrayHasNotKeys(array $keys, array|ArrayAccess|Arrayable $array): void
128 + {
129 + $array instanceof Arrayable && $array = $array->toArray();
130 + foreach ($keys as $key) {
131 + $this->assertArrayNotHasKey($key, $array);
132 + }
133 + }
134 + }
Newer Older