HashTableTest.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheetTests;
  3. use PhpOffice\PhpSpreadsheet\Comment; // need Comparable object
  4. use PhpOffice\PhpSpreadsheet\HashTable;
  5. use PHPUnit\Framework\TestCase;
  6. class HashTableTest extends TestCase
  7. {
  8. public static function createArray(): array
  9. {
  10. $comment1 = new Comment();
  11. $comment1->setAuthor('Author1');
  12. $comment2 = new Comment();
  13. $comment2->setAuthor('Author2');
  14. return [$comment1, $comment2];
  15. }
  16. /**
  17. * @param mixed $comment
  18. */
  19. public static function getAuthor($comment): string
  20. {
  21. return ($comment instanceof Comment) ? $comment->getAuthor() : '';
  22. }
  23. public function testAddRemoveClear(): void
  24. {
  25. $array1 = self::createArray();
  26. $hash1 = new HashTable($array1);
  27. self::assertSame(2, $hash1->count());
  28. $comment3 = new Comment();
  29. $comment3->setAuthor('Author3');
  30. $hash1->add($comment3);
  31. $comment4 = new Comment();
  32. $comment4->setAuthor('Author4');
  33. $hash1->add($comment4);
  34. $comment5 = new Comment();
  35. $comment5->setAuthor('Author5');
  36. // don't add comment5
  37. self::assertSame(4, $hash1->count());
  38. self::assertNull($hash1->getByIndex(10));
  39. $comment = $hash1->getByIndex(2);
  40. self::assertSame('Author3', self::getAuthor($comment));
  41. $hash1->remove($comment3);
  42. self::assertSame(3, $hash1->count());
  43. $comment = $hash1->getByIndex(2);
  44. self::assertSame('Author4', self::getAuthor($comment));
  45. $hash1->remove($comment5);
  46. self::assertSame(3, $hash1->count(), 'Remove non-hash member');
  47. $comment = $hash1->getByIndex(2);
  48. self::assertSame('Author4', self::getAuthor($comment));
  49. self::assertNull($hash1->getByHashCode('xyz'));
  50. $hash1->clear();
  51. self::AssertSame(0, $hash1->count());
  52. }
  53. public function testToArray(): void
  54. {
  55. $array1 = self::createArray();
  56. $count1 = count($array1);
  57. $hash1 = new HashTable($array1);
  58. $array2 = $hash1->toArray();
  59. self::assertCount($count1, $array2);
  60. $idx = 0;
  61. foreach ($array2 as $key => $value) {
  62. self::assertEquals($array1[$idx], $value, "Item $idx");
  63. self::assertSame($idx, $hash1->getIndexForHashCode($key));
  64. ++$idx;
  65. }
  66. }
  67. public function testClone(): void
  68. {
  69. $array1 = self::createArray();
  70. $hash1 = new HashTable($array1);
  71. $hash2 = new HashTable();
  72. self::assertSame(0, $hash2->count());
  73. $hash2->addFromSource();
  74. self::assertSame(0, $hash2->count());
  75. $hash2->addFromSource($array1);
  76. self::assertSame(2, $hash2->count());
  77. self::assertEquals($hash1, $hash2, 'Add in constructor same as addFromSource');
  78. $hash3 = clone $hash1;
  79. self::assertEquals($hash1, $hash3, 'Clone equal to original');
  80. self::assertSame($hash1->getByIndex(0), $hash2->getByIndex(0));
  81. self::assertEquals($hash1->getByIndex(0), $hash3->getByIndex(0));
  82. self::assertNotSame($hash1->getByIndex(0), $hash3->getByIndex(0));
  83. }
  84. }