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

FormSubmissionExportListConfiguratorTest.php (1 issue)

Severity

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\Common\Collections\ArrayCollection;
7
use Doctrine\ORM\Configuration;
8
use Doctrine\ORM\EntityRepository;
9
use Kunstmaan\FormBundle\AdminList\FormSubmissionExportListConfigurator;
10
use Kunstmaan\FormBundle\Entity\FormSubmission;
11
use Kunstmaan\FormBundle\Entity\FormSubmissionFieldTypes\BooleanFormSubmissionField;
12
use Kunstmaan\NodeBundle\Entity\Node;
13
use Kunstmaan\NodeBundle\Entity\NodeTranslation;
14
use Symfony\Component\Translation\Translator;
15
16
class FakeFormSubmission extends FormSubmission
17
{
18
    public function setFields(ArrayCollection $fields)
19
    {
20
        $this->fields = $fields;
21
    }
22
}
23
24
/**
25
 * This test tests the FormPageAdminListConfigurator
26
 */
27
class FormSubmissionExportListConfiguratorTest extends \PHPUnit_Framework_TestCase
28
{
29
    /**
30
     * @var FormSubmissionExportListConfigurator
31
     */
32
    protected $object;
33
34
    /**
35
     * Sets up the fixture, for example, opens a network connection.
36
     * This method is called before a test is executed.
37
     */
38 View Code Duplication
    protected function setUp()
39
    {
40
        $em = $this->getMockedEntityManager();
41
        $node = new Node();
42
        $node->setId(666);
43
        $nt = new NodeTranslation();
44
        $nt->setNode($node);
45
        $translator = new Translator('nl');
46
        $this->object = new FormSubmissionExportListConfigurator($em, $nt, $translator);
47
    }
48
49
    /**
50
     * https://gist.github.com/1331789
51
     *
52
     * @return \Doctrine\ORM\EntityManager
53
     */
54
    protected function getMockedEntityManager()
55
    {
56
        $queryBuilder = $this->getMockBuilder('Doctrine\ORM\QueryBuilder')
57
            ->disableOriginalConstructor()
58
            ->getMock();
59
60
        $field = new BooleanFormSubmissionField();
61
        $field->setFieldName('check');
62
        $field->setValue(true);
63
64
        $sub = new FakeFormSubmission();
65
        $sub->setFields(new ArrayCollection([
66
            $field,
67
        ]));
68
69
70
        $submissions = [
71
            [$sub],
72
            [new FormSubmission()],
73
            [new FormSubmission()],
74
        ];
75
        $query = $this->getMockBuilder('Doctrine\ORM\AbstractQuery')
76
            ->disableOriginalConstructor()
77
            ->getMock();
78
79
        $query->expects($this->any())
80
            ->method('iterate')
81
            ->willReturn($submissions);
82
83
        $methods = [
84
            'select', 'from', 'innerJoin', 'andWhere', 'setParameter', 'addOrderBy'
85
        ];
86
        foreach ($methods as $method) {
87
            $queryBuilder->expects($this->any())
88
                ->method($method)
89
                ->willReturn($queryBuilder);
90
        }
91
92
        $queryBuilder->expects($this->any())
93
            ->method('getQuery')
94
            ->willReturn($query);
95
96
        $repository = Stub::make(EntityRepository::class, [
97
            'find' => null
98
        ]);
99
        $configuration = Stub::make(Configuration::class, [
100
            'getQuoteStrategy' => null
101
        ]);
102
        $emMock  = $this->createMock('\Doctrine\ORM\EntityManager', [], [], '', false);
0 ignored issues
show
The call to FormSubmissionExportList...ratorTest::createMock() has too many arguments starting with array().

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...
103
        $emMock->expects($this->any())
104
            ->method('getRepository')
105
            ->will($this->returnValue($repository));
106
        $emMock->expects($this->any())
107
            ->method('getConfiguration')
108
            ->will($this->returnValue($configuration));
109
        $emMock->expects($this->any())
110
            ->method('getClassMetadata')
111
            ->will($this->returnValue((object) array('name' => 'aClass')));
112
        $emMock->expects($this->any())
113
            ->method('persist')
114
            ->will($this->returnValue(null));
115
        $emMock->expects($this->any())
116
            ->method('flush')
117
            ->will($this->returnValue(null));
118
        $emMock->expects($this->any())
119
            ->method('createQueryBuilder')
120
            ->will($this->returnValue($queryBuilder));
121
122
        /** @var  \Doctrine\ORM\EntityManager $emMock */
123
        return $emMock;
124
    }
125
126
    public function testGetStringValue()
127
    {
128
        $this->assertNull($this->object->buildFilters());
129
        $this->assertEquals('', $this->object->getStringValue([], 'fail'));
130
        $this->assertEquals('pass', $this->object->getStringValue(['test' => 'pass'], 'test'));
131
    }
132
133
    public function testBuildExportFields()
134
    {
135
        $this->object->buildExportFields();
136
        $this->assertCount(3, $this->object->getExportFields());
137
        $this->object->addExportField('abc', 'def');
138
        $this->assertCount(4, $this->object->getExportFields());
139
    }
140
141
    public function testBuildIterator()
142
    {
143
        $this->object->buildIterator();
144
        $filters = $this->object->getIterator();
145
        $this->assertInstanceOf(\ArrayIterator::class, $filters);
146
        $first = $filters->current();
147
        $this->assertCount(1, $first);
148
        $first = $first[0];
149
        $this->assertArrayHasKey('check', $first);
150
        $this->assertEquals('true', $first['check']);
151
152
    }
153
}
154