Completed
Pull Request — master (#941)
by
unknown
02:20
created

AbstractElasticsearchTestCase::getDataArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the ONGR package.
5
 *
6
 * (c) NFQ Technologies UAB <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace ONGR\ElasticsearchBundle\Test;
13
14
use Elasticsearch\Common\Exceptions\BadRequest400Exception;
15
use ONGR\ElasticsearchBundle\Service\IndexService;
16
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
17
use Symfony\Component\DependencyInjection\ContainerInterface;
18
19
/**
20
 * Base test which creates unique connection to test with.
21
 */
22
abstract class AbstractElasticsearchTestCase extends WebTestCase
23
{
24
    protected static $cachedContainer;
25
26
    /**
27
     * @var IndexService[]
28
     */
29
    private $indexes = [];
30
31
    //You may use setUp() for your personal needs.
32
    protected function setUp()
33
    {
34
    }
35
36
    /**
37
     * Can be overwritten in child class to populate elasticsearch index with the data.
38
     *
39
     * Example:
40
     *      "/This/Should/Be/Index/Document/Namespace" =>
41
     *      [
42
     *          '_doc' => [
43
     *              [
44
     *                  '_id' => 1,
45
     *                  'title' => 'foo',
46
     *              ],
47
     *              [
48
     *                  '_id' => 2,
49
     *                  'title' => 'bar',
50
     *              ]
51
     *          ]
52
     *      ]
53
     *
54
     * @return array
55
     */
56
    protected function getDataArray(): array
57
    {
58
        return [];
59
    }
60
61
    protected function getDataDocuments(): array
62
    {
63
        return [];
64
    }
65
66
    private function populateDataArray(IndexService $indexService, array $documents = [])
67
    {
68
        foreach ($documents as $document) {
69
            $indexService->bulk('index', $document);
70
        }
71
        $indexService->commit();
72
    }
73
74
    private function populateDocument(IndexService $indexService, array $documents = [])
75
    {
76
        foreach ($documents as $document) {
77
            $indexService->persist($document);
78
        }
79
        $indexService->commit();
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85
    protected function tearDown(): void
86
    {
87
        parent::tearDown();
88
89
        foreach ($this->indexes as $name => $index) {
90
            try {
91
                $index->dropIndex();
92
            } catch (\Exception $e) {
93
                // Do nothing.
94
            }
95
        }
96
    }
97
98
    protected function getContainer($reinitialize = false, $kernelOptions = []): ContainerInterface
0 ignored issues
show
Unused Code introduced by
The parameter $kernelOptions is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
99
    {
100
        if (!self::$cachedContainer && !$reinitialize) {
101
//            static::bootKernel($kernelOptions);
102
103
            self::$cachedContainer = static::createClient(['environment' => 'test'])->getContainer();
104
        }
105
106
        return self::$cachedContainer;
107
    }
108
109
    protected function getIndex($namespace, $createIndex = true): IndexService
110
    {
111
        try {
112
            if (!array_key_exists($namespace, $this->indexes)) {
113
                $this->indexes[$namespace] = $this->getContainer()->get($namespace);
114
            }
115
116
            if (!$this->indexes[$namespace]->indexExists() && $createIndex) {
117
                $this->indexes[$namespace]->dropAndCreateIndex();
118
119
                // Populates elasticsearch index with the data
120
                $data = $this->getDataArray();
121
                if (!empty($data[$namespace])) {
122
                    $this->populateDataArray($this->indexes[$namespace], $data[$namespace]);
0 ignored issues
show
Documentation introduced by
$this->indexes[$namespace] is of type object|null, but the function expects a object<ONGR\Elasticsearc...e\Service\IndexService>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
123
                }
124
125
                // Populates elasticsearch index with the data
126
                $data = $this->getDataDocuments();
127
                if (!empty($data[$namespace])) {
128
                    $this->populateDocument($this->indexes[$namespace], $data[$namespace]);
0 ignored issues
show
Documentation introduced by
$this->indexes[$namespace] is of type object|null, but the function expects a object<ONGR\Elasticsearc...e\Service\IndexService>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
129
                }
130
131
                $this->indexes[$namespace]->refresh();
132
            }
133
134
            return $this->indexes[$namespace];
135
        } catch (\Exception $e) {
136
            throw new \LogicException($e->getMessage());
137
        }
138
    }
139
}
140