1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Doctrine\Tests\DBAL\Types; |
6
|
|
|
|
7
|
|
|
use Doctrine\DBAL\Types\ConversionException; |
8
|
|
|
use Doctrine\DBAL\Types\Exception\InvalidFormat; |
9
|
|
|
use Doctrine\DBAL\Types\Exception\InvalidType; |
10
|
|
|
use Doctrine\DBAL\Types\Exception\ValueNotConvertible; |
11
|
|
|
use PHPUnit\Framework\TestCase; |
12
|
|
|
use stdClass; |
13
|
|
|
use Throwable; |
14
|
|
|
use function get_class; |
15
|
|
|
use function gettype; |
16
|
|
|
use function is_object; |
17
|
|
|
use function sprintf; |
18
|
|
|
use function tmpfile; |
19
|
|
|
|
20
|
|
|
class ConversionExceptionTest extends TestCase |
21
|
|
|
{ |
22
|
|
|
public function testConversionFailedPreviousException() : void |
23
|
|
|
{ |
24
|
|
|
$previous = $this->createMock(Throwable::class); |
25
|
|
|
|
26
|
|
|
$exception = ValueNotConvertible::new('foo', 'foo', null, $previous); |
27
|
|
|
|
28
|
|
|
self::assertInstanceOf(ConversionException::class, $exception); |
29
|
|
|
self::assertSame($previous, $exception->getPrevious()); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param mixed $scalarValue |
34
|
|
|
* |
35
|
|
|
* @dataProvider scalarsProvider |
36
|
|
|
*/ |
37
|
|
|
public function testConversionFailedInvalidTypeWithScalar($scalarValue) : void |
38
|
|
|
{ |
39
|
|
|
$exception = InvalidType::new($scalarValue, 'foo', ['bar', 'baz']); |
40
|
|
|
|
41
|
|
|
$type = is_object($scalarValue) ? get_class($scalarValue) : gettype($scalarValue); |
42
|
|
|
|
43
|
|
|
self::assertInstanceOf(ConversionException::class, $exception); |
44
|
|
|
self::assertSame( |
45
|
|
|
sprintf( |
46
|
|
|
'Could not convert PHP value "%s" of type "%s" to type "foo". Expected one of the following types: bar, baz.', |
47
|
|
|
$scalarValue, |
48
|
|
|
$type |
49
|
|
|
), |
50
|
|
|
$exception->getMessage() |
51
|
|
|
); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param mixed $nonScalar |
56
|
|
|
* |
57
|
|
|
* @dataProvider nonScalarsProvider |
58
|
|
|
*/ |
59
|
|
|
public function testConversionFailedInvalidTypeWithNonScalar($nonScalar) : void |
60
|
|
|
{ |
61
|
|
|
$exception = InvalidType::new($nonScalar, 'foo', ['bar', 'baz']); |
62
|
|
|
|
63
|
|
|
$type = is_object($nonScalar) ? get_class($nonScalar) : gettype($nonScalar); |
64
|
|
|
|
65
|
|
|
self::assertInstanceOf(ConversionException::class, $exception); |
66
|
|
|
self::assertSame( |
67
|
|
|
sprintf('Could not convert PHP value of type "%s" to type "foo". Expected one of the following types: bar, baz.', $type), |
68
|
|
|
$exception->getMessage() |
69
|
|
|
); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function testConversionFailedInvalidTypePreviousException() : void |
73
|
|
|
{ |
74
|
|
|
$previous = $this->createMock(Throwable::class); |
75
|
|
|
|
76
|
|
|
$exception = InvalidType::new('foo', 'foo', ['bar', 'baz'], $previous); |
77
|
|
|
|
78
|
|
|
self::assertInstanceOf(ConversionException::class, $exception); |
79
|
|
|
self::assertSame($previous, $exception->getPrevious()); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function testConversionFailedFormatPreservesPreviousException() : void |
83
|
|
|
{ |
84
|
|
|
$previous = $this->createMock(Throwable::class); |
85
|
|
|
|
86
|
|
|
$exception = InvalidFormat::new('foo', 'bar', 'baz', $previous); |
87
|
|
|
|
88
|
|
|
self::assertInstanceOf(ConversionException::class, $exception); |
89
|
|
|
self::assertSame($previous, $exception->getPrevious()); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @return mixed[][] |
94
|
|
|
*/ |
95
|
|
|
public static function nonScalarsProvider() : iterable |
96
|
|
|
{ |
97
|
|
|
return [ |
98
|
|
|
[[]], |
99
|
|
|
[['foo']], |
100
|
|
|
[null], |
101
|
|
|
[new stdClass()], |
102
|
|
|
[tmpfile()], |
103
|
|
|
]; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @return mixed[][] |
108
|
|
|
*/ |
109
|
|
|
public static function scalarsProvider() : iterable |
110
|
|
|
{ |
111
|
|
|
return [ |
112
|
|
|
[''], |
113
|
|
|
['foo'], |
114
|
|
|
[123], |
115
|
|
|
[-123], |
116
|
|
|
[12.34], |
117
|
|
|
[true], |
118
|
|
|
[false], |
119
|
|
|
]; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|