UtilsTest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. namespace JmesPath\Tests;
  3. use JmesPath\Utils;
  4. use PHPUnit\Framework\TestCase;
  5. class UtilsTest extends TestCase
  6. {
  7. public function typeProvider()
  8. {
  9. return [
  10. ['a', 'string'],
  11. [10, 'number'],
  12. [1.0, 'number'],
  13. [true, 'boolean'],
  14. [false, 'boolean'],
  15. [[], 'array'],
  16. [[1, 2], 'array'],
  17. [['a' => 1], 'object'],
  18. [new \stdClass(), 'object'],
  19. [function () {}, 'expression'],
  20. [new \ArrayObject(), 'array'],
  21. [new \ArrayObject([1, 2]), 'array'],
  22. [new \ArrayObject(['foo' => 'bar']), 'object'],
  23. [new _TestStr(), 'string']
  24. ];
  25. }
  26. /**
  27. * @dataProvider typeProvider
  28. */
  29. public function testGetsTypes($given, $type)
  30. {
  31. $this->assertEquals($type, Utils::type($given));
  32. }
  33. /**
  34. * @expectedException \InvalidArgumentException
  35. */
  36. public function testThrowsForInvalidArg()
  37. {
  38. Utils::type(new _TestClass());
  39. }
  40. public function isArrayProvider()
  41. {
  42. return [
  43. [[], true],
  44. [[1, 2], true],
  45. [['a' => 1], false],
  46. [new _TestClass(), false],
  47. [new \ArrayObject(['a' => 'b']), false],
  48. [new \ArrayObject([1]), true],
  49. [new \stdClass(), false]
  50. ];
  51. }
  52. /**
  53. * @dataProvider isArrayProvider
  54. */
  55. public function testChecksIfArray($given, $result)
  56. {
  57. $this->assertSame($result, Utils::isArray($given));
  58. }
  59. public function isObjectProvider()
  60. {
  61. return [
  62. [[], true],
  63. [[1, 2], false],
  64. [['a' => 1], true],
  65. [new _TestClass(), false],
  66. [new \ArrayObject(['a' => 'b']), true],
  67. [new \ArrayObject([1]), false],
  68. [new \stdClass(), true]
  69. ];
  70. }
  71. /**
  72. * @dataProvider isObjectProvider
  73. */
  74. public function testChecksIfObject($given, $result)
  75. {
  76. $this->assertSame($result, Utils::isObject($given));
  77. }
  78. public function testHasStableSort()
  79. {
  80. $data = [new _TestStr(), new _TestStr(), 0, 10, 2];
  81. $result = Utils::stableSort($data, function ($a, $b) {
  82. $a = (int) (string) $a;
  83. $b = (int) (string) $b;
  84. return $a > $b ? -1 : ($a == $b ? 0 : 1);
  85. });
  86. $this->assertSame($data[0], $result[0]);
  87. $this->assertSame($data[1], $result[1]);
  88. $this->assertEquals(10, $result[2]);
  89. $this->assertEquals(2, $result[3]);
  90. $this->assertEquals(0, $result[4]);
  91. }
  92. public function testSlicesArrays()
  93. {
  94. $this->assertEquals([3, 2, 1], Utils::slice([1, 2, 3], null, null, -1));
  95. $this->assertEquals([1, 3], Utils::slice([1, 2, 3], null, null, 2));
  96. $this->assertEquals([2, 3], Utils::slice([1, 2, 3], 1));
  97. }
  98. public function testSlicesStrings()
  99. {
  100. $this->assertEquals('cba', Utils::slice('abc', null, null, -1));
  101. $this->assertEquals('ac', Utils::slice('abc', null, null, 2));
  102. $this->assertEquals('bc', Utils::slice('abc', 1));
  103. }
  104. }
  105. class _TestClass implements \ArrayAccess
  106. {
  107. public function offsetExists($offset) {}
  108. public function offsetGet($offset) {}
  109. public function offsetSet($offset, $value) {}
  110. public function offsetUnset($offset) {}
  111. }
  112. class _TestStr
  113. {
  114. public function __toString()
  115. {
  116. return '100';
  117. }
  118. }