|
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\Admin; |
|
15
|
|
|
|
|
16
|
|
|
use PHPUnit\Framework\TestCase; |
|
17
|
|
|
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface; |
|
18
|
|
|
use Sonata\AdminBundle\Model\ModelManagerInterface; |
|
19
|
|
|
use Sonata\AdminBundle\Security\Handler\SecurityHandlerInterface; |
|
20
|
|
|
|
|
21
|
|
|
class BaseAdminModelManagerTest extends TestCase |
|
22
|
|
|
{ |
|
23
|
|
|
public function testHook(): void |
|
24
|
|
|
{ |
|
25
|
|
|
$securityHandler = $this->getMockForAbstractClass(SecurityHandlerInterface::class); |
|
26
|
|
|
|
|
27
|
|
|
$modelManager = $this->getMockForAbstractClass(ModelManagerInterface::class); |
|
28
|
|
|
$modelManager->expects($this->once())->method('create'); |
|
29
|
|
|
$modelManager->expects($this->once())->method('update'); |
|
30
|
|
|
$modelManager->expects($this->once())->method('delete'); |
|
31
|
|
|
|
|
32
|
|
|
$admin = new BaseAdminModelManager_Admin('code', 'class', 'controller'); |
|
33
|
|
|
$admin->setModelManager($modelManager); |
|
34
|
|
|
$admin->setSecurityHandler($securityHandler); |
|
35
|
|
|
|
|
36
|
|
|
$t = new \stdClass(); |
|
37
|
|
|
|
|
38
|
|
|
$admin->update($t); |
|
|
|
|
|
|
39
|
|
|
$admin->create($t); |
|
|
|
|
|
|
40
|
|
|
$admin->delete($t); |
|
|
|
|
|
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function testObject(): void |
|
44
|
|
|
{ |
|
45
|
|
|
$modelManager = $this->getMockForAbstractClass(ModelManagerInterface::class); |
|
46
|
|
|
$modelManager->expects($this->once())->method('find')->willReturnCallback(static function (string $class, int $id): void { |
|
47
|
|
|
if ('class' !== $class) { |
|
48
|
|
|
throw new \RuntimeException('Invalid class argument'); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
if (10 !== $id) { |
|
52
|
|
|
throw new \RuntimeException('Invalid id argument'); |
|
53
|
|
|
} |
|
54
|
|
|
}); |
|
55
|
|
|
|
|
56
|
|
|
$admin = new BaseAdminModelManager_Admin('code', 'class', 'controller'); |
|
57
|
|
|
$admin->setModelManager($modelManager); |
|
58
|
|
|
$admin->getObject(10); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function testCreateQuery(): void |
|
62
|
|
|
{ |
|
63
|
|
|
$query = $this->createMock(ProxyQueryInterface::class); |
|
64
|
|
|
$modelManager = $this->getMockForAbstractClass(ModelManagerInterface::class); |
|
65
|
|
|
$modelManager |
|
66
|
|
|
->expects($this->once()) |
|
67
|
|
|
->method('createQuery') |
|
68
|
|
|
->with('class') |
|
69
|
|
|
->willReturn($query); |
|
70
|
|
|
|
|
71
|
|
|
$admin = new BaseAdminModelManager_Admin('code', 'class', 'controller'); |
|
72
|
|
|
$admin->setModelManager($modelManager); |
|
73
|
|
|
$admin->createQuery(); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public function testId(): void |
|
77
|
|
|
{ |
|
78
|
|
|
$modelManager = $this->createMock(ModelManagerInterface::class); |
|
79
|
|
|
$modelManager |
|
80
|
|
|
->expects($this->exactly(2)) |
|
81
|
|
|
->method('getNormalizedIdentifier') |
|
82
|
|
|
->willReturn('42'); |
|
83
|
|
|
|
|
84
|
|
|
$admin = new BaseAdminModelManager_Admin('code', 'class', 'controller'); |
|
85
|
|
|
$admin->setModelManager($modelManager); |
|
86
|
|
|
|
|
87
|
|
|
$admin->id('Entity'); |
|
|
|
|
|
|
88
|
|
|
$admin->getNormalizedIdentifier('Entity'); |
|
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: