Completed
Push — master ( 4c59ec...77a338 )
by Marco
10:15
created

DDC5684ObjectIdType::convertToDatabaseValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 2
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
        if (DBALTypes\Type::hasType(DDC5684ObjectIdType::CLASSNAME)) {
22
            DBALTypes\Type::overrideType(DDC5684ObjectIdType::CLASSNAME, DDC5684ObjectIdType::CLASSNAME);
23
        } else {
24
            DBALTypes\Type::addType(DDC5684ObjectIdType::CLASSNAME, DDC5684ObjectIdType::CLASSNAME);
25
        }
26
27
        $this->_schemaTool->createSchema([$this->_em->getClassMetadata(DDC5684Object::CLASSNAME)]);
28
    }
29
30
    protected function tearDown()
31
    {
32
        $this->_schemaTool->dropSchema([$this->_em->getClassMetadata(DDC5684Object::CLASSNAME)]);
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::CLASSNAME, $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::CLASSNAME, new DDC5684ObjectId($rawId));
55
56
        $this->assertInstanceOf(DDC5684ObjectId::CLASSNAME, $object->id);
57
        $this->assertEquals($rawId, $object->id->value);
58
    }
59
}
60
61
class DDC5684ObjectIdType extends DBALTypes\IntegerType
62
{
63
    const CLASSNAME = __CLASS__;
64
65
    public function convertToPHPValue($value, AbstractPlatform $platform)
66
    {
67
        return new DDC5684ObjectId($value);
68
    }
69
70
    public function convertToDatabaseValue($value, AbstractPlatform $platform)
71
    {
72
        return $value->value;
73
    }
74
75
    public function getName()
76
    {
77
        return self::CLASSNAME;
78
    }
79
80
    public function requiresSQLCommentHint(AbstractPlatform $platform)
81
    {
82
        return true;
83
    }
84
}
85
86
class DDC5684ObjectId
87
{
88
    const CLASSNAME = __CLASS__;
89
90
    public $value;
91
92
    public function __construct($value)
93
    {
94
        $this->value = $value;
95
    }
96
97
    public function __toString()
98
    {
99
        return (string) $this->value;
100
    }
101
}
102
103
/**
104
 * @Entity
105
 * @Table(name="ticket_5684_objects")
106
 */
107
class DDC5684Object
108
{
109
    const CLASSNAME = __CLASS__;
110
111
    /**
112
     * @Id
113
     * @Column(type=Doctrine\Tests\ORM\Functional\Ticket\DDC5684ObjectIdType::class)
114
     * @GeneratedValue(strategy="AUTO")
115
     */
116
    public $id;
117
}
118