Completed
Push — feature/EVO-7964_fundInfo-exte... ( 6e2b8f...52a026 )
by Bastian
227:47 queued 162:38
created

testSelectExclusionStrategyPrivateCreateArrayByPath()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 25
rs 8.8571
cc 1
eloc 15
nc 1
nop 0
1
<?php
2
/**
3
 * SerializerSelectExclusionStrategyTest class file
4
 */
5
6
namespace Graviton\CoreBundle\Tests\Controller;
7
8
use Graviton\RestBundle\ExclusionStrategy\SelectExclusionStrategy;
9
use Graviton\TestBundle\Test\RestTestCase;
10
use GravitonDyn\TestCaseDeepEqualNamingBundle\DataFixtures\MongoDB\LoadTestCaseDeepEqualNamingData;
11
use Symfony\Component\HttpFoundation\Response;
12
use GravitonDyn\TestCasePrimitiveArrayBundle\DataFixtures\MongoDB\LoadTestCasePrimitiveArrayData;
13
use GravitonDyn\TestCaseNullExtrefBundle\DataFixtures\MongoDB\LoadTestCaseNullExtrefData;
14
15
/**
16
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
17
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
18
 * @link     http://swisscom.ch
19
 */
20
class SerializerSelectExclusionStrategyTest extends RestTestCase
21
{
22
23
    /**
24
     * load fixtures (in this case we can reuse fixtures from other tests)
25
     *
26
     * @return void
27
     */
28
    public function setUp()
29
    {
30
        if (!class_exists(LoadTestCasePrimitiveArrayData::class)) {
31
            $this->markTestSkipped('TestCasePrimitiveArray definition is not loaded');
32
        }
33
        if (!class_exists(LoadTestCaseNullExtrefData::class)) {
34
            $this->markTestSkipped('TestCaseNullExtref definition is not loaded');
35
        }
36
37
        $this->loadFixtures(
38
            [
39
                LoadTestCasePrimitiveArrayData::class,
40
                LoadTestCaseNullExtrefData::class,
41
                LoadTestCaseDeepEqualNamingData::class
42
            ],
43
            null,
44
            'doctrine_mongodb'
45
        );
46
    }
47
48
    /**
49
     * Test testRqlSelectionOnArrays testing the correct serialization of nested arrays
50
     *
51
     * @return void
52
     */
53 View Code Duplication
    public function testRqlSelectionOnArrays()
0 ignored issues
show
Duplication introduced by
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...
54
    {
55
        $expectedResult = json_decode(
56
            file_get_contents(dirname(__FILE__).'/../resources/serializer-exclusion-array.json'),
57
            false
58
        );
59
60
        $client = static::createRestClient();
61
        $client->request(
62
            'GET',
63
            '/testcase/primitivearray/testdata?select(hash.strarray,arrayhash.intarray,arrayhash.hasharray)'
64
        );
65
        $this->assertEquals(Response::HTTP_OK, $client->getResponse()->getStatusCode());
66
        $this->assertEquals($expectedResult, $client->getResults());
67
    }
68
69
    /**
70
     * Test testRqlSelectionOnNested testing the correct serialization of deeply nested values
71
     *
72
     * @return void
73
     */
74 View Code Duplication
    public function testRqlSelectionOnNested()
0 ignored issues
show
Duplication introduced by
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...
75
    {
76
        $expectedResult = json_decode(
77
            file_get_contents(dirname(__FILE__).'/../resources/serializer-exclusion-nested.json'),
78
            false
79
        );
80
81
        $client = static::createRestClient();
82
        $client->request(
83
            'GET',
84
            '/testcase/nullextref/testdata?select(requiredExtref,requiredExtrefDeep.deep.deep,optionalExtrefDeep)'
85
        );
86
        $this->assertEquals(Response::HTTP_OK, $client->getResponse()->getStatusCode());
87
        $this->assertEquals($expectedResult, $client->getResults());
88
    }
89
90
    /**
91
     * Test testRqlSelectionOnNestedDouble testing the correct serialization of deeply nested values
92
     * The error was that if fields had the same name only first was checked and only the second if first not empty
93
     *
94
     * @return void
95
     */
96
    public function testRqlSelectionOnNestedDouble()
97
    {
98
        $expectedResult = json_decode(
99
            file_get_contents(dirname(__FILE__).'/../resources/serializer-exclusion-nested-double.json'),
100
            false
101
        );
102
103
        $client = static::createRestClient();
104
        $client->request(
105
            'GET',
106
            '/testcase/deep-naming/?select(level.levela.levela1,level.levelb.levelb1)'
107
        );
108
        $this->assertEquals(Response::HTTP_OK, $client->getResponse()->getStatusCode());
109
        $this->assertEquals($expectedResult, $client->getResults());
110
    }
111
112
    /**
113
     * Test the private select function
114
     *
115
     * @return void
116
     */
117
    public function testSelectExclusionStrategyPrivateCreateArrayByPath()
118
    {
119
        /** @var SelectExclusionStrategy $class */
120
        $class = $this->getContainer()->get('graviton.rest.serializer.exclusionstrategy.selectexclusionstrategy');
121
        $method = $this->getPrivateClassMethod(get_class($class), 'createArrayByPath');
122
123
        $mainResult = [];
124
125
        $path = 'level.levela.levela1';
126
        $arr = $method->invokeArgs($class, [$path]);
127
        $mainResult = array_merge_recursive($mainResult, $arr);
128
129
        $path = 'level.levelb.levelb1';
130
        $arr = $method->invokeArgs($class, [$path]);
131
        $mainResult = array_merge_recursive($mainResult, $arr);
132
133
        $expectedResult = [
134
            'level' => [
135
                'levela' => ['levela1' => true],
136
                'levelb' => ['levelb1' => true]
137
            ]
138
        ];
139
140
        $this->assertEquals($expectedResult, $mainResult);
141
    }
142
}
143