Completed
Push — 3.x ( e947b5...4733f4 )
by Grégoire
04:57
created

AdminTest::testGetBatchActions()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 58
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 58
rs 9.639
c 0
b 0
f 0
cc 3
eloc 41
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/*
4
 * This file is part of the Sonata Project package.
5
 *
6
 * (c) Thomas Rabaix <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sonata\AdminBundle\Tests\Admin;
13
14
use Sonata\AdminBundle\Admin\AbstractAdmin;
15
use Sonata\AdminBundle\Admin\AdminInterface;
16
use Sonata\AdminBundle\Admin\Pool;
17
use Sonata\AdminBundle\Route\DefaultRouteGenerator;
18
use Sonata\AdminBundle\Route\RoutesCache;
19
use Sonata\AdminBundle\Tests\Fixtures\Admin\CommentAdmin;
20
use Sonata\AdminBundle\Tests\Fixtures\Admin\CommentWithCustomRouteAdmin;
21
use Sonata\AdminBundle\Tests\Fixtures\Admin\FieldDescription;
22
use Sonata\AdminBundle\Tests\Fixtures\Admin\FilteredAdmin;
23
use Sonata\AdminBundle\Tests\Fixtures\Admin\ModelAdmin;
24
use Sonata\AdminBundle\Tests\Fixtures\Admin\PostAdmin;
25
use Sonata\AdminBundle\Tests\Fixtures\Admin\PostWithCustomRouteAdmin;
26
use Sonata\AdminBundle\Tests\Fixtures\Bundle\Entity\Comment;
27
use Sonata\AdminBundle\Tests\Fixtures\Bundle\Entity\Post;
28
use Sonata\AdminBundle\Tests\Fixtures\Bundle\Entity\Tag;
29
use Sonata\AdminBundle\Tests\Fixtures\Entity\FooToString;
30
use Sonata\AdminBundle\Tests\Fixtures\Entity\FooToStringNull;
31
use Sonata\AdminBundle\Tests\Helpers\PHPUnit_Framework_TestCase;
32
use Sonata\CoreBundle\Model\Adapter\AdapterInterface;
33
use Symfony\Component\DependencyInjection\Container;
34
use Symfony\Component\HttpFoundation\Request;
35
use Symfony\Component\PropertyAccess\PropertyAccess;
36
37
class AdminTest extends PHPUnit_Framework_TestCase
38
{
39
    protected $cacheTempFolder;
40
41
    public function setUp()
42
    {
43
        $this->cacheTempFolder = sys_get_temp_dir().'/sonata_test_route';
44
45
        exec('rm -rf '.$this->cacheTempFolder);
46
    }
47
48
    /**
49
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::__construct
50
     */
51
    public function testConstructor()
52
    {
53
        $class = 'Application\Sonata\NewsBundle\Entity\Post';
54
        $baseControllerName = 'SonataNewsBundle:PostAdmin';
55
56
        $admin = new PostAdmin('sonata.post.admin.post', $class, $baseControllerName);
57
        $this->assertInstanceOf('Sonata\AdminBundle\Admin\AbstractAdmin', $admin);
58
        $this->assertSame($class, $admin->getClass());
59
        $this->assertSame($baseControllerName, $admin->getBaseControllerName());
60
    }
61
62
    public function testGetClass()
63
    {
64
        $class = 'Sonata\AdminBundle\Tests\Fixtures\Bundle\Entity\Post';
65
        $baseControllerName = 'SonataNewsBundle:PostAdmin';
66
67
        $admin = new PostAdmin('sonata.post.admin.post', $class, $baseControllerName);
68
69
        $admin->setSubject(new \Sonata\AdminBundle\Tests\Fixtures\Bundle\Entity\BlogPost());
70
        $this->assertSame(
71
            'Sonata\AdminBundle\Tests\Fixtures\Bundle\Entity\BlogPost',
72
            $admin->getClass()
73
        );
74
75
        $admin->setSubClasses(array('foo'));
76
        $this->assertSame(
77
            'Sonata\AdminBundle\Tests\Fixtures\Bundle\Entity\BlogPost',
78
            $admin->getClass()
79
        );
80
81
        $admin->setSubject(null);
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a object.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
82
        $admin->setSubClasses(array());
83
        $this->assertSame($class, $admin->getClass());
84
85
        $admin->setSubClasses(array('foo' => 'bar'));
86
        $admin->setRequest(new Request(array('subclass' => 'foo')));
87
        $this->assertSame('bar', $admin->getClass());
88
    }
89
90
    /**
91
     * @expectedException \RuntimeException
92
     * @expectedExceptionMessage Feature not implemented: an embedded admin cannot have subclass
93
     */
94
    public function testGetClassException()
95
    {
96
        $class = 'Application\Sonata\NewsBundle\Entity\Post';
97
        $baseControllerName = 'SonataNewsBundle:PostAdmin';
98
99
        $admin = new PostAdmin('sonata.post.admin.post', $class, $baseControllerName);
100
        $admin->setParentFieldDescription(new FieldDescription());
101
        $admin->setSubClasses(array('foo' => 'bar'));
102
        $admin->setRequest(new Request(array('subclass' => 'foo')));
103
        $admin->getClass();
104
    }
105
106
    public function testCheckAccessThrowsExceptionOnMadeUpAction()
107
    {
108
        $admin = new PostAdmin(
109
            'sonata.post.admin.post',
110
            'Application\Sonata\NewsBundle\Entity\Post',
111
            'SonataNewsBundle:PostAdmin'
112
        );
113
        $this->setExpectedException(
114
            '\InvalidArgumentException',
115
            'Action "made-up" could not be found'
116
        );
117
        $admin->checkAccess('made-up');
118
    }
119
120
    public function testCheckAccessThrowsAccessDeniedException()
121
    {
122
        $admin = new PostAdmin(
123
            'sonata.post.admin.post',
124
            'Application\Sonata\NewsBundle\Entity\Post',
125
            'SonataNewsBundle:PostAdmin'
126
        );
127
        $securityHandler = $this->prophesize(
128
            'Sonata\AdminBundle\Security\Handler\SecurityHandlerInterface'
129
        );
130
        $securityHandler->isGranted($admin, 'CUSTOM_ROLE', $admin)->willReturn(true);
131
        $securityHandler->isGranted($admin, 'EXTRA_CUSTOM_ROLE', $admin)->willReturn(false);
132
        $customExtension = $this->prophesize(
133
            'Sonata\AdminBundle\Admin\AbstractAdminExtension'
134
        );
135
        $customExtension->getAccessMapping($admin)->willReturn(
136
            array('custom_action' => array('CUSTOM_ROLE', 'EXTRA_CUSTOM_ROLE'))
137
        );
138
        $admin->addExtension($customExtension->reveal());
139
        $admin->setSecurityHandler($securityHandler->reveal());
140
        $this->setExpectedException(
141
            'Symfony\Component\Security\Core\Exception\AccessDeniedException',
142
            'Access Denied to the action custom_action and role EXTRA_CUSTOM_ROLE'
143
        );
144
        $admin->checkAccess('custom_action');
145
    }
146
147
    public function testHasAccessOnMadeUpAction()
148
    {
149
        $admin = new PostAdmin(
150
            'sonata.post.admin.post',
151
            'Application\Sonata\NewsBundle\Entity\Post',
152
            'SonataNewsBundle:PostAdmin'
153
        );
154
155
        $this->assertFalse($admin->hasAccess('made-up'));
156
    }
157
158
    public function testHasAccess()
159
    {
160
        $admin = new PostAdmin(
161
            'sonata.post.admin.post',
162
            'Application\Sonata\NewsBundle\Entity\Post',
163
            'SonataNewsBundle:PostAdmin'
164
        );
165
        $securityHandler = $this->prophesize(
166
            'Sonata\AdminBundle\Security\Handler\SecurityHandlerInterface'
167
        );
168
        $securityHandler->isGranted($admin, 'CUSTOM_ROLE', $admin)->willReturn(true);
169
        $securityHandler->isGranted($admin, 'EXTRA_CUSTOM_ROLE', $admin)->willReturn(false);
170
        $customExtension = $this->prophesize(
171
            'Sonata\AdminBundle\Admin\AbstractAdminExtension'
172
        );
173
        $customExtension->getAccessMapping($admin)->willReturn(
174
            array('custom_action' => array('CUSTOM_ROLE', 'EXTRA_CUSTOM_ROLE'))
175
        );
176
        $admin->addExtension($customExtension->reveal());
177
        $admin->setSecurityHandler($securityHandler->reveal());
178
179
        $this->assertFalse($admin->hasAccess('custom_action'));
180
    }
181
182
    public function testHasAccessAllowsAccess()
183
    {
184
        $admin = new PostAdmin(
185
            'sonata.post.admin.post',
186
            'Application\Sonata\NewsBundle\Entity\Post',
187
            'SonataNewsBundle:PostAdmin'
188
        );
189
        $securityHandler = $this->prophesize(
190
            'Sonata\AdminBundle\Security\Handler\SecurityHandlerInterface'
191
        );
192
        $securityHandler->isGranted($admin, 'CUSTOM_ROLE', $admin)->willReturn(true);
193
        $securityHandler->isGranted($admin, 'EXTRA_CUSTOM_ROLE', $admin)->willReturn(true);
194
        $customExtension = $this->prophesize(
195
            'Sonata\AdminBundle\Admin\AbstractAdminExtension'
196
        );
197
        $customExtension->getAccessMapping($admin)->willReturn(
198
            array('custom_action' => array('CUSTOM_ROLE', 'EXTRA_CUSTOM_ROLE'))
199
        );
200
        $admin->addExtension($customExtension->reveal());
201
        $admin->setSecurityHandler($securityHandler->reveal());
202
203
        $this->assertTrue($admin->hasAccess('custom_action'));
204
    }
205
206
    public function testHasAccessAllowsAccessEditAction()
207
    {
208
        $admin = new PostAdmin(
209
            'sonata.post.admin.post',
210
            'Application\Sonata\NewsBundle\Entity\Post',
211
            'SonataNewsBundle:PostAdmin'
212
        );
213
        $securityHandler = $this->prophesize(
214
            'Sonata\AdminBundle\Security\Handler\SecurityHandlerInterface'
215
        );
216
        $securityHandler->isGranted($admin, 'EDIT_ROLE', $admin)->willReturn(true);
217
        $customExtension = $this->prophesize(
218
            'Sonata\AdminBundle\Admin\AbstractAdminExtension'
219
        );
220
        $customExtension->getAccessMapping($admin)->willReturn(
221
            array('edit_action' => array('EDIT_ROLE'))
222
        );
223
        $admin->addExtension($customExtension->reveal());
224
        $admin->setSecurityHandler($securityHandler->reveal());
225
226
        $this->assertTrue($admin->hasAccess('edit_action'));
227
    }
228
229
    /**
230
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::hasChild
231
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::addChild
232
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::getChild
233
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::isChild
234
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::hasChildren
235
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::getChildren
236
     */
237
    public function testChildren()
238
    {
239
        $postAdmin = new PostAdmin('sonata.post.admin.post', 'Application\Sonata\NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
240
        $this->assertFalse($postAdmin->hasChildren());
241
        $this->assertFalse($postAdmin->hasChild('comment'));
242
243
        $commentAdmin = new CommentAdmin('sonata.post.admin.comment', 'Application\Sonata\NewsBundle\Entity\Comment', 'SonataNewsBundle:CommentAdmin');
244
        $postAdmin->addChild($commentAdmin);
245
        $this->assertTrue($postAdmin->hasChildren());
246
        $this->assertTrue($postAdmin->hasChild('sonata.post.admin.comment'));
247
248
        $this->assertSame('sonata.post.admin.comment', $postAdmin->getChild('sonata.post.admin.comment')->getCode());
249
        $this->assertSame('sonata.post.admin.post|sonata.post.admin.comment', $postAdmin->getChild('sonata.post.admin.comment')->getBaseCodeRoute());
250
        $this->assertSame($postAdmin, $postAdmin->getChild('sonata.post.admin.comment')->getParent());
251
252
        $this->assertFalse($postAdmin->isChild());
253
        $this->assertTrue($commentAdmin->isChild());
254
255
        $this->assertSame(array('sonata.post.admin.comment' => $commentAdmin), $postAdmin->getChildren());
256
    }
257
258
    /**
259
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::configure
260
     */
261
    public function testConfigure()
262
    {
263
        $admin = new PostAdmin('sonata.post.admin.post', 'Application\Sonata\NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
264
        $this->assertNotNull($admin->getUniqid());
265
266
        $admin->initialize();
267
        $this->assertNotNull($admin->getUniqid());
268
        $this->assertSame('Post', $admin->getClassnameLabel());
269
270
        $admin = new CommentAdmin('sonata.post.admin.comment', 'Application\Sonata\NewsBundle\Entity\Comment', 'SonataNewsBundle:CommentAdmin');
271
        $admin->setClassnameLabel('postcomment');
272
273
        $admin->initialize();
274
        $this->assertSame('postcomment', $admin->getClassnameLabel());
275
    }
276
277
    public function testConfigureWithValidParentAssociationMapping()
278
    {
279
        $admin = new PostAdmin('sonata.post.admin.post', 'Application\Sonata\NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
280
        $admin->setParentAssociationMapping('Category');
281
282
        $admin->initialize();
283
        $this->assertSame('Category', $admin->getParentAssociationMapping());
284
    }
285
286
    public function provideGetBaseRoutePattern()
287
    {
288
        return array(
289
            array(
290
                'Application\Sonata\NewsBundle\Entity\Post',
291
                '/sonata/news/post',
292
            ),
293
            array(
294
                'Application\Sonata\NewsBundle\Document\Post',
295
                '/sonata/news/post',
296
            ),
297
            array(
298
                'MyApplication\MyBundle\Entity\Post',
299
                '/myapplication/my/post',
300
            ),
301
            array(
302
                'MyApplication\MyBundle\Entity\Post\Category',
303
                '/myapplication/my/post-category',
304
            ),
305
            array(
306
                'MyApplication\MyBundle\Entity\Product\Category',
307
                '/myapplication/my/product-category',
308
            ),
309
            array(
310
                'MyApplication\MyBundle\Entity\Other\Product\Category',
311
                '/myapplication/my/other-product-category',
312
            ),
313
            array(
314
                'Symfony\Cmf\Bundle\FooBundle\Document\Menu',
315
                '/cmf/foo/menu',
316
            ),
317
            array(
318
                'Symfony\Cmf\Bundle\FooBundle\Doctrine\Phpcr\Menu',
319
                '/cmf/foo/menu',
320
            ),
321
            array(
322
                'Symfony\Bundle\BarBarBundle\Doctrine\Phpcr\Menu',
323
                '/symfony/barbar/menu',
324
            ),
325
            array(
326
                'Symfony\Bundle\BarBarBundle\Doctrine\Phpcr\Menu\Item',
327
                '/symfony/barbar/menu-item',
328
            ),
329
            array(
330
                'Symfony\Cmf\Bundle\FooBundle\Doctrine\Orm\Menu',
331
                '/cmf/foo/menu',
332
            ),
333
            array(
334
                'Symfony\Cmf\Bundle\FooBundle\Doctrine\MongoDB\Menu',
335
                '/cmf/foo/menu',
336
            ),
337
            array(
338
                'Symfony\Cmf\Bundle\FooBundle\Doctrine\CouchDB\Menu',
339
                '/cmf/foo/menu',
340
            ),
341
            array(
342
                'AppBundle\Entity\User',
343
                '/app/user',
344
            ),
345
            array(
346
                'App\Entity\User',
347
                '/app/user',
348
            ),
349
        );
350
    }
351
352
    /**
353
     * @dataProvider provideGetBaseRoutePattern
354
     */
355
    public function testGetBaseRoutePattern($objFqn, $expected)
356
    {
357
        $admin = new PostAdmin('sonata.post.admin.post', $objFqn, 'SonataNewsBundle:PostAdmin');
358
        $this->assertSame($expected, $admin->getBaseRoutePattern());
359
    }
360
361
    /**
362
     * @dataProvider provideGetBaseRoutePattern
363
     */
364
    public function testGetBaseRoutePatternWithChildAdmin($objFqn, $expected)
365
    {
366
        $postAdmin = new PostAdmin('sonata.post.admin.post', $objFqn, 'SonataNewsBundle:PostAdmin');
367
        $commentAdmin = new CommentAdmin('sonata.post.admin.comment', 'Application\Sonata\NewsBundle\Entity\Comment', 'SonataNewsBundle:CommentAdmin');
368
        $commentAdmin->setParent($postAdmin);
369
370
        $this->assertSame($expected.'/{id}/comment', $commentAdmin->getBaseRoutePattern());
371
    }
372
373
    public function testGetBaseRoutePatternWithSpecifedPattern()
374
    {
375
        $postAdmin = new PostWithCustomRouteAdmin('sonata.post.admin.post_with_custom_route', 'Application\Sonata\NewsBundle\Entity\Post', 'SonataNewsBundle:PostWithCustomRouteAdmin');
376
377
        $this->assertSame('/post-custom', $postAdmin->getBaseRoutePattern());
378
    }
379
380
    public function testGetBaseRoutePatternWithChildAdminAndWithSpecifedPattern()
381
    {
382
        $postAdmin = new PostAdmin('sonata.post.admin.post', 'Application\Sonata\NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
383
        $commentAdmin = new CommentWithCustomRouteAdmin('sonata.post.admin.comment_with_custom_route', 'Application\Sonata\NewsBundle\Entity\Comment', 'SonataNewsBundle:CommentWithCustomRouteAdmin');
384
        $commentAdmin->setParent($postAdmin);
385
386
        $this->assertSame('/sonata/news/post/{id}/comment-custom', $commentAdmin->getBaseRoutePattern());
387
    }
388
389
    /**
390
     * @expectedException \RuntimeException
391
     */
392
    public function testGetBaseRoutePatternWithUnreconizedClassname()
393
    {
394
        $admin = new PostAdmin('sonata.post.admin.post', 'News\Thing\Post', 'SonataNewsBundle:PostAdmin');
395
        $admin->getBaseRoutePattern();
396
    }
397
398
    public function provideGetBaseRouteName()
399
    {
400
        return array(
401
            array(
402
                'Application\Sonata\NewsBundle\Entity\Post',
403
                'admin_sonata_news_post',
404
            ),
405
            array(
406
                'Application\Sonata\NewsBundle\Document\Post',
407
                'admin_sonata_news_post',
408
            ),
409
            array(
410
                'MyApplication\MyBundle\Entity\Post',
411
                'admin_myapplication_my_post',
412
            ),
413
            array(
414
                'MyApplication\MyBundle\Entity\Post\Category',
415
                'admin_myapplication_my_post_category',
416
            ),
417
            array(
418
                'MyApplication\MyBundle\Entity\Product\Category',
419
                'admin_myapplication_my_product_category',
420
            ),
421
            array(
422
                'MyApplication\MyBundle\Entity\Other\Product\Category',
423
                'admin_myapplication_my_other_product_category',
424
            ),
425
            array(
426
                'Symfony\Cmf\Bundle\FooBundle\Document\Menu',
427
                'admin_cmf_foo_menu',
428
            ),
429
            array(
430
                'Symfony\Cmf\Bundle\FooBundle\Doctrine\Phpcr\Menu',
431
                'admin_cmf_foo_menu',
432
            ),
433
            array(
434
                'Symfony\Bundle\BarBarBundle\Doctrine\Phpcr\Menu',
435
                'admin_symfony_barbar_menu',
436
            ),
437
            array(
438
                'Symfony\Bundle\BarBarBundle\Doctrine\Phpcr\Menu\Item',
439
                'admin_symfony_barbar_menu_item',
440
            ),
441
            array(
442
                'Symfony\Cmf\Bundle\FooBundle\Doctrine\Orm\Menu',
443
                'admin_cmf_foo_menu',
444
            ),
445
            array(
446
                'Symfony\Cmf\Bundle\FooBundle\Doctrine\MongoDB\Menu',
447
                'admin_cmf_foo_menu',
448
            ),
449
            array(
450
                'Symfony\Cmf\Bundle\FooBundle\Doctrine\CouchDB\Menu',
451
                'admin_cmf_foo_menu',
452
            ),
453
            array(
454
                'AppBundle\Entity\User',
455
                'admin_app_user',
456
            ),
457
            array(
458
                'App\Entity\User',
459
                'admin_app_user',
460
            ),
461
        );
462
    }
463
464
    /**
465
     * @dataProvider provideGetBaseRouteName
466
     */
467
    public function testGetBaseRouteName($objFqn, $expected)
468
    {
469
        $admin = new PostAdmin('sonata.post.admin.post', $objFqn, 'SonataNewsBundle:PostAdmin');
470
471
        $this->assertSame($expected, $admin->getBaseRouteName());
472
    }
473
474
    /**
475
     * @dataProvider provideGetBaseRouteName
476
     */
477
    public function testGetBaseRouteNameWithChildAdmin($objFqn, $expected)
478
    {
479
        $routeGenerator = new DefaultRouteGenerator(
480
            $this->createMock('Symfony\Component\Routing\RouterInterface'),
481
            new RoutesCache($this->cacheTempFolder, true)
482
        );
483
484
        $container = new Container();
485
        $pool = new Pool($container, 'Sonata Admin', '/path/to/pic.png');
486
487
        $pathInfo = new \Sonata\AdminBundle\Route\PathInfoBuilder($this->createMock('Sonata\AdminBundle\Model\AuditManagerInterface'));
488
        $postAdmin = new PostAdmin('sonata.post.admin.post', $objFqn, 'SonataNewsBundle:PostAdmin');
489
        $container->set('sonata.post.admin.post', $postAdmin);
490
        $postAdmin->setConfigurationPool($pool);
491
        $postAdmin->setRouteBuilder($pathInfo);
492
        $postAdmin->setRouteGenerator($routeGenerator);
493
        $postAdmin->initialize();
494
495
        $commentAdmin = new CommentAdmin('sonata.post.admin.comment', 'Application\Sonata\NewsBundle\Entity\Comment', 'SonataNewsBundle:CommentAdmin');
496
        $container->set('sonata.post.admin.comment', $commentAdmin);
497
        $commentAdmin->setConfigurationPool($pool);
498
        $commentAdmin->setRouteBuilder($pathInfo);
499
        $commentAdmin->setRouteGenerator($routeGenerator);
500
        $commentAdmin->initialize();
501
502
        $postAdmin->addChild($commentAdmin);
503
        $pool->setAdminServiceIds(array('sonata.post.admin.post', 'sonata.post.admin.comment'));
504
505
        $this->assertSame($expected.'_comment', $commentAdmin->getBaseRouteName());
506
507
        $this->assertTrue($postAdmin->hasRoute('show'));
508
        $this->assertTrue($postAdmin->hasRoute('sonata.post.admin.post.show'));
509
        $this->assertTrue($postAdmin->hasRoute('sonata.post.admin.post|sonata.post.admin.comment.show'));
510
        $this->assertTrue($postAdmin->hasRoute('sonata.post.admin.comment.list'));
511
        $this->assertFalse($postAdmin->hasRoute('sonata.post.admin.post|sonata.post.admin.comment.edit'));
512
        $this->assertFalse($commentAdmin->hasRoute('edit'));
513
514
        /*
515
         * Test the route name from request
516
         */
517
        $postListRequest = new Request(
518
            array(),
519
            array(),
520
            array(
521
                '_route' => $postAdmin->getBaseRouteName().'_list',
522
            )
523
        );
524
525
        $postAdmin->setRequest($postListRequest);
526
        $commentAdmin->setRequest($postListRequest);
527
528
        $this->assertTrue($postAdmin->isCurrentRoute('list'));
529
        $this->assertFalse($postAdmin->isCurrentRoute('create'));
530
        $this->assertFalse($commentAdmin->isCurrentRoute('list'));
531
        $this->assertTrue($commentAdmin->isCurrentRoute('list', 'sonata.post.admin.post'));
532
        $this->assertFalse($commentAdmin->isCurrentRoute('edit', 'sonata.post.admin.post'));
533
    }
534
535
    /**
536
     * @expectedException \RuntimeException
537
     */
538
    public function testGetBaseRouteNameWithUnreconizedClassname()
539
    {
540
        $admin = new PostAdmin('sonata.post.admin.post', 'News\Thing\Post', 'SonataNewsBundle:PostAdmin');
541
        $admin->getBaseRouteName();
542
    }
543
544
    public function testGetBaseRouteNameWithSpecifiedName()
545
    {
546
        $postAdmin = new PostWithCustomRouteAdmin('sonata.post.admin.post_with_custom_route', 'Application\Sonata\NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
547
548
        $this->assertSame('post_custom', $postAdmin->getBaseRouteName());
549
    }
550
551
    public function testGetBaseRouteNameWithChildAdminAndWithSpecifiedName()
552
    {
553
        $postAdmin = new PostAdmin('sonata.post.admin.post', 'Application\Sonata\NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
554
        $commentAdmin = new CommentWithCustomRouteAdmin('sonata.post.admin.comment_with_custom_route', 'Application\Sonata\NewsBundle\Entity\Comment', 'SonataNewsBundle:CommentWithCustomRouteAdmin');
555
        $commentAdmin->setParent($postAdmin);
556
557
        $this->assertSame('admin_sonata_news_post_comment_custom', $commentAdmin->getBaseRouteName());
558
    }
559
560
    /**
561
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::setUniqid
562
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::getUniqid
563
     */
564
    public function testUniqid()
565
    {
566
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
567
568
        $uniqid = uniqid();
569
        $admin->setUniqid($uniqid);
570
571
        $this->assertSame($uniqid, $admin->getUniqid());
572
    }
573
574
    public function testToString()
575
    {
576
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
577
578
        $s = new \stdClass();
579
580
        $this->assertNotEmpty($admin->toString($s));
581
582
        $s = new FooToString();
583
        $this->assertSame('salut', $admin->toString($s));
584
585
        // To string method is implemented, but returns null
586
        $s = new FooToStringNull();
587
        $this->assertNotEmpty($admin->toString($s));
588
589
        $this->assertSame('', $admin->toString(false));
590
    }
591
592
    public function testIsAclEnabled()
593
    {
594
        $postAdmin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
595
596
        $this->assertFalse($postAdmin->isAclEnabled());
597
598
        $commentAdmin = new CommentAdmin('sonata.post.admin.comment', 'Application\Sonata\NewsBundle\Entity\Comment', 'SonataNewsBundle:CommentAdmin');
599
        $commentAdmin->setSecurityHandler($this->createMock('Sonata\AdminBundle\Security\Handler\AclSecurityHandlerInterface'));
600
        $this->assertTrue($commentAdmin->isAclEnabled());
601
    }
602
603
    /**
604
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::getSubClasses
605
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::getSubClass
606
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::setSubClasses
607
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::hasSubClass
608
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::hasActiveSubClass
609
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::getActiveSubClass
610
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::getActiveSubclassCode
611
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::getClass
612
     */
613
    public function testSubClass()
614
    {
615
        $admin = new PostAdmin(
616
            'sonata.post.admin.post',
617
            'Sonata\AdminBundle\Tests\Fixtures\Bundle\Entity\Post',
618
            'SonataNewsBundle:PostAdmin'
619
        );
620
        $this->assertFalse($admin->hasSubClass('test'));
621
        $this->assertFalse($admin->hasActiveSubClass());
622
        $this->assertCount(0, $admin->getSubClasses());
623
        $this->assertNull($admin->getActiveSubClass());
624
        $this->assertNull($admin->getActiveSubclassCode());
625
        $this->assertSame(
626
            'Sonata\AdminBundle\Tests\Fixtures\Bundle\Entity\Post',
627
            $admin->getClass()
628
        );
629
630
        // Just for the record, if there is no inheritance set, the getSubject is not used
631
        // the getSubject can also lead to some issue
632
        $admin->setSubject(new \Sonata\AdminBundle\Tests\Fixtures\Bundle\Entity\BlogPost());
633
        $this->assertSame(
634
            'Sonata\AdminBundle\Tests\Fixtures\Bundle\Entity\BlogPost',
635
            $admin->getClass()
636
        );
637
638
        $admin->setSubClasses(array('extended1' => 'NewsBundle\Entity\PostExtended1', 'extended2' => 'NewsBundle\Entity\PostExtended2'));
639
        $this->assertFalse($admin->hasSubClass('test'));
640
        $this->assertTrue($admin->hasSubClass('extended1'));
641
        $this->assertFalse($admin->hasActiveSubClass());
642
        $this->assertCount(2, $admin->getSubClasses());
643
        $this->assertNull($admin->getActiveSubClass());
644
        $this->assertNull($admin->getActiveSubclassCode());
645
        $this->assertSame(
646
            'Sonata\AdminBundle\Tests\Fixtures\Bundle\Entity\BlogPost',
647
            $admin->getClass()
648
        );
649
650
        $request = new \Symfony\Component\HttpFoundation\Request(array('subclass' => 'extended1'));
651
        $admin->setRequest($request);
652
        $this->assertFalse($admin->hasSubClass('test'));
653
        $this->assertTrue($admin->hasSubClass('extended1'));
654
        $this->assertTrue($admin->hasActiveSubClass());
655
        $this->assertCount(2, $admin->getSubClasses());
656
        $this->assertSame(
657
            'Sonata\AdminBundle\Tests\Fixtures\Bundle\Entity\BlogPost',
658
            $admin->getActiveSubClass()
659
        );
660
        $this->assertSame('extended1', $admin->getActiveSubclassCode());
661
        $this->assertSame(
662
            'Sonata\AdminBundle\Tests\Fixtures\Bundle\Entity\BlogPost',
663
            $admin->getClass()
664
        );
665
666
        $request->query->set('subclass', 'inject');
667
        $this->assertNull($admin->getActiveSubclassCode());
668
    }
669
670
    /**
671
     * @expectedException \RuntimeException
672
     */
673
    public function testNonExistantSubclass()
674
    {
675
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
676
        $admin->setRequest(new \Symfony\Component\HttpFoundation\Request(array('subclass' => 'inject')));
677
678
        $admin->setSubClasses(array('extended1' => 'NewsBundle\Entity\PostExtended1', 'extended2' => 'NewsBundle\Entity\PostExtended2'));
679
680
        $this->assertTrue($admin->hasActiveSubClass());
681
682
        $admin->getActiveSubClass();
683
    }
684
685
    /**
686
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::hasActiveSubClass
687
     */
688
    public function testOnlyOneSubclassNeededToBeActive()
689
    {
690
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
691
        $admin->setSubClasses(array('extended1' => 'NewsBundle\Entity\PostExtended1'));
692
        $request = new \Symfony\Component\HttpFoundation\Request(array('subclass' => 'extended1'));
693
        $admin->setRequest($request);
694
        $this->assertTrue($admin->hasActiveSubClass());
695
    }
696
697
    public function testGetPerPageOptions()
698
    {
699
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
700
701
        $this->assertSame(array(16, 32, 64, 128, 192), $admin->getPerPageOptions());
702
        $admin->setPerPageOptions(array(500, 1000));
703
        $this->assertSame(array(500, 1000), $admin->getPerPageOptions());
704
    }
705
706
    public function testGetLabelTranslatorStrategy()
707
    {
708
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
709
710
        $this->assertNull($admin->getLabelTranslatorStrategy());
711
712
        $labelTranslatorStrategy = $this->createMock('Sonata\AdminBundle\Translator\LabelTranslatorStrategyInterface');
713
        $admin->setLabelTranslatorStrategy($labelTranslatorStrategy);
714
        $this->assertSame($labelTranslatorStrategy, $admin->getLabelTranslatorStrategy());
715
    }
716
717
    public function testGetRouteBuilder()
718
    {
719
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
720
721
        $this->assertNull($admin->getRouteBuilder());
722
723
        $routeBuilder = $this->createMock('Sonata\AdminBundle\Builder\RouteBuilderInterface');
724
        $admin->setRouteBuilder($routeBuilder);
725
        $this->assertSame($routeBuilder, $admin->getRouteBuilder());
726
    }
727
728
    public function testGetMenuFactory()
729
    {
730
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
731
732
        $this->assertNull($admin->getMenuFactory());
733
734
        $menuFactory = $this->createMock('Knp\Menu\FactoryInterface');
735
        $admin->setMenuFactory($menuFactory);
736
        $this->assertSame($menuFactory, $admin->getMenuFactory());
737
    }
738
739
    public function testGetExtensions()
740
    {
741
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
742
743
        $this->assertSame(array(), $admin->getExtensions());
744
745
        $adminExtension1 = $this->createMock('Sonata\AdminBundle\Admin\AdminExtensionInterface');
746
        $adminExtension2 = $this->createMock('Sonata\AdminBundle\Admin\AdminExtensionInterface');
747
748
        $admin->addExtension($adminExtension1);
749
        $admin->addExtension($adminExtension2);
750
        $this->assertSame(array($adminExtension1, $adminExtension2), $admin->getExtensions());
751
    }
752
753
    public function testGetFilterTheme()
754
    {
755
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
756
757
        $this->assertSame(array(), $admin->getFilterTheme());
758
759
        $admin->setFilterTheme(array('FooTheme'));
760
        $this->assertSame(array('FooTheme'), $admin->getFilterTheme());
761
    }
762
763
    public function testGetFormTheme()
764
    {
765
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
766
767
        $this->assertSame(array(), $admin->getFormTheme());
768
769
        $admin->setFormTheme(array('FooTheme'));
770
771
        $this->assertSame(array('FooTheme'), $admin->getFormTheme());
772
    }
773
774
    public function testGetValidator()
775
    {
776
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
777
778
        $this->assertNull($admin->getValidator());
779
780
        $validatorClass = '\Symfony\Component\Validator\ValidatorInterface';
781
782
        if (interface_exists('Symfony\Component\Validator\Validator\ValidatorInterface')) {
783
            $validatorClass = 'Symfony\Component\Validator\Validator\ValidatorInterface';
784
        }
785
786
        $validator = $this->getMockForAbstractClass($validatorClass);
787
788
        $admin->setValidator($validator);
789
        $this->assertSame($validator, $admin->getValidator());
790
    }
791
792
    public function testGetSecurityHandler()
793
    {
794
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
795
796
        $this->assertNull($admin->getSecurityHandler());
797
798
        $securityHandler = $this->createMock('Sonata\AdminBundle\Security\Handler\SecurityHandlerInterface');
799
        $admin->setSecurityHandler($securityHandler);
800
        $this->assertSame($securityHandler, $admin->getSecurityHandler());
801
    }
802
803
    public function testGetSecurityInformation()
804
    {
805
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
806
807
        $this->assertSame(array(), $admin->getSecurityInformation());
808
809
        $securityInformation = array(
810
            'GUEST' => array('VIEW', 'LIST'),
811
            'STAFF' => array('EDIT', 'LIST', 'CREATE'),
812
        );
813
814
        $admin->setSecurityInformation($securityInformation);
815
        $this->assertSame($securityInformation, $admin->getSecurityInformation());
816
    }
817
818
    public function testGetManagerType()
819
    {
820
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
821
822
        $this->assertNull($admin->getManagerType());
823
824
        $admin->setManagerType('foo_orm');
825
        $this->assertSame('foo_orm', $admin->getManagerType());
826
    }
827
828
    public function testGetModelManager()
829
    {
830
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
831
832
        $this->assertNull($admin->getModelManager());
833
834
        $modelManager = $this->createMock('Sonata\AdminBundle\Model\ModelManagerInterface');
835
836
        $admin->setModelManager($modelManager);
837
        $this->assertSame($modelManager, $admin->getModelManager());
838
    }
839
840
    public function testGetBaseCodeRoute()
841
    {
842
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
843
844
        $this->assertSame('', $admin->getBaseCodeRoute());
845
846
        $admin->setBaseCodeRoute('foo');
847
        $this->assertSame('foo', $admin->getBaseCodeRoute());
848
    }
849
850
    public function testGetRouteGenerator()
851
    {
852
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
853
854
        $this->assertNull($admin->getRouteGenerator());
855
856
        $routeGenerator = $this->createMock('Sonata\AdminBundle\Route\RouteGeneratorInterface');
857
858
        $admin->setRouteGenerator($routeGenerator);
859
        $this->assertSame($routeGenerator, $admin->getRouteGenerator());
860
    }
861
862
    public function testGetConfigurationPool()
863
    {
864
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
865
866
        $this->assertNull($admin->getConfigurationPool());
867
868
        $pool = $this->getMockBuilder('Sonata\AdminBundle\Admin\Pool')
869
            ->disableOriginalConstructor()
870
            ->getMock();
871
872
        $admin->setConfigurationPool($pool);
873
        $this->assertSame($pool, $admin->getConfigurationPool());
874
    }
875
876
    public function testGetShowBuilder()
877
    {
878
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
879
880
        $this->assertNull($admin->getShowBuilder());
881
882
        $showBuilder = $this->createMock('Sonata\AdminBundle\Builder\ShowBuilderInterface');
883
884
        $admin->setShowBuilder($showBuilder);
885
        $this->assertSame($showBuilder, $admin->getShowBuilder());
886
    }
887
888
    public function testGetListBuilder()
889
    {
890
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
891
892
        $this->assertNull($admin->getListBuilder());
893
894
        $listBuilder = $this->createMock('Sonata\AdminBundle\Builder\ListBuilderInterface');
895
896
        $admin->setListBuilder($listBuilder);
897
        $this->assertSame($listBuilder, $admin->getListBuilder());
898
    }
899
900
    public function testGetDatagridBuilder()
901
    {
902
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
903
904
        $this->assertNull($admin->getDatagridBuilder());
905
906
        $datagridBuilder = $this->createMock('Sonata\AdminBundle\Builder\DatagridBuilderInterface');
907
908
        $admin->setDatagridBuilder($datagridBuilder);
909
        $this->assertSame($datagridBuilder, $admin->getDatagridBuilder());
910
    }
911
912
    public function testGetFormContractor()
913
    {
914
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
915
916
        $this->assertNull($admin->getFormContractor());
917
918
        $formContractor = $this->createMock('Sonata\AdminBundle\Builder\FormContractorInterface');
919
920
        $admin->setFormContractor($formContractor);
921
        $this->assertSame($formContractor, $admin->getFormContractor());
922
    }
923
924
    public function testGetRequest()
925
    {
926
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
927
928
        $this->assertFalse($admin->hasRequest());
929
930
        $request = new Request();
931
932
        $admin->setRequest($request);
933
        $this->assertSame($request, $admin->getRequest());
934
        $this->assertTrue($admin->hasRequest());
935
    }
936
937
    public function testGetRequestWithException()
938
    {
939
        $this->setExpectedException('RuntimeException', 'The Request object has not been set');
940
941
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
942
        $admin->getRequest();
943
    }
944
945
    public function testGetTranslationDomain()
946
    {
947
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
948
949
        $this->assertSame('messages', $admin->getTranslationDomain());
950
951
        $admin->setTranslationDomain('foo');
952
        $this->assertSame('foo', $admin->getTranslationDomain());
953
    }
954
955
    /**
956
     * @group legacy
957
     */
958
    public function testGetTranslator()
959
    {
960
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
961
962
        $this->assertNull($admin->getTranslator());
0 ignored issues
show
Deprecated Code introduced by
The method Sonata\AdminBundle\Admin...tAdmin::getTranslator() has been deprecated with message: since 3.9, to be removed with 4.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
963
964
        $translator = $this->createMock('Symfony\Component\Translation\TranslatorInterface');
965
966
        $admin->setTranslator($translator);
0 ignored issues
show
Deprecated Code introduced by
The method Sonata\AdminBundle\Admin...tAdmin::setTranslator() has been deprecated with message: since 3.9, to be removed with 4.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
967
        $this->assertSame($translator, $admin->getTranslator());
0 ignored issues
show
Deprecated Code introduced by
The method Sonata\AdminBundle\Admin...tAdmin::getTranslator() has been deprecated with message: since 3.9, to be removed with 4.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
968
    }
969
970
    public function testGetShowGroups()
971
    {
972
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
973
974
        $this->assertSame(false, $admin->getShowGroups());
975
976
        $groups = array('foo', 'bar', 'baz');
977
978
        $admin->setShowGroups($groups);
979
        $this->assertSame($groups, $admin->getShowGroups());
980
    }
981
982
    public function testGetFormGroups()
983
    {
984
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
985
986
        $this->assertSame(false, $admin->getFormGroups());
987
988
        $groups = array('foo', 'bar', 'baz');
989
990
        $admin->setFormGroups($groups);
991
        $this->assertSame($groups, $admin->getFormGroups());
992
    }
993
994
    public function testGetMaxPageLinks()
995
    {
996
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
997
998
        $this->assertSame(25, $admin->getMaxPageLinks());
999
1000
        $admin->setMaxPageLinks(14);
1001
        $this->assertSame(14, $admin->getMaxPageLinks());
1002
    }
1003
1004
    public function testGetMaxPerPage()
1005
    {
1006
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1007
1008
        $this->assertSame(32, $admin->getMaxPerPage());
1009
1010
        $admin->setMaxPerPage(94);
1011
        $this->assertSame(94, $admin->getMaxPerPage());
1012
    }
1013
1014
    public function testGetLabel()
1015
    {
1016
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1017
1018
        $this->assertNull($admin->getLabel());
1019
1020
        $admin->setLabel('FooLabel');
1021
        $this->assertSame('FooLabel', $admin->getLabel());
1022
    }
1023
1024
    public function testGetBaseController()
1025
    {
1026
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1027
1028
        $this->assertSame('SonataNewsBundle:PostAdmin', $admin->getBaseControllerName());
1029
1030
        $admin->setBaseControllerName('SonataNewsBundle:FooAdmin');
1031
        $this->assertSame('SonataNewsBundle:FooAdmin', $admin->getBaseControllerName());
1032
    }
1033
1034
    public function testGetTemplates()
1035
    {
1036
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1037
1038
        $this->assertSame(array(), $admin->getTemplates());
1039
1040
        $templates = array(
1041
            'list' => 'FooAdminBundle:CRUD:list.html.twig',
1042
            'show' => 'FooAdminBundle:CRUD:show.html.twig',
1043
            'edit' => 'FooAdminBundle:CRUD:edit.html.twig',
1044
        );
1045
1046
        $admin->setTemplates($templates);
1047
        $this->assertSame($templates, $admin->getTemplates());
1048
    }
1049
1050
    public function testGetTemplate1()
1051
    {
1052
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1053
1054
        $this->assertNull($admin->getTemplate('edit'));
1055
1056
        $admin->setTemplate('edit', 'FooAdminBundle:CRUD:edit.html.twig');
1057
        $admin->setTemplate('show', 'FooAdminBundle:CRUD:show.html.twig');
1058
1059
        $this->assertSame('FooAdminBundle:CRUD:edit.html.twig', $admin->getTemplate('edit'));
1060
        $this->assertSame('FooAdminBundle:CRUD:show.html.twig', $admin->getTemplate('show'));
1061
    }
1062
1063
    public function testGetTemplate2()
1064
    {
1065
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1066
1067
        $this->assertNull($admin->getTemplate('edit'));
1068
1069
        $templates = array(
1070
            'list' => 'FooAdminBundle:CRUD:list.html.twig',
1071
            'show' => 'FooAdminBundle:CRUD:show.html.twig',
1072
            'edit' => 'FooAdminBundle:CRUD:edit.html.twig',
1073
        );
1074
1075
        $admin->setTemplates($templates);
1076
1077
        $this->assertSame('FooAdminBundle:CRUD:edit.html.twig', $admin->getTemplate('edit'));
1078
        $this->assertSame('FooAdminBundle:CRUD:show.html.twig', $admin->getTemplate('show'));
1079
    }
1080
1081
    public function testGetIdParameter()
1082
    {
1083
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1084
1085
        $this->assertSame('id', $admin->getIdParameter());
1086
        $this->assertFalse($admin->isChild());
1087
1088
        $parentAdmin = new PostAdmin('sonata.post.admin.post_parent', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostParentAdmin');
1089
        $admin->setParent($parentAdmin);
1090
1091
        $this->assertTrue($admin->isChild());
1092
        $this->assertSame('childId', $admin->getIdParameter());
1093
    }
1094
1095
    public function testGetExportFormats()
1096
    {
1097
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1098
1099
        $this->assertSame(array('json', 'xml', 'csv', 'xls'), $admin->getExportFormats());
1100
    }
1101
1102
    public function testGetUrlsafeIdentifier()
1103
    {
1104
        $admin = new PostAdmin('sonata.post.admin.post', 'Acme\NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1105
1106
        $entity = new \stdClass();
1107
1108
        $modelManager = $this->createMock('Sonata\AdminBundle\Model\ModelManagerInterface');
1109
        $modelManager->expects($this->once())
1110
            ->method('getUrlsafeIdentifier')
1111
            ->with($this->equalTo($entity))
1112
            ->will($this->returnValue('foo'));
1113
        $admin->setModelManager($modelManager);
1114
1115
        $this->assertSame('foo', $admin->getUrlsafeIdentifier($entity));
1116
    }
1117
1118
    public function testDeterminedPerPageValue()
1119
    {
1120
        $admin = new PostAdmin('sonata.post.admin.post', 'Acme\NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1121
1122
        $this->assertFalse($admin->determinedPerPageValue('foo'));
1123
        $this->assertFalse($admin->determinedPerPageValue(123));
1124
        $this->assertTrue($admin->determinedPerPageValue(16));
1125
        $this->assertTrue($admin->determinedPerPageValue(32));
1126
        $this->assertTrue($admin->determinedPerPageValue(64));
1127
        $this->assertTrue($admin->determinedPerPageValue(128));
1128
        $this->assertTrue($admin->determinedPerPageValue(192));
1129
1130
        $admin->setPerPageOptions(array(101, 102, 103));
1131
        $this->assertFalse($admin->determinedPerPageValue(15));
1132
        $this->assertFalse($admin->determinedPerPageValue(25));
1133
        $this->assertFalse($admin->determinedPerPageValue(200));
1134
        $this->assertTrue($admin->determinedPerPageValue(101));
1135
        $this->assertTrue($admin->determinedPerPageValue(102));
1136
        $this->assertTrue($admin->determinedPerPageValue(103));
1137
    }
1138
1139
    public function testIsGranted()
1140
    {
1141
        $admin = new PostAdmin('sonata.post.admin.post', 'Acme\NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1142
1143
        $entity = new \stdClass();
1144
1145
        $securityHandler = $this->createMock('Sonata\AdminBundle\Security\Handler\AclSecurityHandlerInterface');
1146
        $securityHandler->expects($this->any())
1147
            ->method('isGranted')
1148
            ->will($this->returnCallback(function (AdminInterface $adminIn, $attributes, $object = null) use ($admin, $entity) {
1149
                if ($admin == $adminIn && $attributes == 'FOO') {
1150
                    if (($object == $admin) || ($object == $entity)) {
1151
                        return true;
1152
                    }
1153
                }
1154
1155
                return false;
1156
            }));
1157
1158
        $admin->setSecurityHandler($securityHandler);
1159
1160
        $this->assertTrue($admin->isGranted('FOO'));
1161
        $this->assertTrue($admin->isGranted('FOO', $entity));
1162
        $this->assertFalse($admin->isGranted('BAR'));
1163
        $this->assertFalse($admin->isGranted('BAR', $entity));
1164
    }
1165
1166
    public function testSupportsPreviewMode()
1167
    {
1168
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1169
1170
        $this->assertFalse($admin->supportsPreviewMode());
1171
    }
1172
1173
    public function testGetPermissionsShow()
1174
    {
1175
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1176
1177
        $this->assertSame(array('LIST'), $admin->getPermissionsShow(AbstractAdmin::CONTEXT_DASHBOARD));
1178
        $this->assertSame(array('LIST'), $admin->getPermissionsShow(AbstractAdmin::CONTEXT_MENU));
1179
        $this->assertSame(array('LIST'), $admin->getPermissionsShow('foo'));
1180
    }
1181
1182
    public function testShowIn()
1183
    {
1184
        $admin = new PostAdmin('sonata.post.admin.post', 'Acme\NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1185
1186
        $securityHandler = $this->createMock('Sonata\AdminBundle\Security\Handler\AclSecurityHandlerInterface');
1187
        $securityHandler->expects($this->any())
1188
            ->method('isGranted')
1189
            ->will($this->returnCallback(function (AdminInterface $adminIn, $attributes, $object = null) use ($admin) {
0 ignored issues
show
Unused Code introduced by
The parameter $object is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
1190
                if ($admin == $adminIn && $attributes == array('LIST')) {
1191
                    return true;
1192
                }
1193
1194
                return false;
1195
            }));
1196
1197
        $admin->setSecurityHandler($securityHandler);
1198
1199
        $this->assertTrue($admin->showIn(AbstractAdmin::CONTEXT_DASHBOARD));
1200
        $this->assertTrue($admin->showIn(AbstractAdmin::CONTEXT_MENU));
1201
        $this->assertTrue($admin->showIn('foo'));
1202
    }
1203
1204
    public function testGetObjectIdentifier()
1205
    {
1206
        $admin = new PostAdmin('sonata.post.admin.post', 'Acme\NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1207
1208
        $this->assertSame('sonata.post.admin.post', $admin->getObjectIdentifier());
1209
    }
1210
1211
    /**
1212
     * @group legacy
1213
     */
1214
    public function testTrans()
1215
    {
1216
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1217
        $admin->setTranslationDomain('fooMessageDomain');
1218
1219
        $translator = $this->createMock('Symfony\Component\Translation\TranslatorInterface');
1220
        $admin->setTranslator($translator);
0 ignored issues
show
Deprecated Code introduced by
The method Sonata\AdminBundle\Admin...tAdmin::setTranslator() has been deprecated with message: since 3.9, to be removed with 4.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
1221
1222
        $translator->expects($this->once())
1223
            ->method('trans')
1224
            ->with($this->equalTo('foo'), $this->equalTo(array()), $this->equalTo('fooMessageDomain'))
1225
            ->will($this->returnValue('fooTranslated'));
1226
1227
        $this->assertSame('fooTranslated', $admin->trans('foo'));
1228
    }
1229
1230
    /**
1231
     * @group legacy
1232
     */
1233
    public function testTransWithMessageDomain()
1234
    {
1235
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1236
1237
        $translator = $this->createMock('Symfony\Component\Translation\TranslatorInterface');
1238
        $admin->setTranslator($translator);
0 ignored issues
show
Deprecated Code introduced by
The method Sonata\AdminBundle\Admin...tAdmin::setTranslator() has been deprecated with message: since 3.9, to be removed with 4.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
1239
1240
        $translator->expects($this->once())
1241
            ->method('trans')
1242
            ->with($this->equalTo('foo'), $this->equalTo(array('name' => 'Andrej')), $this->equalTo('fooMessageDomain'))
1243
            ->will($this->returnValue('fooTranslated'));
1244
1245
        $this->assertSame('fooTranslated', $admin->trans('foo', array('name' => 'Andrej'), 'fooMessageDomain'));
1246
    }
1247
1248
    /**
1249
     * @group legacy
1250
     */
1251
    public function testTransChoice()
1252
    {
1253
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1254
        $admin->setTranslationDomain('fooMessageDomain');
1255
1256
        $translator = $this->createMock('Symfony\Component\Translation\TranslatorInterface');
1257
        $admin->setTranslator($translator);
0 ignored issues
show
Deprecated Code introduced by
The method Sonata\AdminBundle\Admin...tAdmin::setTranslator() has been deprecated with message: since 3.9, to be removed with 4.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
1258
1259
        $translator->expects($this->once())
1260
            ->method('transChoice')
1261
            ->with($this->equalTo('foo'), $this->equalTo(2), $this->equalTo(array()), $this->equalTo('fooMessageDomain'))
1262
            ->will($this->returnValue('fooTranslated'));
1263
1264
        $this->assertSame('fooTranslated', $admin->transChoice('foo', 2));
0 ignored issues
show
Deprecated Code introduced by
The method Sonata\AdminBundle\Admin...actAdmin::transChoice() has been deprecated with message: since 3.9, to be removed with 4.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
1265
    }
1266
1267
    /**
1268
     * @group legacy
1269
     */
1270
    public function testTransChoiceWithMessageDomain()
1271
    {
1272
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1273
1274
        $translator = $this->createMock('Symfony\Component\Translation\TranslatorInterface');
1275
        $admin->setTranslator($translator);
0 ignored issues
show
Deprecated Code introduced by
The method Sonata\AdminBundle\Admin...tAdmin::setTranslator() has been deprecated with message: since 3.9, to be removed with 4.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
1276
1277
        $translator->expects($this->once())
1278
            ->method('transChoice')
1279
            ->with($this->equalTo('foo'), $this->equalTo(2), $this->equalTo(array('name' => 'Andrej')), $this->equalTo('fooMessageDomain'))
1280
            ->will($this->returnValue('fooTranslated'));
1281
1282
        $this->assertSame('fooTranslated', $admin->transChoice('foo', 2, array('name' => 'Andrej'), 'fooMessageDomain'));
0 ignored issues
show
Deprecated Code introduced by
The method Sonata\AdminBundle\Admin...actAdmin::transChoice() has been deprecated with message: since 3.9, to be removed with 4.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
1283
    }
1284
1285
    public function testSetPersistFilters()
1286
    {
1287
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1288
1289
        $this->assertAttributeSame(false, 'persistFilters', $admin);
1290
        $admin->setPersistFilters(true);
1291
        $this->assertAttributeSame(true, 'persistFilters', $admin);
1292
    }
1293
1294
    public function testGetRootCode()
1295
    {
1296
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1297
1298
        $this->assertSame('sonata.post.admin.post', $admin->getRootCode());
1299
1300
        $parentAdmin = new PostAdmin('sonata.post.admin.post.parent', 'NewsBundle\Entity\PostParent', 'SonataNewsBundle:PostParentAdmin');
1301
        $parentFieldDescription = $this->createMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface');
1302
        $parentFieldDescription->expects($this->once())
1303
            ->method('getAdmin')
1304
            ->will($this->returnValue($parentAdmin));
1305
1306
        $this->assertNull($admin->getParentFieldDescription());
1307
        $admin->setParentFieldDescription($parentFieldDescription);
1308
        $this->assertSame($parentFieldDescription, $admin->getParentFieldDescription());
1309
        $this->assertSame('sonata.post.admin.post.parent', $admin->getRootCode());
1310
    }
1311
1312
    public function testGetRoot()
1313
    {
1314
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1315
1316
        $this->assertSame($admin, $admin->getRoot());
1317
1318
        $parentAdmin = new PostAdmin('sonata.post.admin.post.parent', 'NewsBundle\Entity\PostParent', 'SonataNewsBundle:PostParentAdmin');
1319
        $parentFieldDescription = $this->createMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface');
1320
        $parentFieldDescription->expects($this->once())
1321
            ->method('getAdmin')
1322
            ->will($this->returnValue($parentAdmin));
1323
1324
        $this->assertNull($admin->getParentFieldDescription());
1325
        $admin->setParentFieldDescription($parentFieldDescription);
1326
        $this->assertSame($parentFieldDescription, $admin->getParentFieldDescription());
1327
        $this->assertSame($parentAdmin, $admin->getRoot());
1328
    }
1329
1330
    public function testGetExportFields()
1331
    {
1332
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1333
1334
        $modelManager = $this->createMock('Sonata\AdminBundle\Model\ModelManagerInterface');
1335
        $modelManager->expects($this->once())
1336
            ->method('getExportFields')
1337
            ->with($this->equalTo('NewsBundle\Entity\Post'))
1338
            ->will($this->returnValue(array('foo', 'bar')));
1339
1340
        $admin->setModelManager($modelManager);
1341
        $this->assertSame(array('foo', 'bar'), $admin->getExportFields());
1342
    }
1343
1344
    public function testGetPersistentParametersWithNoExtension()
1345
    {
1346
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1347
1348
        $this->assertEmpty($admin->getPersistentParameters());
1349
    }
1350
1351
    /**
1352
     * @expectedException \RuntimeException
1353
     */
1354
    public function testGetPersistentParametersWithInvalidExtension()
1355
    {
1356
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1357
1358
        $extension = $this->createMock('Sonata\AdminBundle\Admin\AdminExtensionInterface');
1359
        $extension->expects($this->once())->method('getPersistentParameters')->will($this->returnValue(null));
1360
1361
        $admin->addExtension($extension);
1362
1363
        $admin->getPersistentParameters();
1364
    }
1365
1366
    public function testGetPersistentParametersWithValidExtension()
1367
    {
1368
        $expected = array(
1369
            'context' => 'foobar',
1370
        );
1371
1372
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1373
1374
        $extension = $this->createMock('Sonata\AdminBundle\Admin\AdminExtensionInterface');
1375
        $extension->expects($this->once())->method('getPersistentParameters')->will($this->returnValue($expected));
1376
1377
        $admin->addExtension($extension);
1378
1379
        $this->assertSame($expected, $admin->getPersistentParameters());
1380
    }
1381
1382
    public function testGetFormWithNonCollectionParentValue()
1383
    {
1384
        $post = new Post();
1385
        $tagAdmin = $this->createTagAdmin($post);
1386
        $tag = $tagAdmin->getSubject();
1387
1388
        $tag->setPosts(null);
1389
        $tagAdmin->getForm();
1390
        $this->assertSame($post, $tag->getPosts());
1391
    }
1392
1393
    public function testGetFormWithCollectionParentValue()
1394
    {
1395
        $post = new Post();
1396
        $tagAdmin = $this->createTagAdmin($post);
1397
        $tag = $tagAdmin->getSubject();
1398
1399
        // Case of a doctrine collection
1400
        $this->assertInstanceOf('Doctrine\Common\Collections\Collection', $tag->getPosts());
1401
        $this->assertCount(0, $tag->getPosts());
1402
1403
        $tag->addPost(new Post());
1404
1405
        $this->assertCount(1, $tag->getPosts());
1406
1407
        $tagAdmin->getForm();
1408
1409
        $this->assertInstanceOf('Doctrine\Common\Collections\Collection', $tag->getPosts());
1410
        $this->assertCount(2, $tag->getPosts());
1411
        $this->assertContains($post, $tag->getPosts());
1412
1413
        // Case of an array
1414
        $tag->setPosts(array());
1415
        $this->assertCount(0, $tag->getPosts());
1416
1417
        $tag->addPost(new Post());
1418
1419
        $this->assertCount(1, $tag->getPosts());
1420
1421
        $tagAdmin->getForm();
1422
1423
        $this->assertInternalType('array', $tag->getPosts());
1424
        $this->assertCount(2, $tag->getPosts());
1425
        $this->assertContains($post, $tag->getPosts());
1426
    }
1427
1428
    public function testRemoveFieldFromFormGroup()
1429
    {
1430
        $formGroups = array(
1431
            'foobar' => array(
1432
                'fields' => array(
1433
                    'foo' => 'foo',
1434
                    'bar' => 'bar',
1435
                ),
1436
            ),
1437
        );
1438
1439
        $admin = new PostAdmin('sonata.post.admin.post', 'Application\Sonata\NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1440
        $admin->setFormGroups($formGroups);
1441
1442
        $admin->removeFieldFromFormGroup('foo');
1443
        $this->assertSame($admin->getFormGroups(), array(
1444
            'foobar' => array(
1445
                'fields' => array(
1446
                    'bar' => 'bar',
1447
                ),
1448
            ),
1449
        ));
1450
1451
        $admin->removeFieldFromFormGroup('bar');
1452
        $this->assertSame($admin->getFormGroups(), array());
1453
    }
1454
1455
    public function testGetFilterParameters()
1456
    {
1457
        $authorId = uniqid();
1458
1459
        $postAdmin = new PostAdmin('sonata.post.admin.post', 'Application\Sonata\NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1460
1461
        $commentAdmin = new CommentAdmin('sonata.post.admin.comment', 'Application\Sonata\NewsBundle\Entity\Comment', 'SonataNewsBundle:CommentAdmin');
1462
        $commentAdmin->setParentAssociationMapping('post.author');
1463
        $commentAdmin->setParent($postAdmin);
1464
1465
        $request = $this->createMock('Symfony\Component\HttpFoundation\Request', array('get'));
1466
        $query = $this->createMock('Symfony\Component\HttpFoundation\ParameterBag', array('get'));
1467
        $query->expects($this->any())
1468
            ->method('get')
1469
            ->will($this->returnValue(array()));
1470
        $request->query = $query;
1471
        $request->expects($this->any())
1472
            ->method('get')
1473
            ->will($this->returnValue($authorId));
1474
1475
        $commentAdmin->setRequest($request);
1476
1477
        $modelManager = $this->createMock('Sonata\AdminBundle\Model\ModelManagerInterface');
1478
        $modelManager->expects($this->any())
1479
            ->method('getDefaultSortValues')
1480
            ->will($this->returnValue(array()));
1481
1482
        $commentAdmin->setModelManager($modelManager);
1483
1484
        $parameters = $commentAdmin->getFilterParameters();
1485
1486
        $this->assertTrue(isset($parameters['post__author']));
1487
        $this->assertSame(array('value' => $authorId), $parameters['post__author']);
1488
    }
1489
1490
    public function testGetFilterFieldDescription()
1491
    {
1492
        $modelAdmin = new ModelAdmin('sonata.post.admin.model', 'Application\Sonata\FooBundle\Entity\Model', 'SonataFooBundle:ModelAdmin');
1493
1494
        $fooFieldDescription = new FieldDescription();
1495
        $barFieldDescription = new FieldDescription();
1496
        $bazFieldDescription = new FieldDescription();
1497
1498
        $modelManager = $this->createMock('Sonata\AdminBundle\Model\ModelManagerInterface');
1499
        $modelManager->expects($this->exactly(3))
1500
            ->method('getNewFieldDescriptionInstance')
1501
            ->will($this->returnCallback(function ($adminClass, $name, $filterOptions) use ($fooFieldDescription, $barFieldDescription, $bazFieldDescription) {
0 ignored issues
show
Unused Code introduced by
The parameter $filterOptions is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
1502
                switch ($name) {
1503
                    case 'foo':
1504
                        $fieldDescription = $fooFieldDescription;
1505
1506
                        break;
1507
1508
                    case 'bar':
1509
                        $fieldDescription = $barFieldDescription;
1510
1511
                        break;
1512
1513
                    case 'baz':
1514
                        $fieldDescription = $bazFieldDescription;
1515
1516
                        break;
1517
1518
                    default:
1519
                        throw new \RuntimeException(sprintf('Unknown filter name "%s"', $name));
1520
                        break;
0 ignored issues
show
Unused Code introduced by
break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
1521
                }
1522
1523
                $fieldDescription->setName($name);
1524
1525
                return $fieldDescription;
1526
            }));
1527
1528
        $modelAdmin->setModelManager($modelManager);
1529
1530
        $pager = $this->createMock('Sonata\AdminBundle\Datagrid\PagerInterface');
1531
1532
        $datagrid = $this->createMock('Sonata\AdminBundle\Datagrid\DatagridInterface');
1533
        $datagrid->expects($this->once())
1534
            ->method('getPager')
1535
            ->will($this->returnValue($pager));
1536
1537
        $datagridBuilder = $this->createMock('Sonata\AdminBundle\Builder\DatagridBuilderInterface');
1538
        $datagridBuilder->expects($this->once())
1539
            ->method('getBaseDatagrid')
1540
            ->with($this->identicalTo($modelAdmin), array())
1541
            ->will($this->returnValue($datagrid));
1542
1543
        $datagridBuilder->expects($this->exactly(3))
1544
            ->method('addFilter')
1545
            ->will($this->returnCallback(function ($datagrid, $type, $fieldDescription, AdminInterface $admin) {
1546
                $admin->addFilterFieldDescription($fieldDescription->getName(), $fieldDescription);
1547
                $fieldDescription->mergeOption('field_options', array('required' => false));
1548
            }));
1549
1550
        $modelAdmin->setDatagridBuilder($datagridBuilder);
1551
1552
        $this->assertSame(array('foo' => $fooFieldDescription, 'bar' => $barFieldDescription, 'baz' => $bazFieldDescription), $modelAdmin->getFilterFieldDescriptions());
1553
        $this->assertFalse($modelAdmin->hasFilterFieldDescription('fooBar'));
1554
        $this->assertTrue($modelAdmin->hasFilterFieldDescription('foo'));
1555
        $this->assertTrue($modelAdmin->hasFilterFieldDescription('bar'));
1556
        $this->assertTrue($modelAdmin->hasFilterFieldDescription('baz'));
1557
        $this->assertSame($fooFieldDescription, $modelAdmin->getFilterFieldDescription('foo'));
1558
        $this->assertSame($barFieldDescription, $modelAdmin->getFilterFieldDescription('bar'));
1559
        $this->assertSame($bazFieldDescription, $modelAdmin->getFilterFieldDescription('baz'));
1560
    }
1561
1562
    public function testGetSubjectNoRequest()
1563
    {
1564
        $modelManager = $this->createMock('Sonata\AdminBundle\Model\ModelManagerInterface');
1565
        $modelManager
1566
            ->expects($this->never())
1567
            ->method('find');
1568
1569
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1570
        $admin->setModelManager($modelManager);
1571
1572
        $this->assertNull($admin->getSubject());
1573
    }
1574
1575
    public function testGetSideMenu()
1576
    {
1577
        $item = $this->createMock('Knp\Menu\ItemInterface');
1578
        $item
1579
            ->expects($this->once())
1580
            ->method('setChildrenAttribute')
1581
            ->with('class', 'nav navbar-nav');
1582
        $item
1583
            ->expects($this->once())
1584
            ->method('setExtra')
1585
            ->with('translation_domain', 'foo_bar_baz');
1586
1587
        $menuFactory = $this->createMock('Knp\Menu\FactoryInterface');
1588
        $menuFactory
1589
            ->expects($this->once())
1590
            ->method('createItem')
1591
            ->will($this->returnValue($item));
1592
1593
        $modelAdmin = new ModelAdmin('sonata.post.admin.model', 'Application\Sonata\FooBundle\Entity\Model', 'SonataFooBundle:ModelAdmin');
1594
        $modelAdmin->setMenuFactory($menuFactory);
1595
        $modelAdmin->setTranslationDomain('foo_bar_baz');
1596
1597
        $modelAdmin->getSideMenu('foo');
1598
    }
1599
1600
    /**
1601
     * @return array
1602
     */
1603
    public function provideGetSubject()
1604
    {
1605
        return array(
1606
            array(23),
1607
            array('azerty'),
1608
            array('4f69bbb5f14a13347f000092'),
1609
            array('0779ca8d-e2be-11e4-ac58-0242ac11000b'),
1610
            array('123'.AdapterInterface::ID_SEPARATOR.'my_type'), // composite keys are supported
1611
        );
1612
    }
1613
1614
    /**
1615
     * @dataProvider provideGetSubject
1616
     */
1617
    public function testGetSubjectFailed($id)
1618
    {
1619
        $modelManager = $this->createMock('Sonata\AdminBundle\Model\ModelManagerInterface');
1620
        $modelManager
1621
            ->expects($this->once())
1622
            ->method('find')
1623
            ->with('NewsBundle\Entity\Post', $id)
1624
            ->will($this->returnValue(null)); // entity not found
1625
1626
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1627
        $admin->setModelManager($modelManager);
1628
1629
        $admin->setRequest(new Request(array('id' => $id)));
1630
        $this->assertNull($admin->getSubject());
1631
    }
1632
1633
    /**
1634
     * @dataProvider provideGetSubject
1635
     */
1636
    public function testGetSubject($id)
1637
    {
1638
        $entity = new Post();
1639
1640
        $modelManager = $this->createMock('Sonata\AdminBundle\Model\ModelManagerInterface');
1641
        $modelManager
1642
            ->expects($this->once())
1643
            ->method('find')
1644
            ->with('NewsBundle\Entity\Post', $id)
1645
            ->will($this->returnValue($entity));
1646
1647
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1648
        $admin->setModelManager($modelManager);
1649
1650
        $admin->setRequest(new Request(array('id' => $id)));
1651
        $this->assertSame($entity, $admin->getSubject());
1652
        $this->assertSame($entity, $admin->getSubject()); // model manager must be used only once
1653
    }
1654
1655
    public function testGetSubjectWithParentDescription()
1656
    {
1657
        $adminId = 1;
1658
1659
        $comment = new Comment();
1660
1661
        $modelManager = $this->createMock('Sonata\AdminBundle\Model\ModelManagerInterface');
1662
        $modelManager
1663
            ->expects($this->any())
1664
            ->method('find')
1665
            ->with('NewsBundle\Entity\Comment', $adminId)
1666
            ->will($this->returnValue($comment));
1667
1668
        $request = new Request(array('id' => $adminId));
1669
1670
        $postAdmin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1671
        $postAdmin->setRequest($request);
1672
1673
        $commentAdmin = new CommentAdmin('sonata.post.admin.comment', 'NewsBundle\Entity\Comment', 'SonataNewsBundle:CommentAdmin');
1674
        $commentAdmin->setRequest($request);
1675
        $commentAdmin->setModelManager($modelManager);
1676
1677
        $this->assertEquals($comment, $commentAdmin->getSubject());
1678
1679
        $commentAdmin->setSubject(null);
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a object.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
1680
        $commentAdmin->setParentFieldDescription(new FieldDescription());
1681
1682
        $this->assertNull($commentAdmin->getSubject());
1683
    }
1684
1685
    /**
1686
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::configureActionButtons
1687
     */
1688
    public function testGetActionButtonsList()
1689
    {
1690
        $expected = array(
1691
            'create' => array(
1692
                'template' => 'Foo.html.twig',
1693
            ),
1694
        );
1695
1696
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1697
1698
        $securityHandler = $this->createMock('Sonata\AdminBundle\Security\Handler\SecurityHandlerInterface');
1699
        $securityHandler
1700
            ->expects($this->once())
1701
            ->method('isGranted')
1702
            ->with($admin, 'CREATE', $admin)
1703
            ->will($this->returnValue(true));
1704
        $admin->setSecurityHandler($securityHandler);
1705
1706
        $routeGenerator = $this->createMock('Sonata\AdminBundle\Route\RouteGeneratorInterface');
1707
        $routeGenerator
1708
            ->expects($this->once())
1709
            ->method('hasAdminRoute')
1710
            ->with($admin, 'create')
1711
            ->will($this->returnValue(true));
1712
        $admin->setRouteGenerator($routeGenerator);
1713
1714
        $admin->setTemplate('button_create', 'Foo.html.twig');
1715
1716
        $this->assertSame($expected, $admin->getActionButtons('list', null));
1717
    }
1718
1719
    /**
1720
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::configureActionButtons
1721
     */
1722
    public function testGetActionButtonsListCreateDisabled()
1723
    {
1724
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1725
1726
        $securityHandler = $this->createMock('Sonata\AdminBundle\Security\Handler\SecurityHandlerInterface');
1727
        $securityHandler
1728
            ->expects($this->once())
1729
            ->method('isGranted')
1730
            ->with($admin, 'CREATE', $admin)
1731
            ->will($this->returnValue(false));
1732
        $admin->setSecurityHandler($securityHandler);
1733
1734
        $this->assertSame(array(), $admin->getActionButtons('list', null));
1735
    }
1736
1737
    /**
1738
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::configureBatchActions
1739
     */
1740
    public function testGetBatchActions()
1741
    {
1742
        $expected = array(
1743
            'delete' => array(
1744
                'label' => 'action_delete',
1745
                'translation_domain' => 'SonataAdminBundle',
1746
                'ask_confirmation' => true, // by default always true
1747
            ),
1748
            'foo' => array(
1749
                'label' => 'action_foo',
1750
                'translation_domain' => 'SonataAdminBundle',
1751
            ),
1752
            'bar' => array(
1753
                'label' => 'batch.label_bar',
1754
                'translation_domain' => 'SonataAdminBundle',
1755
            ),
1756
            'baz' => array(
1757
                'label' => 'action_baz',
1758
                'translation_domain' => 'AcmeAdminBundle',
1759
            ),
1760
        );
1761
1762
        $pathInfo = new \Sonata\AdminBundle\Route\PathInfoBuilder($this->createMock('Sonata\AdminBundle\Model\AuditManagerInterface'));
1763
1764
        $labelTranslatorStrategy = $this->createMock('Sonata\AdminBundle\Translator\LabelTranslatorStrategyInterface');
1765
        $labelTranslatorStrategy->expects($this->any())
1766
            ->method('getLabel')
1767
            ->will($this->returnCallback(function ($label, $context = '', $type = '') {
1768
                return $context.'.'.$type.'_'.$label;
1769
            }));
1770
1771
        $admin = new PostAdmin('sonata.post.admin.model', 'Application\Sonata\FooBundle\Entity\Model', 'SonataFooBundle:ModelAdmin');
1772
        $admin->setRouteBuilder($pathInfo);
1773
        $admin->setTranslationDomain('SonataAdminBundle');
1774
        $admin->setLabelTranslatorStrategy($labelTranslatorStrategy);
1775
1776
        $routeGenerator = $this->createMock('Sonata\AdminBundle\Route\RouteGeneratorInterface');
1777
        $routeGenerator
1778
            ->expects($this->once())
1779
            ->method('hasAdminRoute')
1780
            ->with($admin, 'delete')
1781
            ->will($this->returnValue(true));
1782
        $admin->setRouteGenerator($routeGenerator);
1783
1784
        $securityHandler = $this->createMock('Sonata\AdminBundle\Security\Handler\SecurityHandlerInterface');
1785
        $securityHandler->expects($this->any())
1786
            ->method('isGranted')
1787
            ->will($this->returnCallback(function (AdminInterface $adminIn, $attributes, $object = null) use ($admin) {
0 ignored issues
show
Unused Code introduced by
The parameter $object is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
1788
                if ($admin == $adminIn && $attributes == 'DELETE') {
1789
                    return true;
1790
                }
1791
1792
                return false;
1793
            }));
1794
        $admin->setSecurityHandler($securityHandler);
1795
1796
        $this->assertSame($expected, $admin->getBatchActions());
1797
    }
1798
1799
    /**
1800
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::showMosaicButton
1801
     */
1802
    public function testShowMosaicButton()
1803
    {
1804
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1805
        $listModes = $admin->getListModes();
1806
1807
        $admin->showMosaicButton(true);
1808
1809
        $this->assertSame($listModes, $admin->getListModes());
1810
    }
1811
1812
    /**
1813
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::showMosaicButton
1814
     */
1815
    public function testShowMosaicButtonHideMosaic()
1816
    {
1817
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1818
        $listModes = $admin->getListModes();
1819
        $expected['list'] = $listModes['list'];
0 ignored issues
show
Coding Style Comprehensibility introduced by
$expected was never initialized. Although not strictly required by PHP, it is generally a good practice to add $expected = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
1820
1821
        $admin->showMosaicButton(false);
1822
1823
        $this->assertSame($expected, $admin->getListModes());
1824
    }
1825
1826
    /**
1827
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::getDashboardActions
1828
     * @dataProvider provideGetBaseRouteName
1829
     */
1830
    public function testDefaultDashboardActionsArePresent($objFqn, $expected)
1831
    {
1832
        $pathInfo = new \Sonata\AdminBundle\Route\PathInfoBuilder($this->createMock('Sonata\AdminBundle\Model\AuditManagerInterface'));
1833
1834
        $routeGenerator = new DefaultRouteGenerator(
1835
            $this->createMock('Symfony\Component\Routing\RouterInterface'),
1836
            new RoutesCache($this->cacheTempFolder, true)
1837
        );
1838
1839
        $admin = new PostAdmin('sonata.post.admin.post', $objFqn, 'SonataNewsBundle:PostAdmin');
1840
        $admin->setRouteBuilder($pathInfo);
1841
        $admin->setRouteGenerator($routeGenerator);
1842
        $admin->initialize();
1843
1844
        $securityHandler = $this->createMock('Sonata\AdminBundle\Security\Handler\SecurityHandlerInterface');
1845
        $securityHandler->expects($this->any())
1846
            ->method('isGranted')
1847
            ->will($this->returnCallback(function (AdminInterface $adminIn, $attributes, $object = null) use ($admin) {
0 ignored issues
show
Unused Code introduced by
The parameter $object is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
1848
                if ($admin == $adminIn && ($attributes == 'CREATE' || $attributes == 'LIST')) {
1849
                    return true;
1850
                }
1851
1852
                return false;
1853
            }));
1854
1855
        $admin->setSecurityHandler($securityHandler);
1856
1857
        $this->assertArrayHasKey('list', $admin->getDashboardActions());
1858
        $this->assertArrayHasKey('create', $admin->getDashboardActions());
1859
    }
1860
1861
    public function testDefaultFilters()
1862
    {
1863
        $admin = new FilteredAdmin('sonata.post.admin.model', 'Application\Sonata\FooBundle\Entity\Model', 'SonataFooBundle:ModelAdmin');
1864
1865
        $subjectId = uniqid();
1866
1867
        $request = $this->createMock('Symfony\Component\HttpFoundation\Request', array('get'));
1868
        $query = $this->createMock('Symfony\Component\HttpFoundation\ParameterBag', array('set', 'get'));
1869
        $query->expects($this->any())
1870
            ->method('get')
1871
            ->with($this->equalTo('filter'))
1872
            ->will($this->returnValue(array(
1873
                'a' => array(
1874
                    'value' => 'b',
1875
                ),
1876
                'foo' => array(
1877
                    'type' => '1',
1878
                    'value' => 'bar',
1879
                ),
1880
                'baz' => array(
1881
                    'type' => '5',
1882
                    'value' => 'test',
1883
                ),
1884
            )));
1885
        $request->query = $query;
1886
1887
        $request->expects($this->any())
1888
            ->method('get')
1889
            ->will($this->returnValue($subjectId));
1890
1891
        $admin->setRequest($request);
1892
1893
        $modelManager = $this->createMock('Sonata\AdminBundle\Model\ModelManagerInterface');
1894
        $modelManager->expects($this->any())
1895
            ->method('getDefaultSortValues')
1896
            ->will($this->returnValue(array()));
1897
1898
        $admin->setModelManager($modelManager);
1899
1900
        $this->assertEquals(array(
1901
            'foo' => array(
1902
                'type' => '1',
1903
                'value' => 'bar',
1904
            ),
1905
            'baz' => array(
1906
                'type' => '5',
1907
                'value' => 'test',
1908
            ),
1909
            '_page' => 1,
1910
            '_per_page' => 32,
1911
            'a' => array(
1912
                'value' => 'b',
1913
            ),
1914
        ), $admin->getFilterParameters());
1915
1916
        $this->assertTrue($admin->isDefaultFilter('foo'));
1917
        $this->assertFalse($admin->isDefaultFilter('bar'));
1918
        $this->assertFalse($admin->isDefaultFilter('a'));
1919
    }
1920
1921
    /**
1922
     * @group legacy
1923
     */
1924
    public function testDefaultBreadcrumbsBuilder()
1925
    {
1926
        $container = $this->createMock('Symfony\Component\DependencyInjection\ContainerInterface');
1927
        $container->expects($this->once())
1928
            ->method('getParameter')
1929
            ->with('sonata.admin.configuration.breadcrumbs')
1930
            ->will($this->returnValue(array()));
1931
1932
        $pool = $this->getMockBuilder('Sonata\AdminBundle\Admin\Pool')
1933
            ->disableOriginalConstructor()
1934
            ->getMock();
1935
        $pool->expects($this->once())
1936
            ->method('getContainer')
1937
            ->will($this->returnValue($container));
1938
1939
        $admin = $this->getMockForAbstractClass('Sonata\AdminBundle\Admin\AbstractAdmin', array(
1940
            'admin.my_code', 'My\Class', 'MyBundle:ClassAdmin',
1941
        ), '', true, true, true, array('getConfigurationPool'));
1942
        $admin->expects($this->once())
1943
            ->method('getConfigurationPool')
1944
            ->will($this->returnValue($pool));
1945
1946
        $this->assertInstanceOf(
1947
            'Sonata\AdminBundle\Admin\BreadcrumbsBuilder',
1948
            $admin->getBreadcrumbsBuilder()
1949
        );
1950
    }
1951
1952
    /**
1953
     * @group legacy
1954
     */
1955
    public function testBreadcrumbsBuilderSetter()
1956
    {
1957
        $admin = $this->getMockForAbstractClass('Sonata\AdminBundle\Admin\AbstractAdmin', array(
1958
            'admin.my_code', 'My\Class', 'MyBundle:ClassAdmin',
1959
        ));
1960
        $this->assertSame($admin, $admin->setBreadcrumbsBuilder($builder = $this->createMock(
1961
            'Sonata\AdminBundle\Admin\BreadcrumbsBuilderInterface'
1962
        )));
1963
        $this->assertSame($builder, $admin->getBreadcrumbsBuilder());
1964
    }
1965
1966
    /**
1967
     * @group legacy
1968
     */
1969
    public function testGetBreadcrumbs()
1970
    {
1971
        $admin = $this->getMockForAbstractClass('Sonata\AdminBundle\Admin\AbstractAdmin', array(
1972
            'admin.my_code', 'My\Class', 'MyBundle:ClassAdmin',
1973
        ));
1974
        $builder = $this->prophesize(
1975
            'Sonata\AdminBundle\Admin\BreadcrumbsBuilderInterface'
1976
        );
1977
        $action = 'myaction';
1978
        $builder->getBreadcrumbs($admin, $action)->shouldBeCalled();
1979
        $admin->setBreadcrumbsBuilder($builder->reveal())->getBreadcrumbs($action);
1980
    }
1981
1982
    /**
1983
     * @group legacy
1984
     */
1985
    public function testBuildBreadcrumbs()
1986
    {
1987
        $admin = $this->getMockForAbstractClass('Sonata\AdminBundle\Admin\AbstractAdmin', array(
1988
            'admin.my_code', 'My\Class', 'MyBundle:ClassAdmin',
1989
        ));
1990
        $builder = $this->prophesize(
1991
            'Sonata\AdminBundle\Admin\BreadcrumbsBuilderInterface'
1992
        );
1993
        $action = 'myaction';
1994
        $menu = $this->createMock('Knp\Menu\ItemInterface');
1995
        $builder->buildBreadcrumbs($admin, $action, $menu)
1996
            ->shouldBeCalledTimes(1)
1997
            ->willReturn($menu);
1998
        $admin->setBreadcrumbsBuilder($builder->reveal());
1999
2000
        /* check the called is proxied only once */
2001
        $this->assertSame($menu, $admin->buildBreadcrumbs($action, $menu));
2002
        $this->assertSame($menu, $admin->buildBreadcrumbs($action, $menu));
2003
    }
2004
2005
    /**
2006
     * NEXT_MAJOR: remove this method.
2007
     *
2008
     * @group legacy
2009
     */
2010
    public function testCreateQueryLegacyCallWorks()
2011
    {
2012
        $admin = $this->getMockForAbstractClass('Sonata\AdminBundle\Admin\AbstractAdmin', array(
2013
            'admin.my_code', 'My\Class', 'MyBundle:ClassAdmin',
2014
        ));
2015
        $modelManager = $this->createMock('Sonata\AdminBundle\Model\ModelManagerInterface');
2016
        $modelManager->expects($this->once())
2017
            ->method('createQuery')
2018
            ->with('My\Class')
2019
            ->willReturn('a query');
2020
2021
        $admin->setModelManager($modelManager);
2022
        $this->assertSame('a query', $admin->createQuery('list'));
2023
    }
2024
2025
    public function testGetDataSourceIterator()
2026
    {
2027
        $datagrid = $this->createMock('Sonata\AdminBundle\Datagrid\DatagridInterface');
2028
        $datagrid->method('buildPager');
2029
2030
        $modelManager = $this->createMock('Sonata\AdminBundle\Model\ModelManagerInterface');
2031
        $modelManager->method('getExportFields')->will($this->returnValue(array(
2032
            'field',
2033
            'foo',
2034
            'bar',
2035
        )));
2036
        $modelManager->expects($this->once())->method('getDataSourceIterator')
2037
            ->with($this->equalTo($datagrid), $this->equalTo(array(
2038
                'Feld' => 'field',
2039
                1 => 'foo',
2040
                2 => 'bar',
2041
            )));
2042
2043
        $admin = $this->getMockBuilder('Sonata\AdminBundle\Admin\AbstractAdmin')
2044
            ->disableOriginalConstructor()
2045
            ->setMethods(array('getDatagrid', 'getTranslationLabel', 'trans'))
2046
            ->getMockForAbstractClass();
2047
        $admin->method('getDatagrid')->will($this->returnValue($datagrid));
2048
        $admin->setModelManager($modelManager);
2049
2050
        $admin->expects($this->any())
2051
            ->method('getTranslationLabel')
2052
            ->will($this->returnCallback(function ($label, $context = '', $type = '') {
2053
                return $context.'.'.$type.'_'.$label;
2054
            }));
2055
        $admin->expects($this->any())
2056
            ->method('trans')
2057
            ->will($this->returnCallback(function ($label) {
2058
                if ($label == 'export.label_field') {
2059
                    return 'Feld';
2060
                }
2061
2062
                return $label;
2063
            }));
2064
2065
        $admin->getDataSourceIterator();
2066
    }
2067
2068
    private function createTagAdmin(Post $post)
2069
    {
2070
        $postAdmin = $this->getMockBuilder('Sonata\AdminBundle\Tests\Fixtures\Admin\PostAdmin')
2071
            ->disableOriginalConstructor()
2072
            ->getMock();
2073
2074
        $postAdmin->expects($this->any())->method('getObject')->will($this->returnValue($post));
2075
2076
        $formBuilder = $this->createMock('Symfony\Component\Form\FormBuilderInterface');
2077
        $formBuilder->expects($this->any())->method('getForm')->will($this->returnValue(null));
2078
2079
        $tagAdmin = $this->getMockBuilder('Sonata\AdminBundle\Tests\Fixtures\Admin\TagAdmin')
2080
            ->setConstructorArgs(array(
2081
                'admin.tag',
2082
                'Sonata\AdminBundle\Tests\Fixtures\Bundle\Entity\Tag',
2083
                'MyBundle:MyController',
2084
            ))
2085
            ->setMethods(array('getFormBuilder'))
2086
            ->getMock();
2087
2088
        $tagAdmin->expects($this->any())->method('getFormBuilder')->will($this->returnValue($formBuilder));
2089
        $tagAdmin->setParent($postAdmin);
2090
2091
        $tag = new Tag();
2092
        $tagAdmin->setSubject($tag);
2093
2094
        $request = $this->createMock('Symfony\Component\HttpFoundation\Request');
2095
        $tagAdmin->setRequest($request);
2096
2097
        $configurationPool = $this->getMockBuilder('Sonata\AdminBundle\Admin\Pool')
2098
            ->disableOriginalConstructor()
2099
            ->getMock();
2100
2101
        $configurationPool->expects($this->any())->method('getPropertyAccessor')->will($this->returnValue(PropertyAccess::createPropertyAccessor()));
2102
2103
        $tagAdmin->setConfigurationPool($configurationPool);
2104
2105
        return $tagAdmin;
2106
    }
2107
}
2108