Completed
Push — master ( f71d4e...c66cbf )
by
unknown
08:22 queued 01:15
created

MediaRepositoryTest::testCountFiltered()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
namespace OpenOrchestra\FunctionalTests\MediaBundle\Repository;
4
5
use OpenOrchestra\BaseBundle\Tests\AbstractTest\AbstractKernelTestCase;
6
use OpenOrchestra\Media\Repository\MediaRepositoryInterface;
7
use OpenOrchestra\ModelInterface\Repository\RepositoryTrait\KeywordableTraitInterface;
8
use OpenOrchestra\Pagination\Configuration\PaginateFinderConfiguration;
9
10
/**
11
 * Class MediaRepositoryTest
12
 *
13
 * @group integrationTest
14
 */
15
class MediaRepositoryTest extends AbstractKernelTestCase
16
{
17
    /**
18
     * @var MediaRepositoryInterface
19
     */
20
    protected $repository;
21
    protected $keywordRepository;
22
23
    /**
24
     * Set up test
25
     */
26
    protected function setUp()
27
    {
28
        parent::setUp();
29
30
        static::bootKernel();
31
        $this->keywordRepository = static::$kernel->getContainer()->get('open_orchestra_model.repository.keyword');
32
        $this->repository = static::$kernel->getContainer()->get('open_orchestra_media.repository.media');
33
    }
34
35
    /**
36
     * @param string $keywords
37
     * @param int    $count
38
     *
39
     * @dataProvider provideKeywordAndCount
40
     */
41
    public function testFindByKeywords($keywords, $count)
42
    {
43
        $keywords = $this->replaceKeywordLabelById($keywords);
44
        $keywords = $this->repository->findByKeywords($keywords);
45
46
        $this->assertCount($count, $keywords);
47
    }
48
49
    /**
50
     * @return array
51
     */
52
    public function provideKeywordAndCount()
53
    {
54
        return array(
55
            array('lorem', 5),
56
            array('sit', 0),
57
            array('dolor', 4),
58
            array('lorem OR dolor', 5),
59
        );
60
    }
61
62
    /**
63
     * @param string $condition
64
     *
65
     * @return array
66
     */
67 View Code Duplication
    protected function replaceKeywordLabelById($condition)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
68
    {
69
        $conditionWithoutOperator = preg_replace(explode('|', KeywordableTraitInterface::OPERATOR_SPLIT), ' ', $condition);
70
        $conditionArray = explode(' ', $conditionWithoutOperator);
71
72
        foreach ($conditionArray as $keyword) {
73
            if ($keyword != '') {
74
                $keywordDocument = $this->keywordRepository->findOneByLabel($keyword);
75
                if (!is_null($keywordDocument)) {
76
                    $condition = str_replace($keyword, $keywordDocument->getId(), $condition);
77
                } else {
78
                    return '';
79
                }
80
            }
81
        }
82
83
        return $condition;
84
    }
85
86
    /**
87
     * test findForPaginate
88
     *
89
     * @param PaginateFinderConfiguration $configuration
90
     * @param int                         $expectedCount
91
     * @param int                         $expectedFilteredCount
92
     *
93
     * @dataProvider providePaginateConfiguration
94
     */
95
    public function testFindForPaginate(PaginateFinderConfiguration $configuration, $expectedCount, $expectedFilteredCount)
0 ignored issues
show
Unused Code introduced by
The parameter $expectedFilteredCount 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...
96
    {
97
        $this->assertCount($expectedCount, $this->repository->findForPaginate($configuration));
98
    }
99
100
    /**
101
     * test count
102
     */
103
    public function testCount()
104
    {
105
        $this->assertSame(6, $this->repository->count());
106
    }
107
108
    /**
109
     * test count with Filtertered
110
     */
111
    public function testCountFiltered()
112
    {
113
        $this->assertSame(1, $this->repository->count('pdf'));
0 ignored issues
show
Unused Code introduced by
The call to MediaRepositoryInterface::count() has too many arguments starting with 'pdf'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
114
        $this->assertSame(5, $this->repository->count('image'));
0 ignored issues
show
Unused Code introduced by
The call to MediaRepositoryInterface::count() has too many arguments starting with 'image'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
115
    }
116
117
    /**
118
     * test countWithFilter
119
     *
120
     * @param PaginateFinderConfiguration $configuration
121
     * @param int                         $expectedCount
122
     * @param int                         $expectedFilteredCount
123
     *
124
     * @dataProvider providePaginateConfiguration
125
     */
126
    public function testCountWithFilter(PaginateFinderConfiguration $configuration, $expectedCount, $expectedFilteredCount)
0 ignored issues
show
Unused Code introduced by
The parameter $expectedCount 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...
127
    {
128
        $this->assertSame($expectedFilteredCount, $this->repository->countWithFilter($configuration));
129
    }
130
131
    /**
132
     * Provide PaginateFinderConfiguration
133
     *
134
     * @return array
135
     */
136
    public function providePaginateConfiguration()
137
    {
138
        $mapping =  array();
139
        $conf1 = PaginateFinderConfiguration::generateFromVariable(null , null, null, $mapping, null);
140
        $conf2 = PaginateFinderConfiguration::generateFromVariable(null , null, null, $mapping, array('label' => 'dolor', 'language' => 'en'));
141
        $conf3 = PaginateFinderConfiguration::generateFromVariable(null , null, null, $mapping, array('type' => 'pdf'));
142
143
        return array(
144
            'No criteria'       => array($conf1, 6, 6),
145
            'Filtering "dolor"' => array($conf2, 4, 4),
146
            'Filtering pdf'     => array($conf3, 1, 1),
147
        );
148
    }
149
150
    /**
151
     * Test remove medias
152
     */
153 View Code Duplication
    public function testRemoveMedias()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
154
    {
155
        $dm = static::$kernel->getContainer()->get('object_manager');
156
        $image01 = $this->repository->findOneByName('Image 01');
157
        $image02 = $this->repository->findOneByName('Image 02');
158
159
        $mediaIds = array($image01->getId(), $image02->getId());
160
161
        $this->repository->removeMedias($mediaIds);
162
        $this->assertNull($this->repository->findOneByName('Image 01'));
163
        $this->assertNull($this->repository->findOneByName('Image 02'));
164
165
        $dm->persist(clone $image01);
166
        $dm->persist(clone $image02);
167
        $dm->flush();
168
    }
169
}
170