More updates to stop using dev or alpha or beta versions.
[yaffs-website] / web / core / tests / Drupal / Tests / Component / Assertion / InspectorTest.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Tests\Component\Assertion\InspectorTest.
6  */
7
8 namespace Drupal\Tests\Component\Assertion;
9
10 use PHPUnit\Framework\TestCase;
11 use Drupal\Component\Assertion\Inspector;
12
13 /**
14  * @coversDefaultClass \Drupal\Component\Assertion\Inspector
15  * @group Assertion
16  */
17 class InspectorTest extends TestCase {
18
19   /**
20    * Tests asserting argument is an array or traversable object.
21    *
22    * @covers ::assertTraversable
23    */
24   public function testAssertTraversable() {
25     $this->assertTrue(Inspector::assertTraversable([]));
26     $this->assertTrue(Inspector::assertTraversable(new \ArrayObject()));
27     $this->assertFalse(Inspector::assertTraversable(new \stdClass()));
28     $this->assertFalse(Inspector::assertTraversable('foo'));
29   }
30
31   /**
32    * Tests asserting all members are strings.
33    *
34    * @covers ::assertAllStrings
35    * @dataProvider providerTestAssertAllStrings
36    */
37   public function testAssertAllStrings($input, $expected) {
38     $this->assertSame($expected, Inspector::assertAllStrings($input));
39   }
40
41   public function providerTestAssertAllStrings() {
42     $data = [
43       'empty-array' => [[], TRUE],
44       'array-with-strings' => [['foo', 'bar'], TRUE],
45       'string' => ['foo', FALSE],
46       'array-with-strings-with-colon' => [['foo', 'bar', 'llama:2001988', 'baz', 'llama:14031991'], TRUE],
47
48       'with-FALSE' => [[FALSE], FALSE],
49       'with-TRUE' => [[TRUE], FALSE],
50       'with-string-and-boolean' => [['foo', FALSE], FALSE],
51       'with-NULL' => [[NULL], FALSE],
52       'string-with-NULL' => [['foo', NULL], FALSE],
53       'integer' => [[1337], FALSE],
54       'string-and-integer' => [['foo', 1337], FALSE],
55       'double' => [[3.14], FALSE],
56       'string-and-double' => [['foo', 3.14], FALSE],
57       'array' => [[[]], FALSE],
58       'string-and-array' => [['foo', []], FALSE],
59       'string-and-nested-array' => [['foo', ['bar']], FALSE],
60       'object' => [[new \stdClass()], FALSE],
61       'string-and-object' => [['foo', new StringObject()], FALSE],
62     ];
63
64     return $data;
65   }
66
67   /**
68    * Tests asserting all members are strings or objects with __toString().
69    *
70    * @covers ::assertAllStringable
71    */
72   public function testAssertAllStringable() {
73     $this->assertTrue(Inspector::assertAllStringable([]));
74     $this->assertTrue(Inspector::assertAllStringable(['foo', 'bar']));
75     $this->assertFalse(Inspector::assertAllStringable('foo'));
76     $this->assertTrue(Inspector::assertAllStringable(['foo', new StringObject()]));
77   }
78
79   /**
80    * Tests asserting all members are arrays.
81    *
82    * @covers ::assertAllArrays
83    */
84   public function testAssertAllArrays() {
85     $this->assertTrue(Inspector::assertAllArrays([]));
86     $this->assertTrue(Inspector::assertAllArrays([[], []]));
87     $this->assertFalse(Inspector::assertAllArrays([[], 'foo']));
88   }
89
90   /**
91    * Tests asserting array is 0-indexed - the strict definition of array.
92    *
93    * @covers ::assertStrictArray
94    */
95   public function testAssertStrictArray() {
96     $this->assertTrue(Inspector::assertStrictArray([]));
97     $this->assertTrue(Inspector::assertStrictArray(['bar', 'foo']));
98     $this->assertFalse(Inspector::assertStrictArray(['foo' => 'bar', 'bar' => 'foo']));
99   }
100
101   /**
102    * Tests asserting all members are strict arrays.
103    *
104    * @covers ::assertAllStrictArrays
105    */
106   public function testAssertAllStrictArrays() {
107     $this->assertTrue(Inspector::assertAllStrictArrays([]));
108     $this->assertTrue(Inspector::assertAllStrictArrays([[], []]));
109     $this->assertFalse(Inspector::assertAllStrictArrays([['foo' => 'bar', 'bar' => 'foo']]));
110   }
111
112   /**
113    * Tests asserting all members have specified keys.
114    *
115    * @covers ::assertAllHaveKey
116    */
117   public function testAssertAllHaveKey() {
118     $this->assertTrue(Inspector::assertAllHaveKey([]));
119     $this->assertTrue(Inspector::assertAllHaveKey([['foo' => 'bar', 'bar' => 'foo']]));
120     $this->assertTrue(Inspector::assertAllHaveKey([['foo' => 'bar', 'bar' => 'foo']], 'foo'));
121     $this->assertTrue(Inspector::assertAllHaveKey([['foo' => 'bar', 'bar' => 'foo']], 'bar', 'foo'));
122     $this->assertFalse(Inspector::assertAllHaveKey([['foo' => 'bar', 'bar' => 'foo']], 'bar', 'foo', 'moo'));
123   }
124
125   /**
126    * Tests asserting all members are integers.
127    *
128    * @covers ::assertAllIntegers
129    */
130   public function testAssertAllIntegers() {
131     $this->assertTrue(Inspector::assertAllIntegers([]));
132     $this->assertTrue(Inspector::assertAllIntegers([1, 2, 3]));
133     $this->assertFalse(Inspector::assertAllIntegers([1, 2, 3.14]));
134     $this->assertFalse(Inspector::assertAllIntegers([1, '2', 3]));
135   }
136
137   /**
138    * Tests asserting all members are floating point variables.
139    *
140    * @covers ::assertAllFloat
141    */
142   public function testAssertAllFloat() {
143     $this->assertTrue(Inspector::assertAllFloat([]));
144     $this->assertTrue(Inspector::assertAllFloat([1.0, 2.1, 3.14]));
145     $this->assertFalse(Inspector::assertAllFloat([1, 2.1, 3.14]));
146     $this->assertFalse(Inspector::assertAllFloat([1.0, '2', 3]));
147     $this->assertFalse(Inspector::assertAllFloat(['Titanic']));
148   }
149
150   /**
151    * Tests asserting all members are callable.
152    *
153    * @covers ::assertAllCallable
154    */
155   public function testAllCallable() {
156     $this->assertTrue(Inspector::assertAllCallable([
157       'strchr',
158       [$this, 'callMe'],
159       [__CLASS__, 'callMeStatic'],
160       function () {
161         return TRUE;
162       }
163     ]));
164
165     $this->assertFalse(Inspector::assertAllCallable([
166       'strchr',
167       [$this, 'callMe'],
168       [__CLASS__, 'callMeStatic'],
169       function () {
170         return TRUE;
171       },
172       "I'm not callable"
173     ]));
174   }
175
176   /**
177    * Tests asserting all members are !empty().
178    *
179    * @covers ::assertAllNotEmpty
180    */
181   public function testAllNotEmpty() {
182     $this->assertTrue(Inspector::assertAllNotEmpty([1, 'two']));
183     $this->assertFalse(Inspector::assertAllNotEmpty(['']));
184   }
185
186   /**
187    * Tests asserting all arguments are numbers or strings castable to numbers.
188    *
189    * @covers ::assertAllNumeric
190    */
191   public function testAssertAllNumeric() {
192     $this->assertTrue(Inspector::assertAllNumeric([1, '2', 3.14]));
193     $this->assertFalse(Inspector::assertAllNumeric([1, 'two', 3.14]));
194   }
195
196   /**
197    * Tests asserting strstr() or stristr() match.
198    *
199    * @covers ::assertAllMatch
200    */
201   public function testAssertAllMatch() {
202     $this->assertTrue(Inspector::assertAllMatch('f', ['fee', 'fi', 'fo']));
203     $this->assertTrue(Inspector::assertAllMatch('F', ['fee', 'fi', 'fo']));
204     $this->assertTrue(Inspector::assertAllMatch('f', ['fee', 'fi', 'fo'], TRUE));
205     $this->assertFalse(Inspector::assertAllMatch('F', ['fee', 'fi', 'fo'], TRUE));
206     $this->assertFalse(Inspector::assertAllMatch('e', ['fee', 'fi', 'fo']));
207     $this->assertFalse(Inspector::assertAllMatch('1', [12]));
208   }
209
210   /**
211    * Tests asserting regular expression match.
212    *
213    * @covers ::assertAllRegularExpressionMatch
214    */
215   public function testAssertAllRegularExpressionMatch() {
216     $this->assertTrue(Inspector::assertAllRegularExpressionMatch('/f/i', ['fee', 'fi', 'fo']));
217     $this->assertTrue(Inspector::assertAllRegularExpressionMatch('/F/i', ['fee', 'fi', 'fo']));
218     $this->assertTrue(Inspector::assertAllRegularExpressionMatch('/f/', ['fee', 'fi', 'fo']));
219     $this->assertFalse(Inspector::assertAllRegularExpressionMatch('/F/', ['fee', 'fi', 'fo']));
220     $this->assertFalse(Inspector::assertAllRegularExpressionMatch('/e/', ['fee', 'fi', 'fo']));
221     $this->assertFalse(Inspector::assertAllRegularExpressionMatch('/1/', [12]));
222   }
223
224   /**
225    * Tests asserting all members are objects.
226    *
227    * @covers ::assertAllObjects
228    */
229   public function testAssertAllObjects() {
230     $this->assertTrue(Inspector::assertAllObjects([new \ArrayObject(), new \ArrayObject()]));
231     $this->assertFalse(Inspector::assertAllObjects([new \ArrayObject(), new \ArrayObject(), 'foo']));
232     $this->assertTrue(Inspector::assertAllObjects([new \ArrayObject(), new \ArrayObject()], '\\Traversable'));
233     $this->assertFalse(Inspector::assertAllObjects([new \ArrayObject(), new \ArrayObject(), 'foo'], '\\Traversable'));
234     $this->assertFalse(Inspector::assertAllObjects([new \ArrayObject(), new StringObject()], '\\Traversable'));
235     $this->assertTrue(Inspector::assertAllObjects([new \ArrayObject(), new StringObject()], '\\Traversable', '\\Drupal\\Tests\\Component\\Assertion\\StringObject'));
236     $this->assertFalse(Inspector::assertAllObjects([new \ArrayObject(), new StringObject(), new \stdClass()], '\\ArrayObject', '\\Drupal\\Tests\\Component\\Assertion\\StringObject'));
237   }
238
239   /**
240    * Test method referenced by ::testAllCallable().
241    */
242   public function callMe() {
243     return TRUE;
244   }
245
246   /**
247    * Test method referenced by ::testAllCallable().
248    */
249   public static function callMeStatic() {
250     return TRUE;
251   }
252
253 }
254
255 /**
256  * Quick class for testing for objects with __toString.
257  */
258 class StringObject {
259   /**
260    * {@inheritdoc}
261    */
262   public function __toString() {
263     return 'foo';
264   }
265
266 }