UtilsTest.php 3.5 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 static function typeProvider(): array
  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, string $type): void
  30. {
  31. $this->assertEquals($type, Utils::type($given));
  32. }
  33. public function testThrowsForInvalidArg(): void
  34. {
  35. $this->expectException(\InvalidArgumentException::class);
  36. Utils::type(new _TestClass());
  37. }
  38. public static function isArrayProvider(): array
  39. {
  40. return [
  41. [[], true],
  42. [[1, 2], true],
  43. [['a' => 1], false],
  44. [new _TestClass(), false],
  45. [new \ArrayObject(['a' => 'b']), false],
  46. [new \ArrayObject([1]), true],
  47. [new \stdClass(), false]
  48. ];
  49. }
  50. /**
  51. * @dataProvider isArrayProvider
  52. */
  53. public function testChecksIfArray($given, bool $result)
  54. {
  55. $this->assertSame($result, Utils::isArray($given));
  56. }
  57. public static function isObjectProvider(): array
  58. {
  59. return [
  60. [[], true],
  61. [[1, 2], false],
  62. [['a' => 1], true],
  63. [new _TestClass(), false],
  64. [new \ArrayObject(['a' => 'b']), true],
  65. [new \ArrayObject([1]), false],
  66. [new \stdClass(), true]
  67. ];
  68. }
  69. /**
  70. * @dataProvider isObjectProvider
  71. */
  72. public function testChecksIfObject($given, $result): void
  73. {
  74. $this->assertSame($result, Utils::isObject($given));
  75. }
  76. public function testHasStableSort(): void
  77. {
  78. $data = [new _TestStr(), new _TestStr(), 0, 10, 2];
  79. $result = Utils::stableSort($data, function ($a, $b) {
  80. $a = (int) (string) $a;
  81. $b = (int) (string) $b;
  82. return $a > $b ? -1 : ($a == $b ? 0 : 1);
  83. });
  84. $this->assertSame($data[0], $result[0]);
  85. $this->assertSame($data[1], $result[1]);
  86. $this->assertEquals(10, $result[2]);
  87. $this->assertEquals(2, $result[3]);
  88. $this->assertEquals(0, $result[4]);
  89. }
  90. public function testSlicesArrays(): void
  91. {
  92. $this->assertEquals([3, 2, 1], Utils::slice([1, 2, 3], null, null, -1));
  93. $this->assertEquals([1, 3], Utils::slice([1, 2, 3], null, null, 2));
  94. $this->assertEquals([2, 3], Utils::slice([1, 2, 3], 1));
  95. }
  96. public function testSlicesStrings(): void
  97. {
  98. $this->assertEquals('cba', Utils::slice('abc', null, null, -1));
  99. $this->assertEquals('ac', Utils::slice('abc', null, null, 2));
  100. $this->assertEquals('bc', Utils::slice('abc', 1));
  101. }
  102. }
  103. class _TestClass implements \ArrayAccess
  104. {
  105. public function offsetExists($offset): bool {}
  106. #[\ReturnTypeWillChange]
  107. public function offsetGet($offset) {}
  108. public function offsetSet($offset, $value): void {}
  109. public function offsetUnset($offset): void {}
  110. }
  111. class _TestStr
  112. {
  113. public function __toString(): string
  114. {
  115. return '100';
  116. }
  117. }