Completed
Push — master ( c3eba2...34879d )
by amaury
05:12 queued 01:44
created

GroupBundle/Repository/GroupRepositoryTest.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\GroupBundle\Repository;
4
5
use OpenOrchestra\Backoffice\Repository\GroupRepositoryInterface;
6
use OpenOrchestra\BaseBundle\Tests\AbstractTest\AbstractKernelTestCase;
7
use OpenOrchestra\ModelBundle\Repository\SiteRepository;
8
use OpenOrchestra\Pagination\Configuration\PaginateFinderConfiguration;
9
use OpenOrchestra\GroupBundle\Document\Group;
10
11
/**
12
 * Class GroupRepositoryTest
13
 *
14
 * @group integrationTest
15
 */
16
class GroupRepositoryTest extends AbstractKernelTestCase
17
{
18
    /**
19
     * @var GroupRepositoryInterface
20
     */
21
    protected $repository;
22
23
    /**
24
     * @var SiteRepository
25
     */
26
    protected $siteRepository;
27
28
    /**
29
     * Set up test
30
     */
31 View Code Duplication
    protected function setUp()
32
    {
33
        parent::setUp();
34
35
        static::bootKernel();
36
        $this->repository = static::$kernel->getContainer()->get('open_orchestra_user.repository.group');
37
        $this->siteRepository = static::$kernel->getContainer()->get('open_orchestra_model.repository.site');
38
    }
39
40
    /**
41
     * @param PaginateFinderConfiguration $configuration
42
     * @param array                       $siteIds
43
     * @param int                         $count
44
     *
45
     * @dataProvider provideConfigurationAndSites
46
     */
47
    public function testFindForPaginate(PaginateFinderConfiguration $configuration, array $siteIds, $count)
48
    {
49
        $siteIds = $this->generateMongoIdForSite($siteIds);
50
        $groups = $this->repository->findForPaginate($configuration, $siteIds);
51
52
        $this->assertCount($count, $groups);
53
    }
54
55
    /**
56
     * test count all user
57
     *
58
     * @param PaginateFinderConfiguration $configuration
59
     * @param array                       $siteIds
60
     * @param int                         $count
61
     *
62
     * @dataProvider provideConfigurationAndSites
63
     */
64
    public function testCount(PaginateFinderConfiguration $configuration, array $siteIds, $count)
65
    {
66
        $siteIds = $this->generateMongoIdForSite($siteIds);
67
        $groups = $this->repository->count($siteIds);
68
69
        $this->assertEquals($count, $groups);
70
    }
71
72
    /**
73
     * @return array
74
     */
75
    public function provideConfigurationAndSites()
76
    {
77
        $configuration = new PaginateFinderConfiguration();
78
        $configuration->setPaginateConfiguration(null, 0, 100, array('label' => 'labels'));
79
        return array(
80
            array($configuration, array(), 0),
81
            array($configuration, array('2'), 2),
82
            array($configuration, array('2', '3'), 3),
83
            array($configuration, array('test'), 0),
84
        );
85
    }
86
87
    /**
88
     * @param PaginateFinderConfiguration $configuration
89
     * @param array                       $siteIds
90
     * @param int                         $count
91
     *
92
     * @dataProvider provideColumnsAndSearchAndCount
93
     */
94
    public function testCountWithFilter(PaginateFinderConfiguration $configuration, array $siteIds, $count)
95
    {
96
        $siteIds = $this->generateMongoIdForSite($siteIds);
97
        $groups = $this->repository->countWithFilter($configuration, $siteIds);
98
99
        $this->assertEquals($count, $groups);
100
    }
101
102
    /**
103
     * @return array
104
     */
105
    public function provideColumnsAndSearchAndCount(){
106
107
        $configuration = new PaginateFinderConfiguration();
108
        $configuration->setPaginateConfiguration(null, 0, 100, array('label' => 'labels'));
109
110
        $configuration->setSearch(array('language' => 'en', 'label' => 'site'));
111
112
        return array(
113
            array($configuration, array(), 0),
114
            array($configuration, array('2'), 1),
115
            array($configuration, array('2', '3'), 1),
116
            array($configuration, array('test'), 0),
117
        );
118
    }
119
120
    /**
121
     * Test remove users
122
     */
123
    public function testRemoveGroups()
124
    {
125
        $dm = static::$kernel->getContainer()->get('object_manager');
126
127
        $groupTest = new Group();
128
        $groupTest->setName('test');
129
        $dm->persist($groupTest);
130
        $dm->flush();
131
        $dm->clear();
132
133
        $groupTest = $this->repository->findOneByName('test');
134
135
        $groupIds = array($groupTest->getId());
136
137
        $this->repository->removeGroups($groupIds);
138
        $this->assertNull($this->repository->findOneByName('test'));
139
    }
140
141
   /**
142
     * Test remove users
143
     */
144
    public function testSoftDeleteGroupBySite()
145
    {
146
        $dm = static::$kernel->getContainer()->get('object_manager');
147
        $site = $this->siteRepository->findOneBySiteId('3');
148
        $this->repository->softDeleteGroupsBySite($site);
0 ignored issues
show
It seems like $site defined by $this->siteRepository->findOneBySiteId('3') on line 147 can be null; however, OpenOrchestra\Backoffice...oftDeleteGroupsBySite() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
149
150
        $groups = $this->repository->findBy(
151
            array('site.$id' => new \MongoId($site->getId()))
152
        );
153
154
        foreach ($groups as $group) {
155
            $this->assertTrue($group->isDeleted());
156
            $group->setDeleted(false);
157
        }
158
        $dm->flush();
159
    }
160
161
    /**
162
     * Generate columns of content with search value
163
     *
164
     * @param string $searchName
165
     * @param string $globalSearch
166
     *
167
     * @return array
168
     */
169
    protected function generateSearchProvider($searchName = '', $globalSearch = '')
170
    {
171
        $search = array();
172
        if (!empty($searchName)) {
173
            $search['columns'] = array('name' => $searchName);
174
        }
175
        if (!empty($globalSearch)) {
176
            $search['global'] = $globalSearch;
177
        }
178
179
        return $search;
180
    }
181
182
    /**
183
     * @param array $siteIds
184
     *
185
     * @return array
186
     */
187
    protected function generateMongoIdForSite(array $siteIds)
188
    {
189
        foreach ($siteIds as $key => $siteId) {
190
            $site = $this->siteRepository->findOneBySiteId($siteId);
191
            if (null !== $site) {
192
                $siteIds[$key] = $site->getId();
193
            } else {
194
                unset($siteIds[$key]);
195
            }
196
        }
197
198
        return $siteIds;
199
    }
200
201
    /**
202
     * Generate relation between columns names and entities attributes
203
     *
204
     * @return array
205
     */
206
    protected function getDescriptionColumnEntity()
207
    {
208
209
        return array(
210
            'name' => array('key' => 'name', 'field' => 'name', 'type' => 'string')
211
        );
212
    }
213
214
}
215