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