|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace EdmondsCommerce\DoctrineStaticMeta\Tests\Large\Entity\Savers; |
|
4
|
|
|
|
|
5
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Interfaces\EntityInterface; |
|
6
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Savers\BulkEntitySaver; |
|
7
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Tests\Assets\AbstractLargeTest; |
|
8
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Tests\Assets\AbstractTest; |
|
9
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Tests\Assets\TestCodeGenerator; |
|
10
|
|
|
|
|
11
|
|
|
class BulkEntitySaverTest extends AbstractLargeTest |
|
12
|
|
|
{ |
|
13
|
|
|
public const WORK_DIR = AbstractTest::VAR_PATH . '/' . self::TEST_TYPE_LARGE . '/BulkEntitySaverTest'; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @var BulkEntitySaver |
|
17
|
|
|
*/ |
|
18
|
|
|
private $saver; |
|
19
|
|
|
|
|
20
|
|
|
public function setup(): void |
|
21
|
|
|
{ |
|
22
|
|
|
parent::setUp(); |
|
23
|
|
|
if (false === self::$built) { |
|
24
|
|
|
$this->getTestCodeGenerator() |
|
25
|
|
|
->copyTo(self::WORK_DIR, self::TEST_PROJECT_ROOT_NAMESPACE); |
|
26
|
|
|
} |
|
27
|
|
|
$this->setupCopiedWorkDirAndCreateDatabase(); |
|
28
|
|
|
$this->saver = new BulkEntitySaver($this->getEntityManager()); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @test |
|
33
|
|
|
* @large |
|
34
|
|
|
*/ |
|
35
|
|
|
public function itCanBulkSaveLargeDataEntities() |
|
36
|
|
|
{ |
|
37
|
|
|
$this->saver->setChunkSize(100); |
|
38
|
|
|
$entityFqn = $this->getCopiedFqn(TestCodeGenerator::TEST_ENTITY_LARGE_DATA); |
|
39
|
|
|
$generator = $this->getTestEntityGeneratorFactory() |
|
40
|
|
|
->createForEntityFqn($entityFqn) |
|
41
|
|
|
->getGenerator($this->getEntityManager(), $entityFqn); |
|
42
|
|
|
for ($i = 0, $iMax = $this->getDataSize(); $i < $iMax; $i++) { |
|
43
|
|
|
$this->saver->addEntityToSave($this->getNextEntity($generator)); |
|
44
|
|
|
} |
|
45
|
|
|
$this->saver->endBulkProcess(); |
|
46
|
|
|
$numEntities = $this->getRepositoryFactory()->getRepository($entityFqn)->count(); |
|
47
|
|
|
self::assertSame($this->getDataSize(), $numEntities); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @return int |
|
52
|
|
|
* @SuppressWarnings(PHPMD.Superglobals) |
|
53
|
|
|
*/ |
|
54
|
|
|
private function getDataSize():int |
|
55
|
|
|
{ |
|
56
|
|
|
if ($this->isQuickTests()) { |
|
57
|
|
|
return 200; |
|
58
|
|
|
} |
|
59
|
|
|
if (isset($_SERVER['BulkEntitySaverTest_DataSize'])) { |
|
60
|
|
|
return (int)$_SERVER['BulkEntitySaverTest_DataSize']; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
return 1000; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
private function getNextEntity(\Generator $generator): EntityInterface |
|
67
|
|
|
{ |
|
68
|
|
|
$generator->next(); |
|
69
|
|
|
|
|
70
|
|
|
return $generator->current(); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|