Completed
Branch master (886cf7)
by Jan
07:19
created

KeywordSearchTermInterpreterTest::setupKeywords()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 57

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 57
rs 8.9381
c 0
b 0
f 0
ccs 15
cts 15
cp 1
cc 2
nc 2
nop 0
crap 2

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php declare(strict_types=1);
2
3
namespace Shopware\Storefront\Test;
4
5
use Doctrine\DBAL\Connection;
6
use Ramsey\Uuid\Uuid;
7
use Shopware\Core\Defaults;
8
use Shopware\Core\Framework\Context;
9
use Shopware\Core\Framework\ORM\Search\Term\SearchTerm;
10
use Shopware\Storefront\Page\Search\KeywordSearchTermInterpreter;
11
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
12
13
class KeywordSearchTermInterpreterTest extends KernelTestCase
14
{
15
    /**
16
     * @var \Doctrine\DBAL\Connection
17
     */
18
    private $connection;
19
20
    /**
21
     * @var KeywordSearchTermInterpreter
22
     */
23
    private $interpreter;
24
25 3
    public function setUp()
26
    {
27 3
        self::bootKernel();
28
29 3
        $this->connection = self::$container->get(Connection::class);
30 3
        $this->interpreter = self::$container->get(KeywordSearchTermInterpreter::class);
31 3
        $this->connection->beginTransaction();
32 3
        $this->connection->executeUpdate('DELETE FROM search_keyword');
33
34 3
        $this->setupKeywords();
35 3
    }
36
37 3
    public function tearDown()
38
    {
39 3
        $this->connection->rollBack();
40
41 3
        parent::tearDown();
42 3
    }
43
44
    /**
45
     * @dataProvider cases
46
     *
47
     * @param string $term
48
     * @param array  $expected
49
     */
50 3
    public function testMatching(string $term, array $expected)
51
    {
52 3
        $context = Context::createDefaultContext(Defaults::TENANT_ID);
53
54 3
        $matches = $this->interpreter->interpret($term, $context);
55
56
        $keywords = array_map(function (SearchTerm $term) {
57 3
            return $term->getTerm();
58 3
        }, $matches->getTerms());
59
60 3
        self::assertEquals($expected, $keywords);
61 3
    }
62
63
    public function cases()
64
    {
65
        return [
66
            [
67
                'zeichn',
68
                ['zeichnet', 'zeichen', 'zweichnet', 'gezeichnet', 'ausgezeichnet', 'verkehrzeichennetzwerk'],
69
            ],
70
            [
71
                'zeichent',
72
                ['zeichen', 'zeichnet', 'gezeichnet', 'ausgezeichnet', 'verkehrzeichennetzwerk'],
73
            ],
74
            [
75
                'Büronetz',
76
                ['büronetzwerk'],
77
            ],
78
        ];
79
    }
80
81 3
    private function setupKeywords()
82
    {
83
        $keywords = [
84 3
            'zeichnet',
85
            'zweichnet',
86
            'ausgezeichnet',
87
            'verkehrzeichennetzwerk',
88
            'gezeichnet',
89
            'zeichen',
90
            'zweideutige',
91
            'zweier',
92
            'zweite',
93
            'zweiteilig',
94
            'zweiten',
95
            'zweites',
96
            'zweiweg',
97
            'zweifellos',
98
            'büronetzwerk',
99
            'heimnetzwerk',
100
            'netzwerk',
101
            'netzwerkadapter',
102
            'netzwerkbuchse',
103
            'netzwerkcontroller',
104
            'netzwerkdrucker',
105
            'netzwerke',
106
            'netzwerken',
107
            'netzwerkinfrastruktur',
108
            'netzwerkkabel',
109
            'netzwerkkabels',
110
            'netzwerkkarte',
111
            'netzwerklösung',
112
            'netzwerkschnittstelle',
113
            'netzwerkschnittstellen',
114
            'netzwerkspeicher',
115
            'netzwerkspeicherlösung',
116
            'netzwerkspieler',
117
            'schwarzweiß',
118
            'netzwerkprotokolle',
119
        ];
120
121 3
        $languageId = Uuid::fromString(Defaults::LANGUAGE)->getBytes();
122 3
        $versionId = Uuid::fromString(Defaults::LIVE_VERSION)->getBytes();
123 3
        $tenantId = Uuid::fromString(Defaults::TENANT_ID)->getBytes();
124
125 3
        foreach ($keywords as $keyword) {
126 3
            preg_match_all('/./us', $keyword, $ar);
127
128 3
            $this->connection->insert('search_keyword', [
129 3
                'tenant_id' => $tenantId,
130 3
                'keyword' => $keyword,
131 3
                'reversed' => implode('', array_reverse($ar[0])),
132 3
                'version_id' => $versionId,
133 3
                'language_id' => $languageId,
134 3
                'language_tenant_id' => $tenantId,
135
            ]);
136
        }
137 3
    }
138
}
139