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

SeoUrlExtensionTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 128
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 71
dl 0
loc 128
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace Shopware\Storefront\Test\Framework\Seo\SeoUrl;
4
5
use PHPUnit\Framework\TestCase;
6
use Shopware\Core\Content\Product\ProductEntity;
7
use Shopware\Core\Defaults;
8
use Shopware\Core\Framework\Context;
9
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
10
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
11
use Shopware\Core\Framework\Test\TestCaseBase\IntegrationTestBehaviour;
12
use Shopware\Core\Framework\Uuid\Uuid;
13
use Shopware\Storefront\Framework\Seo\SeoUrl\SeoUrlCollection;
14
use Shopware\Storefront\Framework\Seo\SeoUrl\SeoUrlEntity;
15
use Shopware\Storefront\Framework\Seo\SeoUrlGenerator\ProductDetailPageSeoUrlGenerator;
16
17
class SeoUrlExtensionTest extends TestCase
18
{
19
    use IntegrationTestBehaviour;
20
21
    /**
22
     * @var EntityRepositoryInterface
23
     */
24
    private $productRepository;
25
26
    public function setUp(): void
27
    {
28
        parent::setUp();
29
        $this->productRepository = $this->getContainer()->get('product.repository');
30
    }
31
32
    public function testInsert(): void
33
    {
34
        $seoUrlId1 = Uuid::randomHex();
35
        $seoUrlId2 = Uuid::randomHex();
36
37
        $id = Uuid::randomHex();
38
        $this->upsertProduct([
39
            'id' => $id,
40
            'name' => 'awesome product',
41
            'extensions' => [
42
                'seoUrls' => [
43
                    [
44
                        'id' => $seoUrlId1,
45
                        'salesChannelId' => Defaults::SALES_CHANNEL,
46
                        'pathInfo' => '/detail/' . $id,
47
                        'seoPathInfo' => 'awesome v2',
48
                        'isCanonical' => true,
49
                    ],
50
                    [
51
                        'id' => $seoUrlId2,
52
                        'salesChannelId' => Defaults::SALES_CHANNEL,
53
                        'pathInfo' => '/detail/' . $id,
54
                        'seoPathInfo' => 'awesome',
55
                        'isCanonical' => true,
56
                    ],
57
                ],
58
            ],
59
        ]);
60
61
        $criteria = new Criteria([$id]);
62
        $criteria->addAssociation('seoUrls');
63
64
        /** @var ProductEntity $first */
65
        $first = $this->productRepository->search($criteria, Context::createDefaultContext())->first();
66
67
        static::assertNotNull($first);
68
69
        /** @var SeoUrlCollection $seoUrls */
70
        $seoUrls = $first->getExtensionOfType('seoUrls', SeoUrlCollection::class);
71
        static::assertNotNull($seoUrls);
72
73
        /** @var SeoUrlEntity|null $seoUrl */
74
        $seoUrl = $seoUrls->filterByProperty('id', $seoUrlId1)->first();
75
        static::assertNotNull($seoUrl);
76
77
        static::assertTrue($seoUrl->getIsModified());
78
        static::assertTrue($seoUrl->getIsCanonical());
79
        static::assertFalse($seoUrl->getIsDeleted());
80
81
        static::assertEquals('awesome v2', $seoUrl->getSeoPathInfo());
82
    }
83
84
    public function testUpdate(): void
85
    {
86
        $seoUrlId = Uuid::randomHex();
87
        $id = Uuid::randomHex();
88
        $this->upsertProduct(['id' => $id, 'name' => 'awesome product']);
89
90
        $router = $this->getContainer()->get('router');
91
        $pathInfo = $router->generate(ProductDetailPageSeoUrlGenerator::ROUTE_NAME, ['productId' => $id]);
92
93
        $this->upsertProduct([
94
            'id' => $id,
95
            'extensions' => [
96
                'seoUrls' => [
97
                    [
98
                        'id' => $seoUrlId,
99
                        'salesChannelId' => Defaults::SALES_CHANNEL,
100
                        'pathInfo' => $pathInfo,
101
                        'seoPathInfo' => 'awesome',
102
                        'isCanonical' => true,
103
                    ],
104
                ],
105
            ],
106
        ]);
107
108
        $criteria = new Criteria([$id]);
109
        $criteria->addAssociation('seoUrls');
110
111
        /** @var ProductEntity $first */
112
        $first = $this->productRepository->search($criteria, Context::createDefaultContext())->first();
113
114
        static::assertNotNull($first);
115
116
        /** @var SeoUrlCollection $seoUrls */
117
        $seoUrls = $first->getExtensionOfType('seoUrls', SeoUrlCollection::class);
118
119
        /** @var SeoUrlEntity|null $seoUrl */
120
        $seoUrl = $seoUrls->filterByProperty('id', $seoUrlId)->first();
121
        static::assertNotNull($seoUrl);
122
123
        static::assertTrue($seoUrl->getIsModified());
124
        static::assertTrue($seoUrl->getIsCanonical());
125
        static::assertFalse($seoUrl->getIsDeleted());
126
127
        static::assertEquals('/detail/' . $id, $seoUrl->getPathInfo());
128
        static::assertEquals($id, $seoUrl->getForeignKey());
129
    }
130
131
    private function upsertProduct($data): void
132
    {
133
        $defaults = [
134
            'productNumber' => Uuid::randomHex(),
135
            'manufacturer' => [
136
                'id' => Uuid::randomHex(),
137
                'name' => 'amazing brand',
138
            ],
139
            'tax' => ['id' => Uuid::randomHex(), 'taxRate' => 19, 'name' => 'tax'],
140
            'price' => ['gross' => 10, 'net' => 12, 'linked' => false],
141
            'stock' => 0,
142
        ];
143
        $data = array_merge($defaults, $data);
144
        $this->productRepository->upsert([$data], Context::createDefaultContext());
145
    }
146
}
147