/**
  * @param ClassMetadataInfo $classMetadataInfo
  * @param string            $dumpDirectory
  *
  * return void
  */
 private function dumpFile(ClassMetadataInfo $classMetadataInfo, $dumpDirectory)
 {
     $this->output->writeln("[" . date('c') . "] dumping {$classMetadataInfo->getName()} ...");
     $this->dumper->setClassMetadataInfo($classMetadataInfo);
     $file = $this->dumper->dumpToYaml($dumpDirectory);
     $this->output->writeln("[" . date('c') . "] successfully dumped in file  {$file}");
     $this->output->writeln(PHP_EOL);
 }
Example #2
0
    /**
     * @see Cosma\Bundle\TestingBundle\Fixture\Dumper::dumpToYaml
     */
    public function testDumpToYaml()
    {
        $directoryPath = sys_get_temp_dir();
        $firstDummyEntity = new DummyEntity();
        $firstDummyEntity->setId(1);
        $firstDummyEntity->setName('first dummy entity');
        $secondDummyEntity = new DummyEntity();
        $secondDummyEntity->setId(2);
        $secondDummyEntity->setName('second dummy entity');
        $firstAnotherDummyEntity = new AnotherDummyEntity();
        $firstAnotherDummyEntity->setId(1);
        $firstAnotherDummyEntity->setName('first another dummy entity');
        $secondAnotherDummyEntity = new AnotherDummyEntity();
        $secondAnotherDummyEntity->setId(2);
        $secondAnotherDummyEntity->setName('second another dummy entity');
        $thirdAnotherDummyEntity = new AnotherDummyEntity();
        $thirdAnotherDummyEntity->setId(3);
        $thirdAnotherDummyEntity->setName('third another dummy entity');
        $firstDummyEntity->setAnotherEntity($firstAnotherDummyEntity);
        $firstDummyEntity->setAnotherEntity($thirdAnotherDummyEntity);
        $secondDummyEntity->setAnotherEntity($secondAnotherDummyEntity);
        $entityRepository = $this->getMockBuilder('\\Doctrine\\Common\\Persistence\\ObjectRepository')->disableOriginalConstructor()->setMethods(['findAll'])->getMockForAbstractClass();
        $entityRepository->expects($this->once())->method('findAll')->will($this->returnValue([$firstDummyEntity, $secondDummyEntity]));
        $entityManager = $this->getMockBuilder('Doctrine\\ORM\\EntityManager')->disableOriginalConstructor()->setMethods(['getRepository'])->getMock();
        $entityManager->expects($this->once())->method('getRepository')->with('Cosma\\Bundle\\TestingBundle\\Tests\\Fixture\\DummyEntity')->will($this->returnValue($entityRepository));
        $classMetaDataInfo = $this->getMockBuilder('\\Doctrine\\ORM\\Mapping\\ClassMetadataInfo')->disableOriginalConstructor()->setMethods(['getName', 'getIdentifier', 'getFieldNames', 'getFieldValue'])->getMock();
        $classMetaDataInfo->expects($this->exactly(4))->method('getName')->will($this->returnValue('Cosma\\Bundle\\TestingBundle\\Tests\\Fixture\\DummyEntity'));
        $classMetaDataInfo->expects($this->exactly(2))->method('getIdentifier')->will($this->returnValue(['id']));
        $classMetaDataInfo->expects($this->never())->method('getAssociationMappings')->will($this->returnValue(['anotherEntities']));
        $classMetaDataInfo->expects($this->exactly(2))->method('getFieldNames')->will($this->returnValue(['id', 'name']));
        $classMetaDataInfo->expects($this->any())->method('getFieldValue')->will($this->returnValue(456));
        $dumper = new Dumper($entityManager);
        $dumper->setAssociation(false);
        $dumper->setClassMetadataInfo($classMetaDataInfo);
        $file = $dumper->dumpToYaml($directoryPath);
        $this->assertEquals("{$directoryPath}/.yml", $file, 'Dump process is not correct');
        $expected = <<<EOT
Cosma\\Bundle\\TestingBundle\\Tests\\Fixture\\DummyEntity:
    cosma_bundle_testingbundle_tests_fixture_dummyentity_456:
        id: 456
        name: 456

EOT;
        $this->assertEquals($expected, file_get_contents($file), 'Dump process is not correct');
    }