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\MediaBundle\Tests\Document; |
15
|
|
|
|
16
|
|
|
use Doctrine\Common\Persistence\ManagerRegistry; |
17
|
|
|
use Doctrine\ODM\MongoDB\DocumentManager; |
18
|
|
|
use PHPUnit\Framework\TestCase; |
19
|
|
|
use Sonata\MediaBundle\Document\MediaManager; |
20
|
|
|
use Sonata\MediaBundle\Model\MediaInterface; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @group document |
24
|
|
|
* @group mongo |
25
|
|
|
*/ |
26
|
|
|
class MediaManagerTest extends TestCase |
27
|
|
|
{ |
28
|
|
|
/** @var MediaManager */ |
29
|
|
|
private $manager; |
30
|
|
|
|
31
|
|
|
protected function setUp(): void |
32
|
|
|
{ |
33
|
|
|
$this->manager = new MediaManager(MediaInterface::class, $this->createRegistryMock()); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function testSave(): void |
37
|
|
|
{ |
38
|
|
|
$media = new Media(); |
39
|
|
|
$this->manager->save($media, 'default', 'media.test'); |
40
|
|
|
|
41
|
|
|
$this->assertSame('default', $media->getContext()); |
42
|
|
|
$this->assertSame('media.test', $media->getProviderName()); |
43
|
|
|
|
44
|
|
|
$media = new Media(); |
45
|
|
|
$this->manager->save($media, true); |
46
|
|
|
|
47
|
|
|
$this->assertNull($media->getContext()); |
48
|
|
|
$this->assertNull($media->getProviderName()); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function testSaveException(): void |
52
|
|
|
{ |
53
|
|
|
$this->expectException(\InvalidArgumentException::class); |
54
|
|
|
|
55
|
|
|
$this->manager->save(null); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function testDeleteException(): void |
59
|
|
|
{ |
60
|
|
|
$this->expectException(\InvalidArgumentException::class); |
61
|
|
|
|
62
|
|
|
$this->manager->delete(null); |
|
|
|
|
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
protected function createRegistryMock(): ManagerRegistry |
66
|
|
|
{ |
67
|
|
|
$dm = $this->getMockBuilder(DocumentManager::class) |
68
|
|
|
->setMethods(['persist', 'flush']) |
69
|
|
|
->disableOriginalConstructor() |
70
|
|
|
->getMock(); |
71
|
|
|
$registry = $this->createMock(ManagerRegistry::class); |
72
|
|
|
|
73
|
|
|
$dm->method('persist'); |
74
|
|
|
$dm->method('flush'); |
75
|
|
|
$registry->method('getManagerForClass')->willReturn($dm); |
76
|
|
|
|
77
|
|
|
return $registry; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
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: