Completed
Push — 3.x ( 38b337...555c81 )
by Jordi Sala
03:08
created

AdminExtractorTest::testExtract()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.6
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\AdminBundle\Tests\Translator\Extractor;
15
16
use PHPUnit\Framework\TestCase;
17
use Sonata\AdminBundle\Admin\AdminInterface;
18
use Sonata\AdminBundle\Admin\BreadcrumbsBuilderInterface;
19
use Sonata\AdminBundle\Admin\Pool;
20
use Sonata\AdminBundle\Translator\Extractor\AdminExtractor;
21
use Symfony\Component\DependencyInjection\Container;
22
use Symfony\Component\Translation\MessageCatalogue;
23
24
final class AdminExtractorTest extends TestCase
25
{
26
    /**
27
     * @var AdminExtractor
28
     */
29
    private $adminExtractor;
30
31
    /**
32
     * @var Pool
33
     */
34
    private $pool;
35
36
    /**
37
     * @var AdminInterface
38
     */
39
    private $fooAdmin;
40
41
    /**
42
     * @var AdminInterface
43
     */
44
    private $barAdmin;
45
46
    /**
47
     * @var BreadcrumbsBuilderInterface
48
     */
49
    private $breadcrumbsBuilder;
50
51
    protected function setUp(): void
52
    {
53
        $this->fooAdmin = $this->createStub(AdminInterface::class);
54
        $this->barAdmin = $this->createStub(AdminInterface::class);
55
56
        $container = new Container();
57
        $container->set('foo_admin', $this->fooAdmin);
58
        $container->set('bar_admin', $this->barAdmin);
59
60
        $this->pool = new Pool($container, 'title', 'logo_title');
61
        $this->pool->setAdminServiceIds(['foo_admin', 'bar_admin']);
62
        $this->pool->setAdminGroups(['group' => [
63
            'label_catalogue' => 'admin_domain',
64
        ]]);
65
66
        $this->breadcrumbsBuilder = $this->createMock(BreadcrumbsBuilderInterface::class);
67
        $this->adminExtractor = new AdminExtractor($this->pool, $this->breadcrumbsBuilder);
68
    }
69
70
    public function testExtractEmpty(): void
71
    {
72
        $catalogue = new MessageCatalogue('en');
73
74
        $this->adminExtractor->extract([], $catalogue);
75
        $this->assertFalse($catalogue->has('foo', 'foo_admin_domain'));
76
    }
77
78
    public function testExtract(): void
79
    {
80
        $this->fooAdmin
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
81
            ->method('getLabel')
82
            ->willReturn('foo_label');
83
        $this->fooAdmin
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
84
            ->method('getTranslationDomain')
85
            ->willReturn('foo_admin_domain');
86
87
        $catalogue = new MessageCatalogue('en');
88
89
        $this->adminExtractor->extract([], $catalogue);
90
91
        $this->assertCount(2, $catalogue->getDomains());
92
        $message = $catalogue->get('foo', 'foo_admin_domain');
93
        $this->assertSame('foo', $message);
94
95
        $this->assertTrue($catalogue->has('group', 'admin_domain'));
96
        $this->assertTrue($catalogue->has('foo_label', 'foo_admin_domain'));
97
    }
98
99
    public function testExtractWithException(): void
100
    {
101
        $this->fooAdmin
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
102
            ->method('getShow')
103
            ->willThrowException(new \RuntimeException('Foo throws exception'));
104
105
        $catalogue = new MessageCatalogue('en');
106
107
        $this->expectException(\RuntimeException::class);
108
        $this->expectExceptionMessage('Foo throws exception');
109
110
        $this->adminExtractor->extract([], $catalogue);
111
    }
112
113
    public function testExtractCallsBreadcrumbs(): void
114
    {
115
        $numberOfAdmins = \count($this->pool->getAdminServiceIds());
116
        $numberOfActionsToCheck = 6;
117
118
        $this->breadcrumbsBuilder->expects($this->exactly($numberOfAdmins * $numberOfActionsToCheck))
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Sonata\AdminBundl...crumbsBuilderInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
119
            ->method('getBreadcrumbs');
120
        $catalogue = new MessageCatalogue('en');
121
122
        $this->adminExtractor->extract([], $catalogue);
123
    }
124
}
125