Completed
Push — master ( b1b8c7...b37043 )
by Timo
06:43
created

ServiceTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 95
Duplicated Lines 60 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 57
loc 95
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
namespace ApacheSolrForTypo3\Solr\Tests\Unit\FieldProcessor;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2009-2015 Daniel Poetzinger <[email protected]>
8
 *  All rights reserved
9
 *
10
 *  This script is part of the TYPO3 project. The TYPO3 project is
11
 *  free software; you can redistribute it and/or modify
12
 *  it under the terms of the GNU General Public License as published by
13
 *  the Free Software Foundation; either version 2 of the License, or
14
 *  (at your option) any later version.
15
 *
16
 *  The GNU General Public License can be found at
17
 *  http://www.gnu.org/copyleft/gpl.html.
18
 *
19
 *  This script is distributed in the hope that it will be useful,
20
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
 *  GNU General Public License for more details.
23
 *
24
 *  This copyright notice MUST APPEAR in all copies of the script!
25
 ***************************************************************/
26
27
use ApacheSolrForTypo3\Solr\FieldProcessor\Service;
28
use ApacheSolrForTypo3\Solr\Tests\Unit\UnitTest;
29
30
/**
31
 * tests the processing Service class
32
 *
33
 * @author Daniel Poetzinger <[email protected]>
34
 * @package TYPO3
35
 * @subpackage solr
36
 */
37
class ServiceTest extends UnitTest
38
{
39
40
    /**
41
     * @var Apache_Solr_Document
42
     */
43
    protected $documentMock;
44
45
    /**
46
     * the service
47
     *
48
     * @var Service
49
     */
50
    protected $service;
51
52
    public function setUp()
53
    {
54
        date_default_timezone_set('Europe/Berlin');
55
        $this->documentMock = new \Apache_Solr_Document();
56
        $this->service = new Service();
57
    }
58
59
    /**
60
     * @test
61
     */
62
    public function transformsStringToUppercaseOnSingleValuedField()
63
    {
64
        $this->documentMock->setField('stringField', 'stringvalue');
65
        $configuration = array('stringField' => 'uppercase');
66
67
        $this->service->processDocument($this->documentMock, $configuration);
68
        $value = $this->documentMock->getField('stringField');
69
        $this->assertEquals(
70
            $value['value'],
71
            'STRINGVALUE',
72
            'field was not processed with uppercase'
73
        );
74
    }
75
76
    /**
77
     * @test
78
     */
79
    public function transformsStringToUppercaseOnMultiValuedField()
80
    {
81
        $this->documentMock->addField('stringField', 'stringvalue_1');
82
        $this->documentMock->addField('stringField', 'stringvalue_2');
83
        $configuration = array('stringField' => 'uppercase');
84
85
        $this->service->processDocument($this->documentMock, $configuration);
86
        $value = $this->documentMock->getField('stringField');
87
        $this->assertEquals(
88
            $value['value'],
89
            array('STRINGVALUE_1', 'STRINGVALUE_2'),
90
            'field was not processed with uppercase'
91
        );
92
    }
93
94
    /**
95
     * @test
96
     */
97
    public function transformsUnixTimestampToIsoDateOnSingleValuedField()
98
    {
99
        $this->documentMock->setField('dateField',
100
            '1262343600'); // 2010-01-01 12:00
101
        $configuration = array('dateField' => 'timestampToIsoDate');
102
103
        $this->service->processDocument($this->documentMock, $configuration);
104
        $value = $this->documentMock->getField('dateField');
105
        $this->assertEquals(
106
            $value['value'],
107
            '2010-01-01T12:00:00Z',
108
            'field was not processed with timestampToIsoDate'
109
        );
110
    }
111
112
    /**
113
     * @test
114
     */
115
    public function transformsUnixTimestampToIsoDateOnMultiValuedField()
116
    {
117
        $this->documentMock->addField('dateField',
118
            '1262343600'); // 2010-01-01 12:00
119
        $this->documentMock->addField('dateField',
120
            '1262343601'); // 2010-01-01 12:01
121
        $configuration = array('dateField' => 'timestampToIsoDate');
122
123
        $this->service->processDocument($this->documentMock, $configuration);
124
        $value = $this->documentMock->getField('dateField');
125
        $this->assertEquals(
126
            $value['value'],
127
            array('2010-01-01T12:00:00Z', '2010-01-01T12:00:01Z'),
128
            'field was not processed with timestampToIsoDate'
129
        );
130
    }
131
}
132