Passed
Branch master (bbfb46)
by Jan
22:41 queued 14:44
created

EntityFixturesBase   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 26
dl 0
loc 66
rs 10
c 0
b 0
f 0
wmc 8

5 Methods

Rating   Name   Duplication   Size   Complexity  
A initializeFixtureContext() 0 3 1
A getFixtureRepository() 0 11 2
A ensureATransactionIsActive() 0 9 2
A createFixture() 0 21 2
A setFixtureContext() 0 3 1
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)
0 ignored issues
show
Bug Best Practice introduced by
The expression return $repository->sear...ta[$fixtureName]['id']) could return the type null which is incompatible with the type-hinted return Shopware\Core\Framework\...AbstractionLayer\Entity. Consider adding an additional type-check to rule them out.
Loading history...
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