ObjectStorageTest.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. namespace Test\PhpDevCommunity\PaperORM\Common;
  3. use PhpDevCommunity\PaperORM\Collection\ObjectStorage;
  4. use PhpDevCommunity\UniTester\TestCase;
  5. use Test\PhpDevCommunity\PaperORM\Entity\UserTest;
  6. class ObjectStorageTest extends TestCase
  7. {
  8. protected function setUp(): void
  9. {
  10. // TODO: Implement setUp() method.
  11. }
  12. protected function tearDown(): void
  13. {
  14. // TODO: Implement tearDown() method.
  15. }
  16. protected function execute(): void
  17. {
  18. $this->testFind();
  19. $this->testFindReturnsNullIfNotFound();
  20. $this->testFindBy();
  21. $this->testFindByReturnsEmptyArrayIfNotFound();
  22. $this->testFirst();
  23. $this->testFirstReturnsNullIfCollectionIsEmpty();
  24. $this->testToArray();
  25. $this->testToArrayReturnsEmptyArrayIfCollectionIsEmpty();
  26. $this->testIsEmptyReturnsTrueForEmptyObjectStorage();
  27. $this->testIsEmptyReturnsFalseForObjectStorageWithItems();
  28. $this->testFindPkWithNullPrimaryKey();
  29. $this->testFindPkWithNonExistentPrimaryKey();
  30. $this->testFindPkWithExistingPrimaryKey();
  31. $this->testFindOneBy();
  32. }
  33. public function testFind()
  34. {
  35. $collection = new ObjectStorage();
  36. $collection->attach(new \stdClass());
  37. $collection->attach(new \stdClass());
  38. $foundObject = $collection->find(function ($item) {
  39. return true;
  40. });
  41. $this->assertInstanceOf(\stdClass::class, $foundObject);
  42. }
  43. public function testFindReturnsNullIfNotFound()
  44. {
  45. $collection = new ObjectStorage();
  46. $foundObject = $collection->find(function ($item) {
  47. return true;
  48. });
  49. $this->assertNull($foundObject);
  50. }
  51. public function testFindBy()
  52. {
  53. $collection = new ObjectStorage();
  54. $object1 = new \stdClass();
  55. $object2 = new \stdClass();
  56. $collection->attach($object1);
  57. $collection->attach($object2);
  58. $foundObjects = $collection->filter(function ($item) use($object1) {
  59. return $item === $object1;
  60. });
  61. $this->assertStrictEquals(1, count($foundObjects));
  62. $this->assertTrue(in_array( $object1, $foundObjects));
  63. }
  64. public function testFindByReturnsEmptyArrayIfNotFound()
  65. {
  66. $collection = new ObjectStorage();
  67. $foundObjects = $collection->filter(function ($item) {
  68. return true;
  69. });
  70. $this->assertEmpty($foundObjects);
  71. }
  72. public function testFirst()
  73. {
  74. $collection = new ObjectStorage();
  75. $object = new \stdClass();
  76. $collection->attach($object);
  77. $firstObject = $collection->first();
  78. $this->assertStrictEquals($object, $firstObject);
  79. }
  80. public function testFirstReturnsNullIfCollectionIsEmpty()
  81. {
  82. $collection = new ObjectStorage();
  83. $firstObject = $collection->first();
  84. $this->assertNull($firstObject);
  85. }
  86. public function testToArray()
  87. {
  88. $collection = new ObjectStorage();
  89. $object1 = new \stdClass();
  90. $object2 = new \stdClass();
  91. $collection->attach($object1);
  92. $collection->attach($object2);
  93. $array = $collection->toArray();
  94. $this->assertStrictEquals(2, count($array));
  95. $this->assertTrue(in_array( $object1, $array));
  96. $this->assertTrue(in_array( $object2, $array));
  97. }
  98. public function testToArrayReturnsEmptyArrayIfCollectionIsEmpty()
  99. {
  100. $collection = new ObjectStorage();
  101. $array = $collection->toArray();
  102. $this->assertEmpty($array);
  103. }
  104. public function testIsEmptyReturnsTrueForEmptyObjectStorage()
  105. {
  106. $objectStorage = new ObjectStorage();
  107. $this->assertTrue($objectStorage->isEmpty());
  108. }
  109. public function testIsEmptyReturnsFalseForObjectStorageWithItems()
  110. {
  111. $object1 = new \stdClass();
  112. $object2 = new \stdClass();
  113. $objectStorage = new ObjectStorage();
  114. $objectStorage->attach($object1);
  115. $objectStorage->attach($object2);
  116. $this->assertFalse($objectStorage->isEmpty());
  117. }
  118. public function testFindPkWithNullPrimaryKey()
  119. {
  120. $collection = new ObjectStorage();
  121. $result = $collection->findPk(null);
  122. $this->assertNull($result);
  123. }
  124. public function testFindPkWithNonExistentPrimaryKey()
  125. {
  126. $collection = new ObjectStorage();
  127. $result = $collection->findPk(999);
  128. $this->assertNull($result);
  129. }
  130. public function testFindPkWithExistingPrimaryKey()
  131. {
  132. $collection = new ObjectStorage();
  133. $object = new UserTest();
  134. $object->setId(123);
  135. $collection->attach($object);
  136. $result = $collection->findPk(123);
  137. $this->assertStrictEquals($object, $result);
  138. }
  139. public function testFindOneBy()
  140. {
  141. $user = new UserTest();
  142. $user->setFirstname('John');
  143. $objectStorage = new ObjectStorage();
  144. $objectStorage->attach($user);
  145. $foundObject = $objectStorage->findOneBy('getFirstname', 'John');
  146. $this->assertStrictEquals($user, $foundObject);
  147. $foundObject = $objectStorage->findOneBy('getNonExistentMethod', 'John');
  148. $this->assertNull($foundObject);
  149. }
  150. }