HashTableTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. /**
  3. * This file is part of PHPPresentation - A pure PHP library for reading and writing
  4. * presentations documents.
  5. *
  6. * PHPPresentation is free software distributed under the terms of the GNU Lesser
  7. * General Public License version 3 as published by the Free Software Foundation.
  8. *
  9. * For the full copyright and license information, please read the LICENSE
  10. * file that was distributed with this source code. For the full list of
  11. * contributors, visit https://github.com/PHPOffice/PHPPresentation/contributors.
  12. *
  13. * @copyright 2009-2015 PHPPresentation contributors
  14. * @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
  15. * @link https://github.com/PHPOffice/PHPPresentation
  16. */
  17. namespace PhpOffice\PhpPresentation\Tests;
  18. use PhpOffice\PhpPresentation\HashTable;
  19. use PhpOffice\PhpPresentation\Slide;
  20. /**
  21. * Test class for HashTable
  22. *
  23. * @coversDefaultClass PhpOffice\PhpPresentation\HashTable
  24. */
  25. class HashTableTest extends \PHPUnit_Framework_TestCase
  26. {
  27. /**
  28. */
  29. public function testConstructNull()
  30. {
  31. $object = new HashTable();
  32. $this->assertEquals(0, $object->count());
  33. $this->assertNull($object->getByIndex());
  34. $this->assertNull($object->getByHashCode());
  35. $this->assertInternalType('array', $object->toArray());
  36. $this->assertEmpty($object->toArray());
  37. }
  38. /**
  39. */
  40. public function testConstructSource()
  41. {
  42. $object = new HashTable(array(
  43. new Slide(),
  44. new Slide(),
  45. ));
  46. $this->assertEquals(2, $object->count());
  47. $this->assertInternalType('array', $object->toArray());
  48. $this->assertCount(2, $object->toArray());
  49. }
  50. /**
  51. */
  52. public function testAdd()
  53. {
  54. $object = new HashTable();
  55. $oSlide = new Slide();
  56. // Add From Source : Null
  57. $this->assertNull($object->addFromSource());
  58. // Add From Source : Array
  59. $this->assertNull($object->addFromSource(array($oSlide)));
  60. $this->assertInternalType('array', $object->toArray());
  61. $this->assertCount(1, $object->toArray());
  62. // Clear
  63. $this->assertNull($object->clear());
  64. $this->assertEmpty($object->toArray());
  65. // Add Object
  66. $this->assertNull($object->add($oSlide));
  67. $this->assertCount(1, $object->toArray());
  68. $this->assertNull($object->clear());
  69. // Add Object w/Hash Index
  70. $oSlide->setHashIndex(rand(1, 100));
  71. $this->assertNull($object->add($oSlide));
  72. $this->assertCount(1, $object->toArray());
  73. // Add Object w/the same Hash Index
  74. $this->assertNull($object->add($oSlide));
  75. $this->assertCount(1, $object->toArray());
  76. }
  77. /**
  78. */
  79. public function testIndex()
  80. {
  81. $object = new HashTable();
  82. $oSlide1 = new Slide();
  83. $oSlide2 = new Slide();
  84. // Add Object
  85. $this->assertNull($object->add($oSlide1));
  86. $this->assertNull($object->add($oSlide2));
  87. // Index
  88. $this->assertEquals(0, $object->getIndexForHashCode($oSlide1->getHashCode()));
  89. $this->assertEquals(1, $object->getIndexForHashCode($oSlide2->getHashCode()));
  90. $this->assertEquals($oSlide1, $object->getByIndex(0));
  91. $this->assertEquals($oSlide2, $object->getByIndex(1));
  92. }
  93. /**
  94. */
  95. public function testRemove()
  96. {
  97. $object = new HashTable();
  98. $oSlide1 = new Slide();
  99. $oSlide2 = new Slide();
  100. $oSlide3 = new Slide();
  101. // Add Object
  102. $this->assertNull($object->add($oSlide1));
  103. $this->assertNull($object->add($oSlide2));
  104. $this->assertNull($object->add($oSlide3));
  105. // Remove
  106. $this->assertNull($object->remove($oSlide2));
  107. $this->assertCount(2, $object->toArray());
  108. $this->assertNull($object->remove($oSlide3));
  109. $this->assertCount(1, $object->toArray());
  110. }
  111. /**
  112. * @expectedException \Exception
  113. * @expectedExceptionMessage Invalid array parameter passed.
  114. */
  115. public function testAddException()
  116. {
  117. $object = new HashTable();
  118. $oSlide = new Slide();
  119. $object->addFromSource($oSlide);
  120. }
  121. }