Test Failed
Pull Request — master (#14)
by Pavel
05:24
created

TypeTest   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
dl 0
loc 71
c 0
b 0
f 0
wmc 2
lcom 1
cbo 6
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B testValuesConvertedToAPI() 0 32 1
B testValuesConvertedFromAPI() 0 32 1
1
<?php
2
3
namespace Bankiru\Api\Doctrine\Tests;
4
5
use Bankiru\Api\Doctrine\Test\Entity\TypeEntity;
6
use ScayTrase\Api\Rpc\RpcRequestInterface;
7
8
class TypeTest extends AbstractEntityManagerTest
9
{
10
    const U_TIMESTAMP = 100500;
11
    const CUSTOM_DATETIME = '2010.10.10 00:00:00';
12
13
    public function testValuesConvertedToAPI()
14
    {
15
        $manager = $this->getManager();
16
17
        $entity    = new TypeEntity();
18
        $datetimeU = new \DateTime();
19
        $datetimeU->setTimestamp(self::U_TIMESTAMP);
20
        $entity->setDatetimeU($datetimeU);
21
22
        $datetimeC = \DateTime::createFromFormat('Y.m.d H:i:s',self::CUSTOM_DATETIME);
23
        $entity->setDatetimeC($datetimeC);
0 ignored issues
show
Security Bug introduced by
It seems like $datetimeC defined by \DateTime::createFromFor... self::CUSTOM_DATETIME) on line 22 can also be of type false; however, Bankiru\Api\Doctrine\Tes...eEntity::setDatetimeC() does only seem to accept object<DateTime>, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
24
25
        $manager->persist($entity);
26
27
        $this->getClient()->push(
28
            $this->getResponseMock(true, (object)['id' => 42]),
29
            function (RpcRequestInterface $request) {
30
                self::assertEquals('test-entity/create', $request->getMethod());
31
                self::assertEquals(
32
                    [
33
                        'datetime_u' => self::U_TIMESTAMP,
34
                        'datetime_c' => self::CUSTOM_DATETIME,
35
                    ],
36
                    $request->getParameters()
37
                );
38
39
                return true;
40
            }
41
        );
42
43
        $manager->flush();
44
    }
45
46
    public function testValuesConvertedFromAPI()
47
    {
48
        $manager = $this->getManager();
49
50
        $this->getClient()->push(
51
            $this->getResponseMock(
52
                true,
53
                (object)[
54
                    'id'       => 1,
55
                    'datetime_u' => self::U_TIMESTAMP,
56
                    'datetime_c' => self::CUSTOM_DATETIME,
57
                ]
58
            ),
59
            function (RpcRequestInterface $request) {
60
                self::assertEquals('test-entity/find', $request->getMethod());
61
                self::assertEquals(
62
                    [
63
                        'id' => 1,
64
                    ],
65
                    $request->getParameters()
66
                );
67
68
                return true;
69
            }
70
        );
71
72
        $entity = $manager->getRepository(TypeEntity::class)->find(1);
73
        self::assertNotNull($entity->getDatetimeU());
74
        self::assertNotNull($entity->getDatetimeC());
75
        self::assertEquals(self::U_TIMESTAMP, $entity->getDatetimeU()->getTimestamp());
76
        self::assertEquals(self::CUSTOM_DATETIME, $entity->getDatetimeC()->format('Y.m.d H:i:s'));
77
    }
78
}
79