Completed
Pull Request — master (#1893)
by
unknown
03:25
created

DateProcessorTest   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 8
dl 0
loc 89
c 0
b 0
f 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A testDate() 0 24 1
A testDateWithNonDefaultOptions() 0 19 1
A testDateField() 0 32 1
1
<?php
2
3
namespace Elastica\Test\Processor;
4
5
use Elastica\Bulk;
6
use Elastica\Document;
7
use Elastica\Processor\DateProcessor;
8
use Elastica\Test\BasePipeline as BasePipelineTest;
9
10
/**
11
 * @internal
12
 */
13
class DateProcessorTest extends BasePipelineTest
14
{
15
    /**
16
     * @group unit
17
     */
18
    public function testDate(): void
19
    {
20
        $processor = new DateProcessor('initial_date', ['dd/MM/yyyy hh:mm:ss']);
21
22
        $expected = [
23
            'date' => [
24
                'field' => 'initial_date',
25
                'formats' => ['dd/MM/yyyy hh:mm:ss'],
26
            ],
27
        ];
28
29
        $this->assertEquals($expected, $processor->toArray());
30
31
        $processor = new DateProcessor('initial_date', ['dd/MM/yyyy hh:mm:ss', 'ISO8601', 'UNIX', 'UNIX_MS']);
32
33
        $expected = [
34
            'date' => [
35
                'field' => 'initial_date',
36
                'formats' => ['dd/MM/yyyy hh:mm:ss', 'ISO8601', 'UNIX', 'UNIX_MS'],
37
            ],
38
        ];
39
40
        $this->assertEquals($expected, $processor->toArray());
41
    }
42
43
    /**
44
     * @group unit
45
     */
46
    public function testDateWithNonDefaultOptions(): void
47
    {
48
        $processor = new DateProcessor('initial_date', ['dd/MM/yyyy hh:mm:ss', 'ISO8601', 'UNIX', 'UNIX_MS']);
49
        $processor->setTargetField('timestamp');
50
        $processor->setTimezone('Europe/Rome');
51
        $processor->setLocale('ITALIAN');
52
53
        $expected = [
54
            'date' => [
55
                'field' => 'initial_date',
56
                'formats' => ['dd/MM/yyyy hh:mm:ss', 'ISO8601', 'UNIX', 'UNIX_MS'],
57
                'target_field' => 'timestamp',
58
                'timezone' => 'Europe/Rome',
59
                'locale' => 'ITALIAN',
60
            ],
61
        ];
62
63
        $this->assertEquals($expected, $processor->toArray());
64
    }
65
66
    /**
67
     * @group functional
68
     */
69
    public function testDateField(): void
70
    {
71
        $date = new DateProcessor('date_field', ['yyyy dd MM hh:mm:ss']);
72
        $date->setTargetField('date_parsed');
73
        $date->setTimezone('Europe/Rome');
74
        $date->setLocale('IT');
75
76
        $pipeline = $this->_createPipeline('my_custom_pipeline', 'pipeline for Date');
77
        $pipeline->addProcessor($date)->create();
78
79
        $index = $this->_createIndex();
80
81
        // Add document to normal index
82
        $doc1 = new Document(null, ['date_field' => '2010 12 06 11:05:15']);
83
84
        $bulk = new Bulk($index->getClient());
85
        $bulk->setIndex($index);
86
87
        $bulk->addDocument($doc1);
88
        $bulk->setRequestParam('pipeline', 'my_custom_pipeline');
89
90
        $bulk->send();
91
        $index->refresh();
92
93
        $result = $index->search('*');
94
95
        $this->assertCount(1, $result->getResults());
0 ignored issues
show
Documentation introduced by
$result->getResults() is of type array<integer,object<Elastica\Result>>, but the function expects a object<Countable>|object...nit\Framework\iterable>.

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...
96
97
        $results = $result->getResults();
98
99
        $this->assertEquals('2010-06-12T00:00:00.000+02:00', ($results[0]->getHit())['_source']['date_parsed']);
100
    }
101
}
102