Completed
Push — feature/EVO-4972-incremental-d... ( 4d7d33...3eef27 )
by Narcotic
23:11 queued 02:50
created

IncrementalDateFieldConstraintTest   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
B testIncrementalDateFieldHandling() 0 87 2
1
<?php
2
/**
3
 * test for IncrementalDateFieldConstraint
4
 */
5
6
namespace Graviton\SchemaBundle\Tests\ConstraintBuilder;
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
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
            $this->assertEquals(Response::HTTP_BAD_REQUEST, $client->getResponse()->getStatusCode());
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
        }
84
85
        // this should work (+1 sec)
86
        $object = (object) [
87
            'id' => 'dude',
88
            'mightyDate' => '1984-05-02T07:00:02+0000'
89
        ];
90
91
        $client = static::createRestClient();
92
        $client->put('/testcase/incremental-date-constraint/dude', $object);
93
        $this->assertEquals(Response::HTTP_NO_CONTENT, $client->getResponse()->getStatusCode());
94
        $this->assertNull($client->getResults());
95
96
        // should work via PATCH
97
        $patchObject = json_encode(
98
            [
99
                [
100
                    'op' => 'replace',
101
                    'path' => '/mightyDate',
102
                    'value' => '1984-05-02T07:00:03+0000'
103
                ]
104
            ]
105
        );
106
        $client = static::createRestClient();
107
        $client->request('PATCH', '/testcase/incremental-date-constraint/dude', [], [], [], $patchObject);
108
        $this->assertEquals(Response::HTTP_OK, $client->getResponse()->getStatusCode());
109
        $this->assertNull($client->getResults());
110
    }
111
}
112