1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Sonata Project package. |
7
|
|
|
* |
8
|
|
|
* (c) Thomas Rabaix <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Sonata\AdminBundle\Tests\Form\DataTransformer; |
15
|
|
|
|
16
|
|
|
use PHPUnit\Framework\TestCase; |
17
|
|
|
use Sonata\AdminBundle\Form\DataTransformer\ModelToIdTransformer; |
18
|
|
|
use Sonata\AdminBundle\Model\ModelManagerInterface; |
19
|
|
|
|
20
|
|
|
class ModelToIdTransformerTest extends TestCase |
21
|
|
|
{ |
22
|
|
|
private $modelManager = null; |
23
|
|
|
|
24
|
|
|
public function setUp(): void |
25
|
|
|
{ |
26
|
|
|
$this->modelManager = $this->getMockForAbstractClass(ModelManagerInterface::class); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function testReverseTransformWhenPassing0AsId(): void |
30
|
|
|
{ |
31
|
|
|
$transformer = new ModelToIdTransformer($this->modelManager, 'TEST'); |
|
|
|
|
32
|
|
|
|
33
|
|
|
$this->modelManager |
34
|
|
|
->expects($this->exactly(2)) |
35
|
|
|
->method('find') |
36
|
|
|
->willReturn(true); |
37
|
|
|
|
38
|
|
|
$this->assertFalse(\in_array(false, ['0', 0], true)); |
39
|
|
|
|
40
|
|
|
// we pass 0 as integer |
41
|
|
|
$this->assertTrue($transformer->reverseTransform(0)); |
42
|
|
|
|
43
|
|
|
// we pass 0 as string |
44
|
|
|
$this->assertTrue($transformer->reverseTransform('0')); |
45
|
|
|
|
46
|
|
|
// we pass null must return null |
47
|
|
|
$this->assertNull($transformer->reverseTransform(null)); |
48
|
|
|
|
49
|
|
|
// we pass false, must return null |
50
|
|
|
$this->assertNull($transformer->reverseTransform(false)); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @dataProvider getReverseTransformValues |
55
|
|
|
*/ |
56
|
|
|
public function testReverseTransform($value, $expected): void |
57
|
|
|
{ |
58
|
|
|
$transformer = new ModelToIdTransformer($this->modelManager, 'TEST2'); |
|
|
|
|
59
|
|
|
|
60
|
|
|
$this->modelManager->expects($this->any())->method('find'); |
61
|
|
|
|
62
|
|
|
$this->assertSame($expected, $transformer->reverseTransform($value)); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function getReverseTransformValues() |
66
|
|
|
{ |
67
|
|
|
return [ |
68
|
|
|
[null, null], |
69
|
|
|
[false, null], |
70
|
|
|
[[], null], |
71
|
|
|
['', null], |
72
|
|
|
]; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function testTransform(): void |
76
|
|
|
{ |
77
|
|
|
$this->modelManager->expects($this->once()) |
78
|
|
|
->method('getNormalizedIdentifier') |
79
|
|
|
->willReturn(123); |
80
|
|
|
|
81
|
|
|
$transformer = new ModelToIdTransformer($this->modelManager, 'TEST'); |
|
|
|
|
82
|
|
|
|
83
|
|
|
$this->assertNull($transformer->transform(null)); |
84
|
|
|
$this->assertNull($transformer->transform(false)); |
85
|
|
|
$this->assertNull($transformer->transform(0)); |
86
|
|
|
$this->assertNull($transformer->transform('0')); |
87
|
|
|
|
88
|
|
|
$this->assertSame(123, $transformer->transform(new \stdClass())); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: