Completed
Push — master ( fcb59d...326ddf )
by Ruud
299:19 queued 288:03
created

FormSubmissionExportListConfiguratorTest.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\FormBundle\Tests\AdminList;
4
5
use Doctrine\Common\Collections\ArrayCollection;
6
use Doctrine\ORM\Configuration;
7
use Doctrine\ORM\EntityManager;
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 PHPUnit\Framework\TestCase;
15
use Symfony\Component\Translation\Translator;
16
17
class FakeFormSubmission extends FormSubmission
18
{
19
    public function setFields(ArrayCollection $fields)
20
    {
21
        $this->fields = $fields;
22
    }
23
}
24
25
/**
26
 * This test tests the FormPageAdminListConfigurator
27
 */
28
class FormSubmissionExportListConfiguratorTest 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...
29
{
30
    /**
31
     * @var FormSubmissionExportListConfigurator
32
     */
33
    protected $object;
34
35
    /**
36
     * Sets up the fixture, for example, opens a network connection.
37
     * This method is called before a test is executed.
38
     */
39 View Code Duplication
    protected function setUp()
40
    {
41
        $em = $this->getMockedEntityManager();
42
        $node = new Node();
43
        $node->setId(666);
44
        $nt = new NodeTranslation();
45
        $nt->setNode($node);
46
        $translator = new Translator('nl');
47
        $this->object = new FormSubmissionExportListConfigurator($em, $nt, $translator);
48
    }
49
50
    /**
51
     * @return \Doctrine\ORM\EntityManager
52
     */
53
    protected function getMockedEntityManager()
54
    {
55
        $queryBuilder = $this->getMockBuilder('Doctrine\ORM\QueryBuilder')
56
            ->disableOriginalConstructor()
57
            ->getMock();
58
59
        $field = new BooleanFormSubmissionField();
60
        $field->setFieldName('check');
61
        $field->setValue(true);
62
63
        $sub = new FakeFormSubmission();
64
        $sub->setFields(new ArrayCollection([
65
            $field,
66
        ]));
67
68
        $submissions = [
69
            [$sub],
70
            [new FormSubmission()],
71
            [new FormSubmission()],
72
        ];
73
        $query = $this->getMockBuilder('Doctrine\ORM\AbstractQuery')
74
            ->disableOriginalConstructor()
75
            ->getMock();
76
77
        $query->expects($this->any())
78
            ->method('iterate')
79
            ->willReturn($submissions);
80
81
        $methods = [
82
            'select', 'from', 'innerJoin', 'andWhere', 'setParameter', 'addOrderBy',
83
        ];
84
        foreach ($methods as $method) {
85
            $queryBuilder->expects($this->any())
86
                ->method($method)
87
                ->willReturn($queryBuilder);
88
        }
89
90
        $queryBuilder->expects($this->any())
91
            ->method('getQuery')
92
            ->willReturn($query);
93
94
        $configuration = $this->createMock(Configuration::class);
95
        $configuration->method('getQuoteStrategy')->willReturn(null);
96
97
        $repository = $this->createMock(EntityRepository::class);
98
        $repository->method('find')->willReturn(null);
99
        $repository->method('findBy')->willReturn(null);
100
        $repository->method('findOneBy')->willReturn(null);
101
102
        $emMock = $this->createMock(EntityManager::class);
103
        $emMock->method('getRepository')->willReturn($repository);
104
        $emMock->method('getClassMetaData')->willReturn((object) ['name' => 'aClass']);
105
        $emMock->method('getConfiguration')->willReturn($configuration);
106
        $emMock->method('clear')->willReturn(null);
107
        $emMock->method('createQueryBuilder')->willReturn($queryBuilder);
108
        $emMock->method('persist')->willReturn(null);
109
        $emMock->method('flush')->willReturn(null);
110
111
        return $emMock;
112
    }
113
114
    public function testGetStringValue()
115
    {
116
        $this->assertNull($this->object->buildFilters());
117
        $this->assertEquals('', $this->object->getStringValue([], 'fail'));
118
        $this->assertEquals('pass', $this->object->getStringValue(['test' => 'pass'], 'test'));
119
    }
120
121
    public function testBuildExportFields()
122
    {
123
        $this->object->buildExportFields();
124
        $this->assertCount(3, $this->object->getExportFields());
125
        $this->object->addExportField('abc', 'def');
126
        $this->assertCount(4, $this->object->getExportFields());
127
    }
128
129
    public function testBuildIterator()
130
    {
131
        $this->object->buildIterator();
132
        $filters = $this->object->getIterator();
133
        $this->assertInstanceOf(\ArrayIterator::class, $filters);
134
        $first = $filters->current();
135
        $this->assertCount(1, $first);
136
        $first = $first[0];
137
        $this->assertArrayHasKey('check', $first);
138
        $this->assertEquals('true', $first['check']);
139
    }
140
}
141