|
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\Controller\Api; |
|
15
|
|
|
|
|
16
|
|
|
use FOS\RestBundle\Request\ParamFetcher; |
|
17
|
|
|
use FOS\RestBundle\View\View; |
|
18
|
|
|
use PHPUnit\Framework\TestCase; |
|
19
|
|
|
use Sonata\MediaBundle\Controller\Api\GalleryController; |
|
20
|
|
|
use Sonata\MediaBundle\Model\GalleryInterface; |
|
21
|
|
|
use Sonata\MediaBundle\Model\GalleryItem; |
|
22
|
|
|
use Sonata\MediaBundle\Model\GalleryItemInterface; |
|
23
|
|
|
use Sonata\MediaBundle\Model\GalleryManagerInterface; |
|
24
|
|
|
use Sonata\MediaBundle\Model\MediaInterface; |
|
25
|
|
|
use Sonata\MediaBundle\Model\MediaManagerInterface; |
|
26
|
|
|
use Symfony\Component\Form\FormFactoryInterface; |
|
27
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
|
28
|
|
|
|
|
29
|
|
|
class GalleryTest extends GalleryItem |
|
30
|
|
|
{ |
|
31
|
|
|
private $id; |
|
32
|
|
|
|
|
33
|
|
|
public function __construct() |
|
34
|
|
|
{ |
|
35
|
|
|
parent::__construct(); |
|
|
|
|
|
|
36
|
|
|
$this->id = random_int(0, getrandmax()); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function getId() |
|
40
|
|
|
{ |
|
41
|
|
|
return $this->id; |
|
42
|
|
|
} |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @author Hugo Briand <[email protected]> |
|
47
|
|
|
*/ |
|
48
|
|
|
class GalleryControllerTest extends TestCase |
|
|
|
|
|
|
49
|
|
|
{ |
|
50
|
|
|
public function testGetGalleriesAction(): void |
|
51
|
|
|
{ |
|
52
|
|
|
$galleryManager = $this->createMock(GalleryManagerInterface::class); |
|
53
|
|
|
$mediaManager = $this->createMock(MediaManagerInterface::class); |
|
54
|
|
|
$formFactory = $this->createMock(FormFactoryInterface::class); |
|
55
|
|
|
|
|
56
|
|
|
$galleryManager->expects($this->once())->method('getPager')->will($this->returnValue([])); |
|
57
|
|
|
|
|
58
|
|
|
$gController = new GalleryController($galleryManager, $mediaManager, $formFactory, 'test'); |
|
59
|
|
|
|
|
60
|
|
|
$paramFetcher = $this->createMock(ParamFetcher::class); |
|
61
|
|
|
$paramFetcher->expects($this->exactly(3))->method('get'); |
|
62
|
|
|
$paramFetcher |
|
63
|
|
|
->expects($this->once()) |
|
64
|
|
|
->method('all') |
|
65
|
|
|
->will($this->returnValue([ |
|
66
|
|
|
'page' => 1, |
|
67
|
|
|
'count' => 10, |
|
68
|
|
|
'orderBy' => ['id' => 'ASC'], |
|
69
|
|
|
])); |
|
70
|
|
|
|
|
71
|
|
|
$this->assertSame([], $gController->getGalleriesAction($paramFetcher)); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public function testGetGalleryAction(): void |
|
75
|
|
|
{ |
|
76
|
|
|
$galleryManager = $this->createMock(GalleryManagerInterface::class); |
|
77
|
|
|
$mediaManager = $this->createMock(MediaManagerInterface::class); |
|
78
|
|
|
$gallery = $this->createMock(GalleryInterface::class); |
|
79
|
|
|
$formFactory = $this->createMock(FormFactoryInterface::class); |
|
80
|
|
|
|
|
81
|
|
|
$galleryManager->expects($this->once())->method('findOneBy')->will($this->returnValue($gallery)); |
|
82
|
|
|
|
|
83
|
|
|
$gController = new GalleryController($galleryManager, $mediaManager, $formFactory, 'test'); |
|
84
|
|
|
|
|
85
|
|
|
$this->assertSame($gallery, $gController->getGalleryAction(1)); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
public function testGetGalleryNotFoundAction(): void |
|
89
|
|
|
{ |
|
90
|
|
|
$this->expectException(NotFoundHttpException::class); |
|
91
|
|
|
$this->expectExceptionMessage('Gallery (42) not found'); |
|
92
|
|
|
|
|
93
|
|
|
$galleryManager = $this->createMock(GalleryManagerInterface::class); |
|
94
|
|
|
$mediaManager = $this->createMock(MediaManagerInterface::class); |
|
95
|
|
|
|
|
96
|
|
|
$formFactory = $this->createMock(FormFactoryInterface::class); |
|
97
|
|
|
|
|
98
|
|
|
$galleryManager->expects($this->once())->method('findOneBy'); |
|
99
|
|
|
|
|
100
|
|
|
$gController = new GalleryController($galleryManager, $mediaManager, $formFactory, 'test'); |
|
101
|
|
|
|
|
102
|
|
|
$gController->getGalleryAction(42); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
public function testGetGalleryGalleryItemsAction(): void |
|
106
|
|
|
{ |
|
107
|
|
|
$galleryManager = $this->createMock(GalleryManagerInterface::class); |
|
108
|
|
|
$galleryItem = $this->createMock(GalleryItemInterface::class); |
|
109
|
|
|
$gallery = $this->createMock(GalleryInterface::class); |
|
110
|
|
|
$formFactory = $this->createMock(FormFactoryInterface::class); |
|
111
|
|
|
|
|
112
|
|
|
$gallery->expects($this->once())->method('getGalleryItems')->will($this->returnValue([$galleryItem])); |
|
113
|
|
|
|
|
114
|
|
|
$galleryManager->expects($this->once())->method('findOneBy')->will($this->returnValue($gallery)); |
|
115
|
|
|
|
|
116
|
|
|
$mediaManager = $this->createMock(MediaManagerInterface::class); |
|
117
|
|
|
|
|
118
|
|
|
$gController = new GalleryController($galleryManager, $mediaManager, $formFactory, 'test'); |
|
119
|
|
|
|
|
120
|
|
|
$this->assertSame([$galleryItem], $gController->getGalleryGalleryItemAction(1)); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
public function testGetGalleryMediaAction(): void |
|
124
|
|
|
{ |
|
125
|
|
|
$media = $this->createMock(MediaInterface::class); |
|
126
|
|
|
$formFactory = $this->createMock(FormFactoryInterface::class); |
|
127
|
|
|
$galleryItem = $this->createMock(GalleryItemInterface::class); |
|
128
|
|
|
$gallery = $this->createMock(GalleryInterface::class); |
|
129
|
|
|
$galleryManager = $this->createMock(GalleryManagerInterface::class); |
|
130
|
|
|
$mediaManager = $this->createMock(MediaManagerInterface::class); |
|
131
|
|
|
|
|
132
|
|
|
$galleryItem->expects($this->once())->method('getMedia')->will($this->returnValue($media)); |
|
133
|
|
|
$gallery->expects($this->once())->method('getGalleryItems')->will($this->returnValue([$galleryItem])); |
|
134
|
|
|
$galleryManager->expects($this->once())->method('findOneBy')->will($this->returnValue($gallery)); |
|
135
|
|
|
|
|
136
|
|
|
$gController = new GalleryController($galleryManager, $mediaManager, $formFactory, 'test'); |
|
137
|
|
|
|
|
138
|
|
|
$this->assertSame([$media], $gController->getGalleryMediasAction(1)); |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: