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

Repository/MediaRepositoryTest.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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 View Code Duplication
    protected function setUp()
0 ignored issues
show
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...
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)
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)
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 countWithFilter
110
     *
111
     * @param PaginateFinderConfiguration $configuration
112
     * @param int                         $expectedCount
113
     * @param int                         $expectedFilteredCount
114
     *
115
     * @dataProvider providePaginateConfiguration
116
     */
117
    public function testCountWithFilter(PaginateFinderConfiguration $configuration, $expectedCount, $expectedFilteredCount)
118
    {
119
        $this->assertSame($expectedFilteredCount, $this->repository->countWithFilter($configuration));
120
    }
121
122
    /**
123
     * Provide PaginateFinderConfiguration
124
     *
125
     * @return array
126
     */
127
    public function providePaginateConfiguration()
128
    {
129
        $mapping =  array();
130
        $conf1 = PaginateFinderConfiguration::generateFromVariable(null , null, null, $mapping, null);
131
        $conf2 = PaginateFinderConfiguration::generateFromVariable(null , null, null, $mapping, array('label' => 'dolor', 'language' => 'en'));
132
        $conf3 = PaginateFinderConfiguration::generateFromVariable(null , null, null, $mapping, array('type' => 'pdf'));
133
134
        return array(
135
            'No criteria'       => array($conf1, 6, 6),
136
            'Filtering "dolor"' => array($conf2, 4, 4),
137
            'Filtering pdf'     => array($conf3, 1, 1),
138
        );
139
    }
140
141
    /**
142
     * Test remove medias
143
     */
144 View Code Duplication
    public function testRemoveMedias()
145
    {
146
        $dm = static::$kernel->getContainer()->get('object_manager');
147
        $image01 = $this->repository->findOneByName('Image 01');
148
        $image02 = $this->repository->findOneByName('Image 02');
149
150
        $mediaIds = array($image01->getId(), $image02->getId());
151
152
        $this->repository->removeMedias($mediaIds);
153
        $this->assertNull($this->repository->findOneByName('Image 01'));
154
        $this->assertNull($this->repository->findOneByName('Image 02'));
155
156
        $dm->persist(clone $image01);
157
        $dm->persist(clone $image02);
158
        $dm->flush();
159
    }
160
}
161