xsolve-pl /
xsolve-associate
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Xsolve\AssociateTests\Functional\DoctrineOrm\Mock; |
||
| 4 | |||
| 5 | use Doctrine\ORM\EntityManagerInterface; |
||
| 6 | use Doctrine\ORM\Tools\SchemaTool; |
||
| 7 | use Doctrine\ORM\Tools\Setup; |
||
| 8 | use Doctrine\ORM\EntityManager; |
||
| 9 | use Xsolve\AssociateTests\Functional\DoctrineOrm\Mock\Entity\Category; |
||
| 10 | use Xsolve\AssociateTests\Functional\DoctrineOrm\Mock\Entity\Product; |
||
| 11 | use Xsolve\AssociateTests\Functional\DoctrineOrm\Mock\Entity\Store; |
||
| 12 | use Xsolve\AssociateTests\Functional\DoctrineOrm\Mock\Entity\User; |
||
| 13 | use Xsolve\AssociateTests\Functional\DoctrineOrm\Mock\Entity\Variant; |
||
| 14 | |||
| 15 | class DoctrineOrmHelper |
||
| 16 | { |
||
| 17 | /** @var EntityManagerInterface */ |
||
| 18 | protected $entityManager; |
||
| 19 | |||
| 20 | protected function setupEntityManager() |
||
| 21 | { |
||
| 22 | $entityPaths = [ |
||
| 23 | __DIR__ . '/Entity', |
||
| 24 | ]; |
||
| 25 | |||
| 26 | $isDevMode = false; |
||
| 27 | |||
| 28 | $databaseOptions = [ |
||
| 29 | 'driver' => 'pdo_sqlite', |
||
| 30 | 'dbname' => 'sifter_test', // TODO Move to some config file. |
||
| 31 | 'user' => 'root', // TODO Move to some config file. |
||
| 32 | 'password' => '', // TODO Move to some config file. |
||
| 33 | 'memory' => true, // TODO Move to some config file. |
||
| 34 | ]; |
||
| 35 | |||
| 36 | $metadataConfiguration = Setup::createAnnotationMetadataConfiguration($entityPaths, $isDevMode); |
||
| 37 | $this->entityManager = EntityManager::create($databaseOptions, $metadataConfiguration); |
||
| 38 | } |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @return EntityManagerInterface |
||
| 42 | */ |
||
| 43 | public function getEntityManager(): EntityManagerInterface |
||
| 44 | { |
||
| 45 | if (!$this->entityManager instanceof EntityManagerInterface) { |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 46 | $this->setupEntityManager(); |
||
| 47 | } |
||
| 48 | |||
| 49 | return $this->entityManager; |
||
| 50 | } |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @throws \Doctrine\ORM\Tools\ToolsException |
||
| 54 | */ |
||
| 55 | public function createSchema() |
||
| 56 | { |
||
| 57 | $this->getEntityManager(); |
||
| 58 | |||
| 59 | $entityClassMetadatas = []; |
||
| 60 | foreach ($this->getEntityClassNames() as $entityClassName) { |
||
| 61 | $entityClassMetadatas[] = $this->entityManager->getClassMetadata($entityClassName); |
||
| 62 | } |
||
| 63 | |||
| 64 | $tool = new SchemaTool($this->entityManager); |
||
| 65 | $tool->createSchema($entityClassMetadatas); |
||
| 66 | } |
||
| 67 | |||
| 68 | public function loadData() |
||
| 69 | { |
||
| 70 | $this->getEntityManager(); |
||
| 71 | |||
| 72 | $dataProvider = new DataProvider(); |
||
| 73 | foreach ($dataProvider->getAll() as $entity) { |
||
| 74 | $this->entityManager->persist($entity); |
||
| 75 | } |
||
| 76 | $this->entityManager->flush(); |
||
| 77 | } |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @return string[] |
||
| 81 | */ |
||
| 82 | protected function getEntityClassNames(): array |
||
| 83 | { |
||
| 84 | return [ |
||
| 85 | Category::class, |
||
| 86 | User::class, |
||
| 87 | Store::class, |
||
| 88 | Product::class, |
||
| 89 | Variant::class, |
||
| 90 | ]; |
||
| 91 | } |
||
| 92 | } |
||
| 93 |