5 * Contains \Drupal\Tests\Component\Assertion\InspectorTest.
8 namespace Drupal\Tests\Component\Assertion;
10 use PHPUnit_Framework_TestCase;
11 use Drupal\Component\Assertion\Inspector;
14 * @coversDefaultClass \Drupal\Component\Assertion\Inspector
17 class InspectorTest extends PHPUnit_Framework_TestCase {
20 * Tests asserting argument is an array or traversable object.
22 * @covers ::assertTraversable
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'));
32 * Tests asserting all members are strings.
34 * @covers ::assertAllStrings
35 * @dataProvider providerTestAssertAllStrings
37 public function testAssertAllStrings($input, $expected) {
38 $this->assertSame($expected, Inspector::assertAllStrings($input));
41 public function providerTestAssertAllStrings() {
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],
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],
68 * Tests asserting all members are strings or objects with __toString().
70 * @covers ::assertAllStringable
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()]));
80 * Tests asserting all members are arrays.
82 * @covers ::assertAllArrays
84 public function testAssertAllArrays() {
85 $this->assertTrue(Inspector::assertAllArrays([]));
86 $this->assertTrue(Inspector::assertAllArrays([[], []]));
87 $this->assertFalse(Inspector::assertAllArrays([[], 'foo']));
91 * Tests asserting array is 0-indexed - the strict definition of array.
93 * @covers ::assertStrictArray
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']));
102 * Tests asserting all members are strict arrays.
104 * @covers ::assertAllStrictArrays
106 public function testAssertAllStrictArrays() {
107 $this->assertTrue(Inspector::assertAllStrictArrays([]));
108 $this->assertTrue(Inspector::assertAllStrictArrays([[], []]));
109 $this->assertFalse(Inspector::assertAllStrictArrays([['foo' => 'bar', 'bar' => 'foo']]));
113 * Tests asserting all members have specified keys.
115 * @covers ::assertAllHaveKey
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'));
126 * Tests asserting all members are integers.
128 * @covers ::assertAllIntegers
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]));
138 * Tests asserting all members are floating point variables.
140 * @covers ::assertAllFloat
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']));
151 * Tests asserting all members are callable.
153 * @covers ::assertAllCallable
155 public function testAllCallable() {
156 $this->assertTrue(Inspector::assertAllCallable([
159 [__CLASS__, 'callMeStatic'],
165 $this->assertFalse(Inspector::assertAllCallable([
168 [__CLASS__, 'callMeStatic'],
177 * Tests asserting all members are !empty().
179 * @covers ::assertAllNotEmpty
181 public function testAllNotEmpty() {
182 $this->assertTrue(Inspector::assertAllNotEmpty([1, 'two']));
183 $this->assertFalse(Inspector::assertAllNotEmpty(['']));
187 * Tests asserting all arguments are numbers or strings castable to numbers.
189 * @covers ::assertAllNumeric
191 public function testAssertAllNumeric() {
192 $this->assertTrue(Inspector::assertAllNumeric([1, '2', 3.14]));
193 $this->assertFalse(Inspector::assertAllNumeric([1, 'two', 3.14]));
197 * Tests asserting strstr() or stristr() match.
199 * @covers ::assertAllMatch
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]));
211 * Tests asserting regular expression match.
213 * @covers ::assertAllRegularExpressionMatch
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]));
225 * Tests asserting all members are objects.
227 * @covers ::assertAllObjects
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'));
240 * Test method referenced by ::testAllCallable().
242 public function callMe() {
247 * Test method referenced by ::testAllCallable().
249 public static function callMeStatic() {
256 * Quick class for testing for objects with __toString.
262 public function __toString() {