1+ <?php
2+ namespace tester ;
3+
4+
5+ use php \lib \reflect ;
6+ use php \lib \str ;
7+
8+ class Assert
9+ {
10+ /**
11+ * @param $value
12+ * @return string
13+ */
14+ static protected function formatValue ($ value ): string
15+ {
16+ if ($ value === null ) {
17+ return "NULL " ;
18+ }
19+
20+ if (is_object ($ value )) {
21+ return "instance of " . reflect::typeOf ($ value );
22+ }
23+
24+ if (is_array ($ value )) {
25+ $ result = var_export ($ value , true );
26+ if (str::length ($ result ) > 255 ) {
27+ return "Array< " . sizeof ($ value ) . "> " ;
28+ } else {
29+ return $ result ;
30+ }
31+ }
32+
33+ return var_export ($ value , true );
34+ }
35+
36+ /**
37+ * @param string $message
38+ * @param array ...$args
39+ * @throws AssertionError
40+ */
41+ static function fail (string $ message , $ formatted , ...$ args )
42+ {
43+ foreach ($ args as $ i => $ arg ) {
44+ $ formatted = str::replace ($ formatted , "\{ $ i\} " , Assert::formatValue ($ arg ));
45+ }
46+
47+ if (isset ($ message ) && $ message !== '' ) {
48+ $ formatted = "$ message, $ formatted " ;
49+ }
50+
51+ throw new AssertionError ($ formatted );
52+ }
53+
54+ /**
55+ * @param $expected
56+ * @param $actual
57+ * @param string|null $message
58+ * @throws AssertionError
59+ */
60+ static function isEqual ($ expected , $ actual , string $ message = null )
61+ {
62+ if ($ expected != $ actual ) {
63+ Assert::fail ($ message , "expected: {0}, but was: {1} " , $ expected , $ actual );
64+ }
65+ }
66+
67+ /**
68+ * @param $expected
69+ * @param $actual
70+ * @param string|null $message
71+ */
72+ static function isIdentical ($ expected , $ actual , string $ message = null )
73+ {
74+ if ($ expected !== $ actual ) {
75+ Assert::fail ($ message , 'expected: {0}, but was: {1}, is not identical ' , $ expected , $ actual );
76+ }
77+ }
78+
79+ /**
80+ * @param $actual
81+ * @param string|null $message
82+ */
83+ static function isNull ($ actual , string $ message = null )
84+ {
85+ if ($ actual !== null ) {
86+ Assert::fail ($ message , "expected NULL, but was: {0} " , $ actual );
87+ }
88+ }
89+
90+ /**
91+ * @param $actual
92+ * @param string|null $message
93+ */
94+ static function isNotNull ($ actual , string $ message = null )
95+ {
96+ if ($ actual === null ) {
97+ Assert::fail ($ message , "expected not NULL, but was: {0} " , $ actual );
98+ }
99+ }
100+
101+ /**
102+ * @param $actual
103+ * @param string|null $message
104+ */
105+ static function isTrue ($ actual , string $ message = null )
106+ {
107+ if ($ actual !== true ) {
108+ Assert::fail ($ message , "expected true, but was: {0} " , $ actual );
109+ }
110+ }
111+
112+ /**
113+ * @param $actual
114+ * @param string|null $message
115+ */
116+ static function isFalse ($ actual , string $ message = null )
117+ {
118+ if ($ actual !== false ) {
119+ Assert::fail ($ message , "expected false, but was: {0} " , $ actual );
120+ }
121+ }
122+
123+ /**
124+ * @param $actual
125+ * @param string|null $message
126+ */
127+ static function isEmpty ($ actual , string $ message = null )
128+ {
129+ if (!empty ($ actual )) {
130+ Assert::fail ($ message , "expected empty, but was: {0} " , $ actual );
131+ }
132+ }
133+
134+ /**
135+ * @param $actual
136+ * @param string|null $message
137+ */
138+ static function isNotEmpty ($ actual , string $ message = null )
139+ {
140+ if (empty ($ actual )) {
141+ Assert::fail ($ message , "expected not empty, but was: {0} " , $ actual );
142+ }
143+ }
144+ }
0 commit comments