ObjectUtilsTest.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. namespace App\Tests\Unit\Service\Utils;
  3. use App\Service\Utils\ObjectUtils;
  4. use PHPUnit\Framework\TestCase;
  5. class ObjectUtilsTest extends TestCase
  6. {
  7. public function testHashWithEmptyArray()
  8. {
  9. $this->assertEquals(
  10. '44136fa355b3678a1146ad16f7e8649e94fb4fc21fe77e8310c060f61caaff8a',
  11. ObjectUtils::hash([])
  12. );
  13. $this->assertEquals(
  14. 'bf21a9e8fbc5a3846fb05b4fa0859e0917b2202f',
  15. ObjectUtils::hash([], 'sha1')
  16. );
  17. }
  18. public function testHashWithArray()
  19. {
  20. $this->assertEquals(
  21. 'e6a3385fb77c287a712e7f406a451727f0625041823ecf23bea7ef39b2e39805',
  22. ObjectUtils::hash(['a' => 1, 'b' => 2, 'c' => 3])
  23. );
  24. $this->assertEquals(
  25. 'e7ec4a8f2309bdd4c4c57cb2adfb79c91a293597',
  26. ObjectUtils::hash(['a' => 1, 'b' => 2, 'c' => 3], 'sha1')
  27. );
  28. }
  29. public function testHashWithUnsortedArray()
  30. {
  31. $this->assertEquals(
  32. ObjectUtils::hash(['a' => 1, 'b' => 2, 'c' => 3]),
  33. ObjectUtils::hash(['b' => 2, 'a' => 1, 'c' => 3])
  34. );
  35. $this->assertEquals(
  36. ObjectUtils::hash(['a' => 1, 'b' => 2, 'c' => 3], 'sha1'),
  37. ObjectUtils::hash(['b' => 2, 'a' => 1, 'c' => 3], 'sha1')
  38. );
  39. }
  40. public function testHashWithObject()
  41. {
  42. $this->assertEquals(
  43. 'e6a3385fb77c287a712e7f406a451727f0625041823ecf23bea7ef39b2e39805',
  44. ObjectUtils::hash((object) ['a' => 1, 'b' => 2, 'c' => 3])
  45. );
  46. }
  47. }