]> git.mxchange.org Git - friendica.git/blob - tests/src/Util/ArraysTest.php
Add more special chars at tests
[friendica.git] / tests / src / Util / ArraysTest.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Test\src\Util;
23
24 use Friendica\Util\Arrays;
25 use PHPUnit\Framework\TestCase;
26
27 /**
28  * Array utility testing class
29  */
30 class ArraysTest extends TestCase
31 {
32         /**
33          * Tests if an empty array and an empty delimiter returns an empty string.
34          */
35         public function testEmptyArrayEmptyDelimiter()
36         {
37                 $str = Arrays::recursiveImplode([], '');
38                 self::assertEmpty($str);
39         }
40
41         /**
42          * Tests if an empty array and a non-empty delimiter returns an empty string.
43          */
44         public function testEmptyArrayNonEmptyDelimiter()
45         {
46                 $str = Arrays::recursiveImplode([], ',');
47                 self::assertEmpty($str);
48         }
49
50         /**
51          * Tests if a non-empty array and an empty delimiter returns the value (1).
52          */
53         public function testNonEmptyArrayEmptyDelimiter()
54         {
55                 $str = Arrays::recursiveImplode([1], '');
56                 self::assertSame($str, '1');
57         }
58
59         /**
60          * Tests if a non-empty array and an empty delimiter returns the value (12).
61          */
62         public function testNonEmptyArray2EmptyDelimiter()
63         {
64                 $str = Arrays::recursiveImplode([1, 2], '');
65                 self::assertSame($str, '12');
66         }
67
68         /**
69          * Tests if a non-empty array and a non-empty delimiter returns the value (1).
70          */
71         public function testNonEmptyArrayNonEmptyDelimiter()
72         {
73                 $str = Arrays::recursiveImplode([1], ',');
74                 self::assertSame($str, '1');
75         }
76
77         /**
78          * Tests if a non-empty array and a non-empty delimiter returns the value (1,2).
79          */
80         public function testNonEmptyArray2NonEmptyDelimiter()
81         {
82                 $str = Arrays::recursiveImplode([1, 2], ',');
83                 self::assertSame($str, '1,2');
84         }
85
86         /**
87          * Tests if a 2-dim array and an empty delimiter returns the expected string.
88          */
89         public function testEmptyMultiArray2EmptyDelimiter()
90         {
91                 $str = Arrays::recursiveImplode([[1], []], '');
92                 self::assertSame($str, '{1}{}');
93         }
94
95         /**
96          * Tests if a 2-dim array and an empty delimiter returns the expected string.
97          */
98         public function testEmptyMulti2Array2EmptyDelimiter()
99         {
100                 $str = Arrays::recursiveImplode([[1], [2]], '');
101                 self::assertSame($str, '{1}{2}');
102         }
103
104         /**
105          * Tests if a 2-dim array and a non-empty delimiter returns the expected string.
106          */
107         public function testEmptyMultiArray2NonEmptyDelimiter()
108         {
109                 $str = Arrays::recursiveImplode([[1], []], ',');
110                 self::assertSame($str, '{1},{}');
111         }
112
113         /**
114          * Tests if a 2-dim array and a non-empty delimiter returns the expected string.
115          */
116         public function testEmptyMulti2Array2NonEmptyDelimiter()
117         {
118                 $str = Arrays::recursiveImplode([[1], [2]], ',');
119                 self::assertSame($str, '{1},{2}');
120         }
121
122         /**
123          * Tests if a 3-dim array and a non-empty delimiter returns the expected string.
124          */
125         public function testEmptyMulti3Array2NonEmptyDelimiter()
126         {
127                 $str = Arrays::recursiveImplode([[1], [2, [3]]], ',');
128                 self::assertSame($str, '{1},{2,{3}}');
129         }
130
131         /**
132          * Test the Arrays::walkRecursive() function.
133          */
134         public function testApiWalkRecursive()
135         {
136                 $array = ['item1'];
137                 self::assertEquals(
138                         $array,
139                         Arrays::walkRecursive(
140                                 $array,
141                                 function () {
142                                         // Should we test this with a callback that actually does something?
143                                         return true;
144                                 }
145                         )
146                 );
147         }
148
149         /**
150          * Test the Arrays::walkRecursive() function with an array.
151          *
152          * @return void
153          */
154         public function testApiWalkRecursiveWithArray()
155         {
156                 $array = [['item1'], ['item2']];
157                 self::assertEquals(
158                         $array,
159                         Arrays::walkRecursive(
160                                 $array,
161                                 function () {
162                                         // Should we test this with a callback that actually does something?
163                                         return true;
164                                 }
165                         )
166                 );
167         }
168 }