Completed
Push — master ( 6e92d2...d7f3a5 )
by
unknown
109:07 queued 86:32
created

AdminList/FormPageAdminListConfiguratorTest.php (3 issues)

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\FormBundle\Tests\AdminList;
4
5
use Codeception\Stub;
6
use Doctrine\ORM\Configuration;
7
use Doctrine\ORM\EntityRepository;
8
use Doctrine\ORM\QueryBuilder;
9
use Kunstmaan\AdminListBundle\AdminList\ItemAction\SimpleItemAction;
10
use Kunstmaan\FormBundle\AdminList\FormPageAdminListConfigurator;
11
use Kunstmaan\NodeBundle\Entity\AbstractPage;
12
13
/**
14
 * This test tests the FormPageAdminListConfigurator
15
 */
16
class FormPageAdminListConfiguratorTest extends \PHPUnit_Framework_TestCase
17
{
18
    const PERMISSION_VIEW = 'view';
19
20
    /**
21
     * @var FormPageAdminListConfigurator
22
     */
23
    protected $object;
24
25
    /**
26
     * Sets up the fixture, for example, opens a network connection.
27
     * This method is called before a test is executed.
28
     */
29
    protected function setUp()
30
    {
31
        $em = $this->getMockedEntityManager();
32
        $tokenStorage = $this->createMock('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface');
33
        $roleHierarchy = $this->getMockBuilder('Symfony\Component\Security\Core\Role\RoleHierarchyInterface')
34
          ->getMock();
35
        $aclHelper = $this->createMock('Kunstmaan\AdminBundle\Helper\Security\Acl\AclHelper', array(), array($em, $tokenStorage, $roleHierarchy));
36
37
        $this->object = new FormPageAdminListConfigurator($em, $aclHelper, self::PERMISSION_VIEW);
38
    }
39
40
    /**
41
     * https://gist.github.com/1331789
42
     *
43
     * @return \Doctrine\ORM\EntityManager
0 ignored issues
show
Should the return type not be \PHPUnit_Framework_MockObject_MockObject?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
44
     */
45 View Code Duplication
    protected function getMockedEntityManager()
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...
46
    {
47
        $repository = Stub::make(EntityRepository::class, [
48
          'find' => null
49
        ]);
50
        $configuration = Stub::make(Configuration::class, [
51
           'getQuoteStrategy' => null
52
        ]);
53
        $emMock  = $this->createMock('\Doctrine\ORM\EntityManager', array('getRepository', 'getConfiguration', 'getClassMetadata', 'persist', 'flush'), array(), '', false);
0 ignored issues
show
The call to FormPageAdminListConfiguratorTest::createMock() has too many arguments starting with array('getRepository', '...a', 'persist', 'flush').

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...
54
        $emMock->expects($this->any())
55
            ->method('getRepository')
56
            ->will($this->returnValue($repository));
57
        $emMock->expects($this->any())
58
            ->method('getConfiguration')
59
            ->will($this->returnValue($configuration));
60
        $emMock->expects($this->any())
61
            ->method('getClassMetadata')
62
            ->will($this->returnValue((object) array('name' => 'aClass')));
63
        $emMock->expects($this->any())
64
            ->method('persist')
65
            ->will($this->returnValue(null));
66
        $emMock->expects($this->any())
67
            ->method('flush')
68
            ->will($this->returnValue(null));
69
70
        return $emMock;  // it tooks 13 lines to achieve mock!
71
    }
72
73
    public function testAdaptQueryBuilder()
74
    {
75
        $queryBuilder = $this->getMockBuilder('Doctrine\ORM\QueryBuilder')
76
            ->disableOriginalConstructor()
77
            ->getMock();
78
79
        $queryBuilder->expects($this->any())
80
            ->method('innerJoin')
81
            ->will($this->returnSelf());
82
83
        $queryBuilder->expects($this->any())
84
            ->method('andWhere')
85
            ->will($this->returnSelf());
86
87
        /* @var $queryBuilder QueryBuilder */
88
        $this->object->adaptQueryBuilder($queryBuilder);
89
    }
90
91 View Code Duplication
    public function testFixedGetters()
92
    {
93
        $item = Stub::makeEmpty(AbstractPage::class, ['getId' => 123]);
94
        $this->assertEquals('', $this->object->getAddUrlFor([]));
95
        $this->assertEquals('KunstmaanNodeBundle', $this->object->getBundleName());
96
        $this->assertEquals('NodeTranslation', $this->object->getEntityName());
97
        $this->assertEquals('KunstmaanFormBundle:FormSubmissions', $this->object->getControllerPath());
98
        $this->assertCount(0, $this->object->getDeleteUrlFor($item));
99
        $this->assertCount(1, $this->object->getIndexUrl());
100
        $this->assertCount(2, $this->object->getEditUrlFor($item));
101
        $this->assertFalse($this->object->canAdd());
102
        $this->assertFalse($this->object->canEdit($item));
103
        $this->assertFalse($this->object->canDelete($item));
104
    }
105
106
    public function testBuildFilters()
107
    {
108
        $this->object->buildFilters();
109
        $filters = $this->object->getFilterBuilder()->getFilterDefinitions();
110
        $this->assertCount(2, $filters);
111
    }
112
113
    public function testBuildFields()
114
    {
115
        $this->object->buildFields();
116
        $fields = $this->object->getFields();
117
        $this->assertCount(3, $fields);
118
    }
119
120 View Code Duplication
    public function testBuildItemActions()
121
    {
122
        $item = Stub::makeEmpty(AbstractPage::class, ['getId' => 123]);
123
        $this->object->buildItemActions();
124
        $actions = $this->object->getItemActions();
125
        $this->assertCount(1, $actions);
126
        $this->assertInstanceOf(SimpleItemAction::class, $actions[0]);
127
        /** @var SimpleItemAction $action */
128
        $action = $actions[0];
129
        $this->assertCount(2, $action->getUrlFor($item));
130
    }
131
}
132