Completed
Push — master ( 1db3cd...632e40 )
by Jeroen
24:52 queued 11:31
created

AbstractArticlePageAdminListConfiguratorTest.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 Kunstmaan\ArticleBundle\Tests\AdminList;
4
5
use Doctrine\ORM\EntityManager;
6
use Doctrine\ORM\EntityRepository;
7
use Doctrine\ORM\QueryBuilder;
8
use Kunstmaan\AdminBundle\Helper\Security\Acl\AclHelper;
9
use Kunstmaan\ArticleBundle\AdminList\AbstractArticlePageAdminListConfigurator;
10
use Kunstmaan\NodeBundle\Entity\Node;
11
use Kunstmaan\NodeBundle\Entity\NodeTranslation;
12
use Kunstmaan\NodeBundle\Helper\NodeMenu;
13
use Kunstmaan\NodeBundle\Helper\NodeMenuItem;
14
use PHPUnit\Framework\TestCase;
15
use ReflectionClass;
16
17
class Configurator extends AbstractArticlePageAdminListConfigurator
18
{
19
    /** @var EntityRepository */
20
    private $repo;
21
22
    public function __construct(EntityManager $em, AclHelper $aclHelper, $locale, $permission, $repo)
23
    {
24
        parent::__construct($em, $aclHelper, $locale, $permission);
25
        $this->repo = $repo;
26
    }
27
28
    /**
29
     * @return bool
30
     */
31
    public function getOverviewPageRepository()
32
    {
33
        return $this->repo;
34
    }
35
}
36
37
/**
38
 * Class AbstractArticlePageAdminListConfiguratorTest
39
 */
40
class AbstractArticlePageAdminListConfiguratorTest extends TestCase
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
41
{
42
    /**
43
     * @var AbstractArticlePageAdminListConfigurator
44
     */
45
    protected $object;
46
47
    /**
48
     * @var EntityManager
49
     */
50
    protected $em;
51
52
    /**
53
     * Sets up the fixture, for example, opens a network connection.
54
     * This method is called before a test is executed.
55
     */
56
    protected function setUp()
57
    {
58
        $em = $this->createMock(EntityManager::class);
59
        $em->expects($this->any())
60
            ->method($this->anything())
61
            ->willReturn($em);
62
63
        $acl = $this->createMock(AclHelper::class);
64
65
        $repo = $this->createMock(EntityRepository::class);
66
        $repo->expects($this->any())
67
            ->method($this->anything())
68
            ->willReturn([['fake' => 'array']]);
69
70
        $this->em = $em;
71
72
        /* @var EntityManager $em */
73
        /* @var AclHelper $acl */
74
        $this->object = new Configurator($em, $acl, 'nl', 'admin', $repo);
75
    }
76
77
    public function testGetters()
78
    {
79
        $this->assertEquals('KunstmaanArticleBundle', $this->object->getBundleName());
80
        $this->assertEquals('AbstractArticlePage', $this->object->getEntityName());
81
        $this->assertEquals('KunstmaanArticleBundle:AbstractArticlePageAdminList:list.html.twig', $this->object->getListTemplate());
82
        $this->assertEquals('KunstmaanArticleBundle:AbstractArticlePage', $this->object->getRepositoryName());
83
    }
84
85
    public function testBuildFields()
86
    {
87
        $this->object->buildFields();
88
        $this->object->buildFilters();
89
        $fields = $this->object->getFields();
90
        $this->assertCount(4, $fields);
91
    }
92
93
    public function testGetUrls()
94
    {
95
        $node = new Node();
96
        $node->setId(1314);
97
        $nodeTranslation = new NodeTranslation();
98
        /** @var NodeMenu $menu */
99
        $menu = $this->createMock(NodeMenu::class);
100
        $item = new NodeMenuItem($node, $nodeTranslation, false, $menu);
101
102
        $url = $this->object->getEditUrlFor($item);
103
104
        $this->assertCount(2, $url);
105
        $this->assertArrayHasKey('path', $url);
106
        $this->assertArrayHasKey('params', $url);
107
        $this->assertEquals('KunstmaanNodeBundle_nodes_edit', $url['path']);
108
        $this->assertCount(1, $url['params']);
109
        $this->assertEquals(1314, $url['params']['id']);
110
        $url = $this->object->getEditUrlFor($item);
111
112
        $url = $this->object->getDeleteUrlFor($item);
113
114
        $this->assertCount(2, $url);
115
        $this->assertArrayHasKey('path', $url);
116
        $this->assertArrayHasKey('params', $url);
117
        $this->assertEquals('KunstmaanNodeBundle_nodes_delete', $url['path']);
118
        $this->assertCount(1, $url['params']);
119
        $this->assertEquals(1314, $url['params']['id']);
120
    }
121
122
    /**
123
     * @throws \ReflectionException
124
     */
125
    public function testGetQueryBuilder()
126
    {
127
        $em = $this->createMock(EntityManager::class);
128
        $qb = $this->createMock(QueryBuilder::class);
129
        $em->expects($this->any())
130
            ->method('createQueryBuilder')
131
            ->willReturn($qb);
132
133
        $em->expects($this->any())
134
            ->method('getRepository')
135
            ->willReturn($em);
136
137
        $this->em = $em;
138
139
        $mirror = new ReflectionClass(Configurator::class);
140
        $method = $mirror->getMethod('getQueryBuilder');
141
        $method->setAccessible(true);
142
143
        $mirror = new ReflectionClass(Configurator::class);
144
        $prop = $mirror->getProperty('em');
145
        $prop->setAccessible(true);
146
        $prop->setValue($this->object, $em);
147
148
        /* @var QueryBuilder $qb */
149
        $this->object->adaptQueryBuilder($qb);
150
        $qb = $method->invoke($this->object);
151
        $this->assertInstanceOf(QueryBuilder::class, $qb);
152
    }
153
154
    /**
155
     * @throws \ReflectionException
156
     */
157
    public function testEntityClassName()
158
    {
159
        $em = $this->createMock(EntityManager::class);
160
        $repo = $this->createMock(EntityRepository::class);
161
162
        $em->expects($this->any())
163
            ->method('getRepository')
164
            ->willReturn($repo);
165
166
        $repo->expects($this->any())
167
            ->method('getClassName')
168
            ->willReturn(Configurator::class);
169
170
        $this->em = $em;
171
172
        $mirror = new ReflectionClass(Configurator::class);
173
        $method = $mirror->getMethod('getQueryBuilder');
174
        $method->setAccessible(true);
175
176
        $mirror = new ReflectionClass(Configurator::class);
177
        $prop = $mirror->getProperty('em');
178
        $prop->setAccessible(true);
179
        $prop->setValue($this->object, $em);
180
        $this->assertEquals(Configurator::class, $this->object->getEntityClassName());
181
    }
182
183
    /**
184
     * @throws \ReflectionException
185
     */
186
    public function testGetOverViewPages()
187
    {
188
        $this->assertCount(1, $this->object->getOverviewPages());
189
        $this->assertCount(1, $this->object->getOverviewPage());
190
191
        $mirror = new ReflectionClass(Configurator::class);
192
        $prop = $mirror->getProperty('repo');
193
        $prop->setAccessible(true);
194
195
        $repo = $this->createMock(EntityRepository::class);
196
        $repo->expects($this->any())
197
            ->method($this->anything())
198
            ->willReturn([]);
199
200
        $prop->setValue($this->object, $repo);
201
202
        $this->assertNull($this->object->getOverviewPage());
203
    }
204
}
205