Failed Conditions
Push — master ( 6744b4...2b8acb )
by Marco
60:45 queued 60:36
created

Tests/ORM/Functional/Ticket/DDC5684Test.php (1 issue)

Upgrade to new PHP Analysis Engine

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 Doctrine\Tests\ORM\Functional\Ticket;
4
5
use Doctrine\DBAL\Platforms\AbstractPlatform;
6
use Doctrine\DBAL\Types as DBALTypes;
7
8
/**
9
 * This test verifies that custom post-insert identifiers respect type conversion semantics.
10
 * The generated identifier must be converted via DBAL types before populating the entity
11
 * identifier field.
12
 *
13
 * @group 5935 5684 6020 6152
14
 */
15
class DDC5684Test extends \Doctrine\Tests\OrmFunctionalTestCase
16
{
17
    protected function setUp()
18
    {
19
        parent::setUp();
20
21 View Code Duplication
        if (DBALTypes\Type::hasType(DDC5684ObjectIdType::class)) {
22
            DBALTypes\Type::overrideType(DDC5684ObjectIdType::class, DDC5684ObjectIdType::class);
23
        } else {
24
            DBALTypes\Type::addType(DDC5684ObjectIdType::class, DDC5684ObjectIdType::class);
25
        }
26
27
        $this->_schemaTool->createSchema([$this->_em->getClassMetadata(DDC5684Object::class)]);
28
    }
29
30
    protected function tearDown()
31
    {
32
        $this->_schemaTool->dropSchema([$this->_em->getClassMetadata(DDC5684Object::class)]);
33
34
        parent::tearDown();
35
    }
36
37
    public function testAutoIncrementIdWithCustomType()
38
    {
39
        $object = new DDC5684Object();
40
        $this->_em->persist($object);
41
        $this->_em->flush();
42
43
        $this->assertInstanceOf(DDC5684ObjectId::class, $object->id);
44
    }
45
46
    public function testFetchObjectWithAutoIncrementedCustomType()
47
    {
48
        $object = new DDC5684Object();
49
        $this->_em->persist($object);
50
        $this->_em->flush();
51
        $this->_em->clear();
52
53
        $rawId = $object->id->value;
54
        $object = $this->_em->find(DDC5684Object::class, new DDC5684ObjectId($rawId));
55
56
        $this->assertInstanceOf(DDC5684ObjectId::class, $object->id);
57
        $this->assertEquals($rawId, $object->id->value);
58
    }
59
}
60
61
class DDC5684ObjectIdType extends DBALTypes\IntegerType
62
{
63
    public function convertToPHPValue($value, AbstractPlatform $platform)
64
    {
65
        return new DDC5684ObjectId($value);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \Doctrine\Tes...DC5684ObjectId($value); (Doctrine\Tests\ORM\Funct...\Ticket\DDC5684ObjectId) is incompatible with the return type of the parent method Doctrine\DBAL\Types\IntegerType::convertToPHPValue of type integer.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
66
    }
67
68
    public function convertToDatabaseValue($value, AbstractPlatform $platform)
69
    {
70
        return $value->value;
71
    }
72
73
    public function getName()
74
    {
75
        return self::class;
76
    }
77
78
    public function requiresSQLCommentHint(AbstractPlatform $platform)
79
    {
80
        return true;
81
    }
82
}
83
84
class DDC5684ObjectId
85
{
86
    public $value;
87
88
    public function __construct($value)
89
    {
90
        $this->value = $value;
91
    }
92
93
    public function __toString()
94
    {
95
        return (string) $this->value;
96
    }
97
}
98
99
/**
100
 * @Entity
101
 * @Table(name="ticket_5684_objects")
102
 */
103
class DDC5684Object
104
{
105
    /**
106
     * @Id
107
     * @Column(type=Doctrine\Tests\ORM\Functional\Ticket\DDC5684ObjectIdType::class)
108
     * @GeneratedValue(strategy="AUTO")
109
     */
110
    public $id;
111
}
112