Example #1
0
 public function testGetIds()
 {
     $objects = [];
     $object = new ExtendedDataObject();
     $objects[] = $object->setId(4);
     $object = new ExtendedDataObject();
     $objects[] = $object->setId(7);
     $this->assertEquals([4, 7], ExtendedDataObject::getIds($objects));
 }
Example #2
0
 public function testFlush()
 {
     $unitOfWork = new UnitOfWork($this->objectMapper);
     $toSave = new ExtendedDataObject();
     $toSave->setId(5);
     $toSaveAll = [new OtherDataObject(), new OtherDataObject()];
     $unitOfWork->save($toSave)->saveAll($toSaveAll);
     $toDelete = new ExtendedDataObject();
     $toDelete->setId(7);
     $toDeleteAll = [new OtherDataObject(), new OtherDataObject()];
     $unitOfWork->delete($toDelete)->deleteAll($toDeleteAll);
     $this->connection->expects($this->once())->method('beginTransaction');
     $this->connection->expects($this->once())->method('commit');
     $this->objectMapper->expects($this->once())->method('save')->with($toSave);
     $this->objectMapper->expects($this->once())->method('saveAll')->with($toSaveAll);
     $this->objectMapper->expects($this->once())->method('delete')->with($toDelete);
     $this->objectMapper->expects($this->once())->method('deleteAll')->with($toDeleteAll);
     $unitOfWork->flush();
 }
 public function testFindAll()
 {
     $this->cache->expects($this->exactly(2))->method('contains')->will($this->onConsecutiveCalls(false, true));
     $this->cache->expects($this->once())->method('save');
     $this->cache->expects($this->once())->method('fetch')->willReturn([['id' => 1, 'myColumn' => 'My Value'], ['id' => 2, 'myColumn' => 'My Value 2']]);
     $repository = $this->getRepository();
     $objects = [];
     $object = new ExtendedDataObject();
     $objects[] = $object->setId(1)->setMyColumn('My Value');
     $object = new ExtendedDataObject();
     $objects[] = $object->setId(2)->setMyColumn('My Value 2');
     $repository->expects($this->once())->method('fetchAll')->willReturn($objects);
     $objects = $repository->findAll();
     $this->assertCount(2, $objects);
     $this->assertInstanceOf(ExtendedDataObject::class, $objects[0]);
     /** @var ExtendedDataObject[] $objects */
     $objects = $repository->findAll();
     $this->assertCount(2, $objects);
     $this->assertInstanceOf(ExtendedDataObject::class, $objects[0]);
     $this->assertEquals('My Value', $objects[0]->getMyColumn());
 }
Example #4
0
 public function testDeleteAllEvents()
 {
     $firedEvents = ['DataObject.beforeDelete' => 0, 'DataObject.ExtendedDataObject.beforeDelete' => 0, 'DataObject.afterDelete' => 0, 'DataObject.ExtendedDataObject.afterDelete' => 0];
     $this->dispatcher->addListener('DataObject.beforeDelete', function (DataObjectEventInterface $event) use(&$firedEvents) {
         $firedEvents['DataObject.beforeDelete']++;
     });
     $this->dispatcher->addListener('DataObject.ExtendedDataObject.beforeDelete', function (DataObjectEventInterface $event) use(&$firedEvents) {
         $firedEvents['DataObject.ExtendedDataObject.beforeDelete']++;
     });
     $this->dispatcher->addListener('DataObject.afterDelete', function (DataObjectEventInterface $event) use(&$firedEvents) {
         $firedEvents['DataObject.afterDelete']++;
     });
     $this->dispatcher->addListener('DataObject.ExtendedDataObject.afterDelete', function (DataObjectEventInterface $event) use(&$firedEvents) {
         $firedEvents['DataObject.ExtendedDataObject.afterDelete']++;
     });
     $repo = $this->getRepository();
     $dataObjects = [];
     $dataObject = new ExtendedDataObject();
     $dataObjects[] = $dataObject->setId('12345');
     $dataObject = new ExtendedDataObject();
     $dataObjects[] = $dataObject->setId('6789');
     $repo->deleteAll($dataObjects);
     $this->assertEquals(2, $firedEvents['DataObject.beforeDelete']);
     $this->assertEquals(2, $firedEvents['DataObject.ExtendedDataObject.beforeDelete']);
     $this->assertEquals(2, $firedEvents['DataObject.afterDelete']);
     $this->assertEquals(2, $firedEvents['DataObject.ExtendedDataObject.afterDelete']);
 }
Example #5
0
 public function testLoadManyToMany()
 {
     $objects = [];
     $object = new ExtendedDataObject();
     $objects[] = $object->setId(123);
     $object = new ExtendedDataObject();
     $objects[] = $object->setId(456);
     $mockRepo = $this->getMockBuilder(ExtendedDataObjectRepository::class)->disableOriginalConstructor()->getMock();
     $return = ['789' => new OtherDataObject()];
     $mockRepo->expects($this->once())->method('loadManyToMany')->with($objects, OtherDataObject::class, 'link_table')->willReturn($return);
     $loaded = $this->getCorma($mockRepo)->loadManyToMany($objects, OtherDataObject::class, 'link_table');
     $this->assertEquals($return, $loaded);
 }
Example #6
0
 public function testSaveManyToMany()
 {
     $otherObjects = [];
     $otherObject = new OtherDataObject();
     $otherObject->setName('Other object many-to-many 1-1');
     $otherObject2 = new OtherDataObject();
     $otherObject2->setName('Other object many-to-many 1-2');
     $otherObject3 = new OtherDataObject();
     $otherObject3->setName('Other object many-to-many 2-1');
     $otherObject4 = new OtherDataObject();
     $otherObject4->setName('Other object many-to-many 2-2');
     $otherObjectToDelete = new OtherDataObject();
     $otherObjects[] = $otherObjectToDelete->setName('Other object many-to-many 1-3');
     $otherObjectToDelete2 = new OtherDataObject();
     $otherObjects[] = $otherObjectToDelete2->setName('Other object many-to-many 2-3');
     $objects = [];
     $object = new ExtendedDataObject();
     $object2 = new ExtendedDataObject();
     $objects[] = $object->setMyColumn('Save many-to-many 1')->setOtherDataObjects([$otherObject, $otherObject2]);
     $objects[] = $object2->setMyColumn('Save many-to-many 2')->setOtherDataObjects([$otherObject3, $otherObject4]);
     $this->repository->saveAll($objects);
     $this->objectMapper->saveAll($otherObjects);
     $queryHelper = $this->objectMapper->getQueryHelper();
     $queryHelper->massInsert('extended_other_rel', [['extendedDataObjectId' => $object->getId(), 'otherDataObjectId' => $otherObjectToDelete->getId()], ['extendedDataObjectId' => $object2->getId(), 'otherDataObjectId' => $otherObjectToDelete2->getId()]]);
     $relationshipSaver = $this->objectMapper->getRelationshipSaver();
     $relationshipSaver->saveManyToMany($objects, OtherDataObject::class, 'extended_other_rel');
     $this->assertGreaterThan(0, $otherObject->getId());
     $this->assertGreaterThan(0, $otherObject2->getId());
     $this->assertGreaterThan(0, $otherObject3->getId());
     $this->assertGreaterThan(0, $otherObject4->getId());
     $objectLinks = $queryHelper->buildSelectQuery('extended_other_rel', self::$connection->quoteIdentifier('otherDataObjectId'), ['extendedDataObjectId' => $object->getId()])->execute()->fetchAll(\PDO::FETCH_COLUMN);
     $this->assertCount(2, $objectLinks);
     $this->assertEquals($otherObject->getId(), $objectLinks[0]);
     $this->assertEquals($otherObject2->getId(), $objectLinks[1]);
     $object2Links = $queryHelper->buildSelectQuery('extended_other_rel', self::$connection->quoteIdentifier('otherDataObjectId'), ['extendedDataObjectId' => $object2->getId()])->execute()->fetchAll(\PDO::FETCH_COLUMN);
     $this->assertCount(2, $object2Links);
     $this->assertEquals($otherObject3->getId(), $object2Links[0]);
     $this->assertEquals($otherObject4->getId(), $object2Links[1]);
 }