Completed
Push — develop ( 616447...cceb64 )
by Adrian
11s
created

IncrementalDateFieldConstraintTest   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 3
c 2
b 0
f 0
lcom 1
cbo 2
dl 0
loc 99
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B testIncrementalDateFieldHandling() 0 90 3
1
<?php
2
/**
3
 * test for IncrementalDateFieldConstraint
4
 */
5
6
namespace Graviton\SchemaBundle\Tests\Controller;
7
8
use Graviton\TestBundle\Test\RestTestCase;
9
use Symfony\Component\HttpFoundation\Response;
10
11
/**
12
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
13
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
14
 * @link     http://swisscom.ch
15
 */
16
class IncrementalDateFieldConstraintTest extends RestTestCase
17
{
18
19
    /**
20
     * test the validation of the incrementalDate constraint
21
     *
22
     * @return void
23
     */
24
    public function testIncrementalDateFieldHandling()
25
    {
26
        // create the record
27
        $object = (object) [
28
            'id' => 'dude',
29
            'mightyDate' => '1984-05-02T07:00:01+0000'
30
        ];
31
32
        $client = static::createRestClient();
33
        $client->put('/testcase/incremental-date-constraint/dude', $object);
34
        $this->assertEquals(Response::HTTP_NO_CONTENT, $client->getResponse()->getStatusCode());
35
        $this->assertNull($client->getResults());
36
37
        // foolish attempts to beat the validation
38
        $shouldNotWork = [
39
            'same' => '1984-05-02T07:00:01+0000',
40
            'timezone' => '1984-05-02T07:00:01+2000',
41
            '1sec' => '1984-05-02T07:00:00+0000',
42
            'string' => 'ss'
43
        ];
44
45
        foreach ($shouldNotWork as $date) {
46
            $object = (object) [
47
                'id' => 'dude',
48
                'mightyDate' => $date
49
            ];
50
51
            $client = static::createRestClient();
52
            $client->put('/testcase/incremental-date-constraint/dude', $object);
53
54
            $this->assertEquals(Response::HTTP_BAD_REQUEST, $client->getResponse()->getStatusCode());
55
            $this->assertEquals(
56
                $client->getResults()[0],
57
                (object) [
58
                    'propertyPath' => 'mightyDate',
59
                    'message' => 'The date must be greater than the saved date 1984-05-02T07:00:01+0000'
60
                ]
61
            );
62
63
            // same with patch, no change made, same date if first validation goes through
64
            $patchObject = json_encode(
65
                [
66
                    [
67
                        'op' => 'replace',
68
                        'path' => '/mightyDate',
69
                        'value' => $date
70
                    ]
71
                ]
72
            );
73
            $client->request('PATCH', '/testcase/incremental-date-constraint/dude', [], [], [], $patchObject);
74
75
            if ($client->getResponse()->getStatusCode() == Response::HTTP_BAD_REQUEST) {
76
                $this->assertEquals(
77
                    $client->getResults()[0],
78
                    (object) [
79
                        'propertyPath' => 'mightyDate',
80
                        'message' => 'The date must be greater than the saved date 1984-05-02T07:00:01+0000'
81
                    ]
82
                );
83
            } else {
84
                $this->assertEquals(Response::HTTP_NOT_MODIFIED, $client->getResponse()->getStatusCode());
85
            }
86
        }
87
88
        // this should work (+1 sec)
89
        $object = (object) [
90
            'id' => 'dude',
91
            'mightyDate' => '1984-05-02T07:00:02+0000'
92
        ];
93
94
        $client = static::createRestClient();
95
        $client->put('/testcase/incremental-date-constraint/dude', $object);
96
        $this->assertEquals(Response::HTTP_NO_CONTENT, $client->getResponse()->getStatusCode());
97
        $this->assertNull($client->getResults());
98
99
        // should work via PATCH
100
        $patchObject = json_encode(
101
            [
102
                [
103
                    'op' => 'replace',
104
                    'path' => '/mightyDate',
105
                    'value' => '1984-05-02T07:00:03+0000'
106
                ]
107
            ]
108
        );
109
        $client = static::createRestClient();
110
        $client->request('PATCH', '/testcase/incremental-date-constraint/dude', [], [], [], $patchObject);
111
        $this->assertEquals(Response::HTTP_OK, $client->getResponse()->getStatusCode());
112
        $this->assertNull($client->getResults());
113
    }
114
115
    /**
116
     * check correct behavior in respect to different timezones..
117
     *
118
     * @return void
119
     */
120
    public function testCorrectTimezoneHandling()
121
    {
122
        // create the record
123
        $object = (object) [
124
            'id' => 'tz',
125
            'mightyDate' => '1984-05-02T00:00:00+0000'
126
        ];
127
128
        $client = static::createRestClient();
129
        $client->put('/testcase/incremental-date-constraint/tz', $object);
130
        $this->assertEquals(Response::HTTP_NO_CONTENT, $client->getResponse()->getStatusCode());
131
        $this->assertNull($client->getResults());
132
133
        // change with value of different timezone
134
        $object = (object) [
135
            'id' => 'tz',
136
            'mightyDate' => '1984-05-02T06:00:00-7000'
137
        ];
138
139
        $client = static::createRestClient();
140
        $client->put('/testcase/incremental-date-constraint/tz', $object);
141
        $this->assertEquals(Response::HTTP_NO_CONTENT, $client->getResponse()->getStatusCode());
142
        $this->assertNull($client->getResults());
143
144
        // now, this should be denied (same time as saved)
145
        $object = (object) [
146
            'id' => 'tz',
147
            'mightyDate' => '1984-05-01T21:00:00+5000'
148
        ];
149
        $client = static::createRestClient();
150
        $client->put('/testcase/incremental-date-constraint/tz', $object);
151
        $this->assertEquals(Response::HTTP_BAD_REQUEST, $client->getResponse()->getStatusCode());
152
    }
153
}
154