Passed
Pull Request — 2.6 (#7566)
by
unknown
07:07
created

getDDC7561ConfirmationStatus()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Doctrine\Tests\ORM\Functional\Ticket;
4
5
use Doctrine\Tests\OrmFunctionalTestCase;
6
use Doctrine\DBAL\Platforms\AbstractPlatform;
7
use Doctrine\DBAL\Types\ConversionException;
8
use Doctrine\DBAL\Types\Type;
9
10
/**
11
 * @group DDC-1707
12
 */
13
class DDC7561Test extends OrmFunctionalTestCase
14
{
15
    public function setUp()
16
    {
17
        parent::setUp();
18
19
        Type::addType(
20
            'ddc_7561_withdraw_order_confirmation_status',
21
            DDC7561DDC7561ConfirmationStatusType::class
22
        );
23
24
        $this->_schemaTool->createSchema(
25
            [
26
                $this->_em->getClassMetadata(DDC7561AbstractOrder::class),
27
                $this->_em->getClassMetadata(DDC7561CardOrder::class),
28
                $this->_em->getClassMetadata(DDC7561WithdrawOrder::class),
29
            ]
30
        );
31
    }
32
33
    public function testExpectsDDC7561CardOrderFetching()
34
    {
35
        $expectedOrder = new DDC7561CardOrder(1, '1000', 'USD');
36
37
        $this->_em->persist($expectedOrder);
38
        $this->_em->flush();
39
        $this->_em->clear();
40
41
        $repository = $this->_em->getRepository(DDC7561AbstractOrder::class);
42
43
        $actualOrder = $repository->find(1);
44
45
        $this->assertEquals($expectedOrder, $actualOrder);
46
    }
47
}
48
49
/**
50
 * @Entity
51
 * @Table(name="ddc_7561_orders")
52
 * @InheritanceType("JOINED")
53
 * @DiscriminatorColumn(name="type", type="string")
54
 * @DiscriminatorMap({
55
 *     "card" = "DDC7561CardOrder",
56
 *     "withdraw" = "DDC7561WithdrawOrder"
57
 * })
58
 */
59
abstract class DDC7561AbstractOrder {
60
    /**
61
     * @Id()
62
     * @GeneratedValue()
63
     * @Column(type="integer")
64
     * @var int
65
     */
66
    private $id;
67
68
    /**
69
     * DDC7561AbstractOrder constructor.
70
     * @param int $id
71
     */
72
    public function __construct(int $id)
73
    {
74
        $this->id = $id;
75
    }
76
77
    /**
78
     * @return int
79
     */
80
    public function getId(): int
81
    {
82
        return $this->id;
83
    }
84
}
85
86
/**
87
 * @Entity
88
 * @Table(name="ddc_7561_card_orders")
89
 */
90
class DDC7561CardOrder extends DDC7561AbstractOrder {
91
    /**
92
     * @var string
93
     */
94
    private $amount;
95
96
    /**
97
     * @var string
98
     */
99
    private $currencyIsoCode;
100
101
    /**
102
     * DDC7561CardOrder constructor.
103
     * @param int $id
104
     * @param string $amount
105
     * @param string $currencyIsoCode
106
     */
107
    public function __construct(int $id, string $amount, string $currencyIsoCode)
108
    {
109
        parent::__construct($id);
110
111
        $this->amount = $amount;
112
        $this->currencyIsoCode = $currencyIsoCode;
113
    }
114
115
    /**
116
     * @return string
117
     */
118
    public function getAmount(): string
119
    {
120
        return $this->amount;
121
    }
122
123
    /**
124
     * @return string
125
     */
126
    public function getCurrencyIsoCode(): string
127
    {
128
        return $this->currencyIsoCode;
129
    }
130
}
131
132
/**
133
 * @Entity
134
 * @Table(name="ddc_7561_withdraw_orders")
135
 */
136
class DDC7561WithdrawOrder extends DDC7561AbstractOrder {
137
    /**
138
     * @Column(type="decimal", precision=10, scale=2)
139
     *
140
     * @var string
141
     */
142
    private $amount;
143
144
    /**
145
     * @Column(type="string", length=3)
146
     * @var string
147
     */
148
    private $currencyIsoCode;
149
150
    /**
151
     * @var DDC7561ConfirmationStatus
152
     * @Column(type="ddc_7561_withdraw_order_confirmation_status")
153
     */
154
    private $DDC7561ConfirmationStatus;
155
156
    /**
157
     * DDC7561WithdrawOrder constructor.
158
     * @param int $id
159
     * @param string $amount
160
     * @param string $currencyIsoCode
161
     */
162
    public function __construct(int $id, string $amount, string $currencyIsoCode)
163
    {
164
        parent::__construct($id);
165
        $this->amount = $amount;
166
        $this->currencyIsoCode = $currencyIsoCode;
167
        $this->DDC7561ConfirmationStatus = new DDC7561ConfirmationStatus('pending');
168
    }
169
170
    /**
171
     * @return string
172
     */
173
    public function getAmount(): string
174
    {
175
        return $this->amount;
176
    }
177
178
    /**
179
     * @return string
180
     */
181
    public function getCurrencyIsoCode(): string
182
    {
183
        return $this->currencyIsoCode;
184
    }
185
186
    /**
187
     * @return DDC7561ConfirmationStatus
188
     */
189
    public function getDDC7561ConfirmationStatus(): DDC7561ConfirmationStatus
190
    {
191
        return $this->DDC7561ConfirmationStatus;
192
    }
193
}
194
195
class DDC7561DDC7561ConfirmationStatusType extends Type
196
{
197
    /**
198
     * @param mixed            $value
199
     * @param AbstractPlatform $platform
200
     *
201
     * @return string
202
     *
203
     * @throws ConversionException
204
     */
205
    public function convertToDatabaseValue($value, AbstractPlatform $platform): string
206
    {
207
        if (!$value instanceof DDC7561ConfirmationStatus) {
208
            throw new ConversionException(\sprintf(
209
                'Value must be instance of "%s", instance "%s" given',
210
                DDC7561ConfirmationStatus::class,
211
                \is_object($value) ? \get_class($value) : \gettype($value)
212
            ));
213
        }
214
215
        /* @var DDC7561ConfirmationStatus $value */
216
        return $value->getValue();
217
    }
218
219
    /**
220
     * @param mixed            $value
221
     * @param AbstractPlatform $platform
222
     *
223
     * @return DDC7561ConfirmationStatus
224
     *
225
     * @throws ConversionException
226
     */
227
    public function convertToPHPValue($value, AbstractPlatform $platform): DDC7561ConfirmationStatus
228
    {
229
        try {
230
            return new DDC7561ConfirmationStatus($value);
231
        } catch (\Throwable $e) {
232
            throw new ConversionException($e->getMessage(), $e->getCode(), $e);
233
        }
234
    }
235
236
    /**
237
     * @param array            $fieldDeclaration
238
     * @param AbstractPlatform $platform
239
     *
240
     * @return string
241
     */
242
    public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform): string
243
    {
244
        return 'ddc_7561_withdraw_order_confirmation_status';
245
    }
246
247
    /**
248
     * @return string
249
     */
250
    public function getName(): string
251
    {
252
        return 'ddc_7561_withdraw_order_confirmation_status';
253
    }
254
}
255
256
class DDC7561ConfirmationStatus {
257
    /**
258
     * @var string
259
     */
260
    private $value;
261
262
    /**
263
     * DDC7561ConfirmationStatus constructor.
264
     * @param string $value
265
     */
266
    public function __construct(string $value)
267
    {
268
        if (\in_array($value, ['pending', 'approved', 'declined'])) {
269
            throw new \InvalidArgumentException();
270
        }
271
272
        $this->value = $value;
273
    }
274
275
    /**
276
     * @return string
277
     */
278
    public function getValue(): string
279
    {
280
        return $this->value;
281
    }
282
}