diff --git a/tests/unit/Espo/ORM/CollectionTest.php b/tests/unit/Espo/ORM/CollectionTest.php new file mode 100644 index 0000000000..94576f637c --- /dev/null +++ b/tests/unit/Espo/ORM/CollectionTest.php @@ -0,0 +1,120 @@ +createMock(MetadataDataProvider::class); + + $metadataDataProvider + ->expects($this->any()) + ->method('get') + ->willReturn($ormMetadata); + + $this->metadata = new Metadata($metadataDataProvider); + $defsData = new DefsData($this->metadata); + $defs = new Defs($defsData); + + $this->entityManager = $this->createMock(EntityManager::class); + + $this->entityManager + ->expects($this->any()) + ->method('getDefs') + ->willReturn($defs); + } + + /** @noinspection PhpSameParameterValueInspection */ + private function createEntity(string $entityType): BaseEntity + { + $defs = $this->metadata->get($entityType); + + return new BaseEntity($entityType, $defs, $this->entityManager); + } + + public function testEntityCollectionAppend(): void + { + $entity1 = $this->createEntity('Account'); + $entity2 = $this->createEntity('Account'); + $entity3 = $this->createEntity('Account'); + + $collection = new EntityCollection([$entity1]); + + $collection[] = $entity2; + $collection->append($entity3); + + $this->assertEquals(3, $collection->count()); + } + + public function testEntityCollectionIteratorToArray(): void + { + $entity1 = $this->createEntity('Account'); + $entity2 = $this->createEntity('Account'); + $entity3 = $this->createEntity('Account'); + + $collection1 = new EntityCollection([$entity1]); + $collection2 = new EntityCollection([$entity2, $entity3]); + + $collection = new EntityCollection([ + ...iterator_to_array($collection1), + ...iterator_to_array($collection2), + ]); + + $this->assertEquals(3, $collection->count()); + } + + public function testEntityCollectionUnset(): void + { + $entity1 = $this->createEntity('Account'); + $entity2 = $this->createEntity('Account'); + + $collection = new EntityCollection([$entity1]); + + $collection[] = $entity2; + unset($collection[1]); + + $this->assertEquals(1, $collection->count()); + } +}