|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace Shopware\Core\System\Test; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Connection; |
|
6
|
|
|
use Shopware\Core\Framework\Context; |
|
7
|
|
|
use Shopware\Core\Framework\DataAbstractionLayer\DefinitionInstanceRegistry; |
|
8
|
|
|
use Shopware\Core\Framework\DataAbstractionLayer\Entity; |
|
9
|
|
|
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface; |
|
10
|
|
|
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria; |
|
11
|
|
|
use Shopware\Core\Framework\Test\TestCaseBase\KernelLifecycleManager; |
|
12
|
|
|
use Shopware\Core\Framework\Uuid\Uuid; |
|
13
|
|
|
|
|
14
|
|
|
trait EntityFixturesBase |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var Context |
|
18
|
|
|
*/ |
|
19
|
|
|
private $entityFixtureContext; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @before |
|
23
|
|
|
* Resets the context before each test |
|
24
|
|
|
*/ |
|
25
|
|
|
public function initializeFixtureContext(): void |
|
26
|
|
|
{ |
|
27
|
|
|
$this->entityFixtureContext = Context::createDefaultContext(); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public function setFixtureContext(Context $context): void |
|
31
|
|
|
{ |
|
32
|
|
|
$this->entityFixtureContext = $context; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public static function getFixtureRepository(string $fixtureName): EntityRepositoryInterface |
|
36
|
|
|
{ |
|
37
|
|
|
self::ensureATransactionIsActive(); |
|
38
|
|
|
|
|
39
|
|
|
$container = KernelLifecycleManager::getKernel()->getContainer(); |
|
40
|
|
|
|
|
41
|
|
|
if ($container->has('test.service_container')) { |
|
42
|
|
|
$container = $container->get('test.service_container'); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
return $container->get(DefinitionInstanceRegistry::class)->getRepository($fixtureName); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function createFixture(string $fixtureName, array $fixtureData, EntityRepositoryInterface $repository): Entity |
|
49
|
|
|
{ |
|
50
|
|
|
self::ensureATransactionIsActive(); |
|
51
|
|
|
|
|
52
|
|
|
$repository->create([$fixtureData[$fixtureName]], $this->entityFixtureContext); |
|
53
|
|
|
|
|
54
|
|
|
if (array_key_exists('mediaType', $fixtureData[$fixtureName])) { |
|
55
|
|
|
$connection = KernelLifecycleManager::getKernel() |
|
56
|
|
|
->getContainer() |
|
57
|
|
|
->get(Connection::class); |
|
58
|
|
|
$connection->update( |
|
59
|
|
|
'media', |
|
60
|
|
|
[ |
|
61
|
|
|
'media_type' => serialize($fixtureData[$fixtureName]['mediaType']), |
|
62
|
|
|
], |
|
63
|
|
|
['id' => Uuid::fromHexToBytes($fixtureData[$fixtureName]['id'])] |
|
64
|
|
|
); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
return $repository->search(new Criteria([$fixtureData[$fixtureName]['id']]), $this->entityFixtureContext) |
|
|
|
|
|
|
68
|
|
|
->get($fixtureData[$fixtureName]['id']); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
private static function ensureATransactionIsActive(): void |
|
72
|
|
|
{ |
|
73
|
|
|
/** @var Connection $connection */ |
|
74
|
|
|
$connection = KernelLifecycleManager::getKernel() |
|
75
|
|
|
->getContainer() |
|
76
|
|
|
->get(Connection::class); |
|
77
|
|
|
|
|
78
|
|
|
if (!$connection->isTransactionActive()) { |
|
79
|
|
|
throw new \BadMethodCallException('You should not start writing to the database outside of transactions'); |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|