DDC2012ItemPerson
last analyzed

Complexity

Total Complexity 0

Size/Duplication

Total Lines 2
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 2
c 0
b 0
f 0
wmc 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Tests\ORM\Functional\Ticket;
6
7
use Doctrine\DBAL\Platforms\AbstractPlatform;
8
use Doctrine\DBAL\Types\Type;
9
use Doctrine\ORM\Annotation as ORM;
10
use Doctrine\Tests\OrmFunctionalTestCase;
11
use function explode;
12
use function get_class;
13
use function implode;
14
use function is_array;
15
use function strtolower;
16
17
/**
18
 * @group DDC-2012
19
 * @group non-cacheable
20
 */
21
class DDC2012Test extends OrmFunctionalTestCase
22
{
23
    protected function setUp() : void
24
    {
25
        parent::setUp();
26
27
        Type::addType(DDC2012TsVectorType::MYTYPE, DDC2012TsVectorType::class);
28
29
        DDC2012TsVectorType::$calls = [];
30
31
        $this->schemaTool->createSchema(
32
            [
33
                $this->em->getClassMetadata(DDC2012Item::class),
34
                $this->em->getClassMetadata(DDC2012ItemPerson::class),
35
            ]
36
        );
37
    }
38
39
    public function testIssue() : void
40
    {
41
        $item      = new DDC2012ItemPerson();
42
        $item->tsv = ['word1', 'word2', 'word3'];
43
44
        $this->em->persist($item);
45
        $this->em->flush();
46
        $this->em->clear();
47
48
        $item = $this->em->find(get_class($item), $item->id);
49
50
        self::assertArrayHasKey('convertToDatabaseValueSQL', DDC2012TsVectorType::$calls);
51
        self::assertArrayHasKey('convertToDatabaseValue', DDC2012TsVectorType::$calls);
52
        self::assertArrayHasKey('convertToPHPValue', DDC2012TsVectorType::$calls);
53
54
        self::assertCount(1, DDC2012TsVectorType::$calls['convertToDatabaseValueSQL']);
55
        self::assertCount(1, DDC2012TsVectorType::$calls['convertToDatabaseValue']);
56
        self::assertCount(1, DDC2012TsVectorType::$calls['convertToPHPValue']);
57
58
        self::assertInstanceOf(DDC2012Item::class, $item);
59
        self::assertEquals(['word1', 'word2', 'word3'], $item->tsv);
60
61
        $item->tsv = ['word1', 'word2'];
62
63
        $this->em->persist($item);
64
        $this->em->flush();
65
        $this->em->clear();
66
67
        $item = $this->em->find(get_class($item), $item->id);
68
69
        self::assertCount(2, DDC2012TsVectorType::$calls['convertToDatabaseValueSQL']);
70
        self::assertCount(2, DDC2012TsVectorType::$calls['convertToDatabaseValue']);
71
        self::assertCount(2, DDC2012TsVectorType::$calls['convertToPHPValue']);
72
73
        self::assertInstanceOf(DDC2012Item::class, $item);
74
        self::assertEquals(['word1', 'word2'], $item->tsv);
75
    }
76
}
77
78
/**
79
 * @ORM\Table(name="ddc2010_item")
80
 * @ORM\Entity
81
 * @ORM\InheritanceType("JOINED")
82
 * @ORM\DiscriminatorColumn(name="type_id", type="smallint")
83
 * @ORM\DiscriminatorMap({
84
 *      1 = DDC2012ItemPerson::class,
85
 *      2 = DDC2012Item::class
86
 * })
87
 */
88
class DDC2012Item
89
{
90
    /**
91
     * @ORM\Id
92
     * @ORM\GeneratedValue
93
     * @ORM\Column(type="integer")
94
     */
95
    public $id;
96
97
    /** @ORM\Column(name="tsv", type="tsvector", nullable=true) */
98
    public $tsv;
99
}
100
101
/**
102
 * @ORM\Table(name="ddc2010_item_person")
103
 * @ORM\Entity
104
 */
105
class DDC2012ItemPerson extends DDC2012Item
106
{
107
}
108
109
class DDC2012TsVectorType extends Type
110
{
111
    public const MYTYPE = 'tsvector';
112
113
    public static $calls = [];
114
115
    /**
116
     * {@inheritdoc}
117
     */
118
    public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform) : string
119
    {
120
        return $platform->getVarcharTypeDeclarationSQL($fieldDeclaration);
121
    }
122
123
    /**
124
     * {@inheritdoc}
125
     */
126
    public function convertToDatabaseValue($value, AbstractPlatform $platform)
127
    {
128
        if (is_array($value)) {
129
            $value = implode(' ', $value);
130
        }
131
132
        self::$calls[__FUNCTION__][] = [
133
            'value'     => $value,
134
            'platform'  => $platform,
135
        ];
136
137
        return $value;
138
    }
139
140
    /**
141
     * {@inheritdoc}
142
     */
143
    public function convertToPHPValue($value, AbstractPlatform $platform)
144
    {
145
        self::$calls[__FUNCTION__][] = [
146
            'value'     => $value,
147
            'platform'  => $platform,
148
        ];
149
150
        return explode(' ', strtolower($value));
151
    }
152
153
    /**
154
     * {@inheritdoc}
155
     */
156
    public function convertToDatabaseValueSQL($sqlExpr, AbstractPlatform $platform) : string
157
    {
158
        self::$calls[__FUNCTION__][] = [
159
            'sqlExpr'   => $sqlExpr,
160
            'platform'  => $platform,
161
        ];
162
163
        // changed to upper expression to keep the test compatible with other Databases
164
        //sprintf('to_tsvector(%s)', $sqlExpr);
165
166
        return $platform->getUpperExpression($sqlExpr);
167
    }
168
169
    /**
170
     * {@inheritdoc}
171
     */
172
    public function canRequireSQLConversion() : bool
173
    {
174
        return true;
175
    }
176
177
    /**
178
     * {@inheritdoc}
179
     */
180
    public function getName() : string
181
    {
182
        return self::MYTYPE;
183
    }
184
}
185