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