public function testPrepare()
 {
     $expectedId = 'COL:EXAMPLEID';
     $expectedName = 'EXAMPLENAME';
     $subId = 'FRM:EXAMPLEID2';
     $subType = FormItem::class;
     $subItem = new FormItem();
     $subItem->setId($subId);
     $storage = $this->getMock(StorageInterface::class);
     $storage->expects($this->once())->method('get')->willReturn([CollectionItem::EXCHANGE_ID => $expectedId, CollectionItem::EXCHANGE_NAME => $expectedName, CollectionItemBuilderListener::ITEM_CONFIG_KEY => [[CollectionItemBuilderListener::SUB_ID => $subId, CollectionItemBuilderListener::SUB_TYPE => $subType]]]);
     $timetableManager = $this->getMock(TimetableManagerInterface::class);
     $timetableManager->method('getPointInTime')->willReturn(new \DateTime());
     $itemFactory = $this->getMock(ItemFactoryInterface::class);
     $itemFactory->expects($this->once())->method('getItem')->with($this->equalTo($subItem))->willReturn($subItem);
     $collectionItem = new CollectionItem();
     $collectionItem->setId($expectedId);
     $event = new ItemFactoryEvent();
     $event->setItem($collectionItem)->setName(ItemFactoryEvents::PREPARE_ITEM);
     $listener = new CollectionItemBuilderListener($storage, $timetableManager, $itemFactory);
     $listener->prepareItem($event);
     $this->assertEquals([$subItem], $collectionItem->getAll());
     $this->assertEquals($expectedId, $collectionItem->getId());
     $this->assertEquals($expectedName, $collectionItem->getName());
 }
Exemplo n.º 2
0
 public function testGettersAndSetters()
 {
     $collectionItem = new CollectionItem();
     $expectedName = 'rawr';
     $this->assertFalse($collectionItem->hasName());
     $this->assertInstanceOf(CollectionItem::class, $collectionItem->setName($expectedName));
     $this->assertTrue($collectionItem->hasName());
     $this->assertEquals($expectedName, $collectionItem->getName());
     $expectedItemA = $this->getMock(ItemInterface::class);
     $expectedItemA->method('getId')->willReturn('A');
     $expectedItemB = $this->getMock(ItemInterface::class);
     $expectedItemB->method('getId')->willReturn('B');
     $unexpectedItem = $this->getMock(ItemInterface::class);
     $unexpectedItem->method('getId')->willReturn('C');
     $this->assertInstanceOf(CollectionItem::class, $collectionItem->add($expectedItemA));
     $collectionItem->add($expectedItemB)->add($unexpectedItem);
     $this->assertTrue($collectionItem->has('A'));
     $this->assertTrue($collectionItem->has('B'));
     $this->assertTrue($collectionItem->has('C'));
     $this->assertInstanceOf(CollectionItem::class, $collectionItem->remove('C'));
     $this->assertFalse($collectionItem->has('C'));
     $this->assertEquals(2, $collectionItem->count());
     $count = 0;
     foreach ($collectionItem as $testItem) {
         $count++;
         $this->assertContains($testItem, [$expectedItemA, $expectedItemB]);
     }
     $this->assertEquals(2, $count, 'Foreach loop did not run expected number of times');
 }