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

SeoControllerTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 61
dl 0
loc 90
rs 10
c 0
b 0
f 0
wmc 4
1
<?php declare(strict_types=1);
2
3
namespace Shopware\Storefront\Test\Framework\Seo\Controller;
4
5
use PHPUnit\Framework\TestCase;
6
use Shopware\Core\Content\Product\ProductDefinition;
7
use Shopware\Core\Defaults;
8
use Shopware\Core\Framework\Test\TestCaseBase\AdminFunctionalTestBehaviour;
9
use Shopware\Core\Framework\Uuid\Uuid;
10
use Shopware\Storefront\Framework\Seo\SeoUrlGenerator\ProductDetailPageSeoUrlGenerator;
11
use Shopware\Storefront\Framework\Seo\SeoUrlTemplate\SeoUrlTemplateEntity;
12
13
class SeoControllerTest extends TestCase
14
{
15
    use AdminFunctionalTestBehaviour;
16
17
    public function testValidateEmpty(): void
18
    {
19
        $this->getClient()->request('POST', '/api/v1/_action/seo-url-template/validate');
20
        $response = $this->getClient()->getResponse();
21
        $result = json_decode($response->getContent(), true);
22
23
        static::assertNotEmpty($result['errors']);
24
        static::assertEquals(400, $response->getStatusCode());
25
    }
26
27
    public function testValidateInvalid(): void
28
    {
29
        $template = new SeoUrlTemplateEntity();
30
        $template->setRouteName('frontend.detail.page');
31
        $template->setTemplate('{{ product.name }');
32
        $template->setEntityName($this->getContainer()->get(ProductDefinition::class)->getEntityName());
33
        $template->setSalesChannelId(Defaults::SALES_CHANNEL);
34
35
        $this->getClient()->request('POST', '/api/v1/_action/seo-url-template/validate', $template->jsonSerialize());
36
        $response = $this->getClient()->getResponse();
37
        $result = json_decode($response->getContent(), true);
38
39
        static::assertNotEmpty($result['errors'] ?? []);
40
        static::assertEquals(400, $response->getStatusCode());
41
    }
42
43
    public function testValidateValid(): void
44
    {
45
        $template = new SeoUrlTemplateEntity();
46
        $template->setRouteName('frontend.detail.page');
47
        $template->setTemplate('{{ product.name }}');
48
        $template->setEntityName($this->getContainer()->get(ProductDefinition::class)->getEntityName());
49
        $template->setSalesChannelId(Defaults::SALES_CHANNEL);
50
51
        $this->getClient()->request('POST', '/api/v1/_action/seo-url-template/validate', $template->jsonSerialize());
52
        $response = $this->getClient()->getResponse();
53
        $result = json_decode($response->getContent(), true);
54
55
        static::assertArrayNotHasKey('errors', $result);
56
        static::assertEquals(200, $response->getStatusCode());
57
    }
58
59
    public function testGetSeoContext(): void
60
    {
61
        $product = [
62
            'id' => Uuid::randomHex(),
63
            'productNumber' => Uuid::randomHex(),
64
            'name' => 'test',
65
            'price' => [
66
                'gross' => 10,
67
                'net' => 20,
68
                'linked' => false,
69
            ],
70
            'manufacturer' => [
71
                'id' => Uuid::randomHex(),
72
                'name' => 'test',
73
            ],
74
            'tax' => ['name' => 'test', 'taxRate' => 15],
75
            'stock' => 0,
76
        ];
77
        $this->getClient()->request('POST', '/api/v1/product', $product);
78
79
        $data = [
80
            'routeName' => ProductDetailPageSeoUrlGenerator::ROUTE_NAME,
81
            'entityName' => $this->getContainer()->get(ProductDefinition::class)->getEntityName(),
82
        ];
83
        $this->getClient()->request('POST', '/api/v1/_action/seo-url-template/context', $data);
84
85
        $response = $this->getClient()->getResponse();
86
        static::assertEquals(200, $response->getStatusCode());
87
88
        $data = json_decode($response->getContent(), true);
89
        $expectedKeys = [
90
            'product',
91
            'id',
92
            'productId',
93
            'shortId',
94
            'productName',
95
            'manufacturerId',
96
            'manufacturerName',
97
            'manufacturerNumber',
98
        ];
99
        $actualKeys = array_keys($data);
100
        sort($expectedKeys);
101
        sort($actualKeys);
102
        static::assertEquals($expectedKeys, $actualKeys);
103
    }
104
}
105