UtilsTest.php 3.4 KB

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