PageRenderTime 43ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/web/core/tests/Drupal/Tests/Component/Assertion/InspectorTest.php

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