Completed
Pull Request — 3.x (#6263)
by
unknown
53:39
created

AdminExtractorTest::setUp()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 48

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 48
rs 8.8234
c 0
b 0
f 0
cc 5
nc 4
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\JMSTranslatorBundle;
15
16
use JMS\TranslationBundle\Model\Message;
17
use JMS\TranslationBundle\Model\MessageCatalogue;
18
use JMS\TranslationBundle\Translation\ExtractorInterface;
19
use PHPUnit\Framework\TestCase;
20
use Psr\Log\LoggerInterface;
21
use Sonata\AdminBundle\Admin\AdminInterface;
22
use Sonata\AdminBundle\Admin\BreadcrumbsBuilderInterface;
23
use Sonata\AdminBundle\Admin\Pool;
24
use Sonata\AdminBundle\Translator\Extractor\JMSTranslatorBundle\AdminExtractor;
25
use Symfony\Component\DependencyInjection\ContainerInterface;
26
use Symfony\Component\Translation\TranslatorInterface;
27
28
/**
29
 * Test for AdminExtractor.
30
 *
31
 * NEXT_MAJOR: Remove this class.
32
 *
33
 * @group legacy
34
 *
35
 * @author Andrej Hudec <[email protected]>
36
 */
37
class AdminExtractorTest extends TestCase
38
{
39
    /**
40
     * @var AdminExtractor
41
     */
42
    private $adminExtractor;
43
44
    /**
45
     * @var Pool
46
     */
47
    private $pool;
48
49
    /**
50
     * @var AdminInterface
51
     */
52
    private $fooAdmin;
53
54
    /**
55
     * @var AdminInterface
56
     */
57
    private $barAdmin;
58
59
    /**
60
     * @var BreadcrumbsBuilderInterface
61
     */
62
    private $breadcrumbsBuilder;
63
64
    protected function setUp(): void
65
    {
66
        if (!interface_exists(TranslatorInterface::class)) {
67
            $this->markTestSkipped('This test is only available using Symfony 4');
68
        }
69
70
        if (!interface_exists(ExtractorInterface::class)) {
71
            $this->markTestSkipped('JMS Translator Bundle does not exist');
72
        }
73
74
        $this->fooAdmin = $this->getMockForAbstractClass(AdminInterface::class);
75
        $this->barAdmin = $this->getMockForAbstractClass(AdminInterface::class);
76
77
        $container = $this->getMockForAbstractClass(ContainerInterface::class);
78
        $container
79
            ->method('get')
80
            ->willReturnCallback(function (string $id): AdminInterface {
81
                switch ($id) {
82
                    case 'foo_admin':
83
                        return $this->fooAdmin;
84
                    case 'bar_admin':
85
                        return $this->barAdmin;
86
                }
87
            });
88
89
        $logger = $this->getMockForAbstractClass(LoggerInterface::class);
90
91
        $this->pool = $this->getMockBuilder(Pool::class)
92
            ->disableOriginalConstructor()
93
            ->getMock();
94
        $this->pool
95
            ->method('getAdminServiceIds')
96
            ->willReturn(['foo_admin', 'bar_admin']);
97
        $this->pool
98
            ->method('getContainer')
99
            ->willReturn($container);
100
        $this->pool
101
            ->method('getAdminGroups')
102
            ->willReturn(['group' => [
103
                'label_catalogue' => 'admin_domain',
104
            ]]);
105
106
        $this->adminExtractor = new AdminExtractor($this->pool, $logger);
0 ignored issues
show
Deprecated Code introduced by
The class Sonata\AdminBundle\Trans...orBundle\AdminExtractor has been deprecated with message: since sonata-project/admin-bundle 3.72. Use `translation:update` Symfony command instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

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

Loading history...
107
        $this->adminExtractor->setLogger($logger);
108
109
        $this->breadcrumbsBuilder = $this->getMockForAbstractClass(BreadcrumbsBuilderInterface::class);
110
        $this->adminExtractor->setBreadcrumbsBuilder($this->breadcrumbsBuilder);
111
    }
112
113
    public function testExtractEmpty(): void
114
    {
115
        $catalogue = $this->adminExtractor->extract();
116
117
        $this->assertInstanceOf(MessageCatalogue::class, $catalogue);
118
        $this->assertFalse($catalogue->has(new Message('foo', 'foo_admin_domain')));
119
    }
120
121
    public function testExtract(): void
122
    {
123
        $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...
124
            ->method('getShow')
125
            ->willReturnCallback(function (): void {
126
                $this->assertSame('foo', $this->adminExtractor->trans('foo', [], 'foo_admin_domain'));
127
                $this->assertSame('foo', $this->adminExtractor->transChoice('foo', 1, [], 'foo_admin_domain'));
128
            });
129
        $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...
130
            ->method('getLabel')
131
            ->willReturn('foo_label');
132
        $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...
133
            ->method('getTranslationDomain')
134
            ->willReturn('foo_admin_domain');
135
136
        $catalogue = $this->adminExtractor->extract();
137
138
        $this->assertCount(2, $catalogue->getDomains());
139
140
        $this->assertTrue($catalogue->has(new Message('foo', 'foo_admin_domain')));
141
        $this->assertFalse($catalogue->has(new Message('nonexistent', 'foo_admin_domain')));
142
143
        $this->assertInstanceOf(Message::class, $catalogue->get('foo', 'foo_admin_domain'));
144
145
        $message = $catalogue->get('foo', 'foo_admin_domain');
146
        $this->assertSame('foo', $message->getId());
147
        $this->assertSame('foo_admin_domain', $message->getDomain());
148
149
        $this->assertTrue($catalogue->has(new Message('group', 'admin_domain')));
150
        $this->assertTrue($catalogue->has(new Message('foo_label', 'foo_admin_domain')));
151
    }
152
153
    public function testExtractWithException(): void
154
    {
155
        $this->expectException(\RuntimeException::class);
156
        $this->expectExceptionMessage('Foo throws exception');
157
158
        $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...
159
            ->method('getShow')
160
            ->willReturnCallback(static function (): void {
161
                throw new \RuntimeException('Foo throws exception');
162
            });
163
164
        $this->adminExtractor->extract();
165
    }
166
167
    public function testExtractCallsBreadcrumbs(): void
168
    {
169
        $this->breadcrumbsBuilder->expects($this->exactly(2 * 6))
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...
170
            ->method('getBreadcrumbs');
171
        $this->adminExtractor->extract();
172
    }
173
}
174