Passed
Branch master (b37043)
by Timo
11:18
created

LastSearchesServiceTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 110
Duplicated Lines 10.91 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 1 Features 0
Metric Value
dl 12
loc 110
rs 10
c 1
b 1
f 0
wmc 7
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
3
namespace ApacheSolrForTypo3\Solr\Tests\Unit\Domain\Search\ResultSet;
4
5
/***************************************************************
6
 *  Copyright notice
7
 *
8
 *  (c) 2015-2016 Timo Schmidt
9
 *  All rights reserved
10
 *
11
 *  This script is part of the TYPO3 project. The TYPO3 project is
12
 *  free software; you can redistribute it and/or modify
13
 *  it under the terms of the GNU General Public License as published by
14
 *  the Free Software Foundation; either version 2 of the License, or
15
 *  (at your option) any later version.
16
 *
17
 *  The GNU General Public License can be found at
18
 *  http://www.gnu.org/copyleft/gpl.html.
19
 *
20
 *  This script is distributed in the hope that it will be useful,
21
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
 *  GNU General Public License for more details.
24
 *
25
 *  This copyright notice MUST APPEAR in all copies of the script!
26
 ***************************************************************/
27
28
use ApacheSolrForTypo3\Solr\Domain\Search\LastSearches\LastSearchesService;
29
use ApacheSolrForTypo3\Solr\System\Configuration\TypoScriptConfiguration;
30
use ApacheSolrForTypo3\Solr\Tests\Unit\UnitTest;
31
use TYPO3\CMS\Core\Cache\Frontend\AbstractFrontend;
32
use TYPO3\CMS\Core\Database\DatabaseConnection;
33
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
34
35
class LastSearchesServiceTest extends UnitTest
36
{
37
    /**
38
     * @var LastSearchesService
39
     */
40
    protected $lastSearchesService;
41
42
    /**
43
     * @var TypoScriptFrontendController
44
     */
45
    protected $tsfeMock;
46
47
    /**
48
     * @var DatabaseConnection
49
     */
50
    protected $databaseMock;
51
52
    /**
53
     * @var TypoScriptConfiguration
54
     */
55
    protected $configurationMock;
56
57
    /**
58
     * @return void
59
     */
60
    public function setUp()
61
    {
62
        $this->tsfeMock = $this->getDumbMock('TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController');
63
        $this->databaseMock = $this->getDumbMock('TYPO3\CMS\Core\Database\DatabaseConnection');
64
        $this->configurationMock = $this->getDumbMock('\ApacheSolrForTypo3\Solr\System\Configuration\TypoScriptConfiguration');
65
66
        $this->lastSearchesService = $this->getMockBuilder(LastSearchesService::class)
67
            ->setMethods(['getLastSearchesFromFrontendSession'])
68
            ->setConstructorArgs([  $this->configurationMock,
69
                $this->tsfeMock,
70
                $this->databaseMock])->getMock();
71
    }
72
73
    /**
74
     * @test
75
     */
76
    public function canGetLastSearchesFromSessionInUserMode()
77
    {
78
        $fakedLastSearchesInSession = array('first search', 'second search');
79
80
        $this->lastSearchesService->expects($this->once())->method('getLastSearchesFromFrontendSession')->will($this->returnValue(
81
            $fakedLastSearchesInSession
82
        ));
83
84
        $this->assertDatabaseWillNeverBeQueried();
85
        $this->fakeLastSearchMode('user');
86
        $this->fakeLastSearchLimit(10);
87
88
        $lastSearches = $this->lastSearchesService->getLastSearches();
89
        $this->assertSame($fakedLastSearchesInSession, array_reverse($lastSearches), 'Did not get last searches from session in user mode');
90
    }
91
92
    /**
93
     * @test
94
     */
95
    public function canGetLastSearchesFromDatabaseInGlobalMode()
96
    {
97
        $fakedLastSearchesInDatabase = array(
98
            array('keywords' => 'test'),
99
            array('keywords' => 'test 2')
100
        );
101
102
        $this->fakeLastSearchMode('global');
103
        $this->fakeLastSearchLimit(10);
104
        $this->assertSessionWillNeverBeQueried();
105
106
        $this->databaseMock->expects($this->once())->method('exec_SELECTgetRows')->will($this->returnValue($fakedLastSearchesInDatabase));
107
108
        $lastSearches = $this->lastSearchesService->getLastSearches();
109
110
        $this->assertSame(array('test', 'test 2'), $lastSearches, 'Did not get last searches from database');
111
    }
112
113
    /**
114
     * @param string $mode
115
     */
116
    protected function fakeLastSearchMode($mode)
117
    {
118
        $this->configurationMock->expects($this->once())->method('getSearchLastSearchesMode')->will($this->returnValue($mode));
119
    }
120
121
    /**
122
     * @param integer $limit
123
     */
124
    protected function fakeLastSearchLimit($limit)
125
    {
126
        $this->configurationMock->expects($this->once())->method('getSearchLastSearchesLimit')->will($this->returnValue($limit));
127
    }
128
129
    /**
130
     * @return void
131
     */
132
    protected function assertDatabaseWillNeverBeQueried()
133
    {
134
        $this->databaseMock->expects($this->never())->method('exec_SELECTgetRows');
135
    }
136
137
    /**
138
     * @return void
139
     */
140
    protected function assertSessionWillNeverBeQueried()
141
    {
142
        $this->lastSearchesService->expects($this->never())->method('getLastSearchesFromFrontendSession');
143
    }
144
}
145