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

JMSTranslatorBundle/AdminExtractorTest.php (6 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
27
/**
28
 * Test for AdminExtractor.
29
 *
30
 * NEXT_MAJOR: Remove this class.
31
 *
32
 * @group legacy
33
 *
34
 * @author Andrej Hudec <[email protected]>
35
 */
36
class AdminExtractorTest extends TestCase
37
{
38
    /**
39
     * @var AdminExtractor
40
     */
41
    private $adminExtractor;
42
43
    /**
44
     * @var Pool
45
     */
46
    private $pool;
47
48
    /**
49
     * @var AdminInterface
50
     */
51
    private $fooAdmin;
52
53
    /**
54
     * @var AdminInterface
55
     */
56
    private $barAdmin;
57
58
    /**
59
     * @var BreadcrumbsBuilderInterface
60
     */
61
    private $breadcrumbsBuilder;
62
63
    protected function setUp(): void
64
    {
65
        if (!interface_exists(ExtractorInterface::class)) {
66
            $this->markTestSkipped('JMS Translator Bundle does not exist');
67
        }
68
69
        $this->fooAdmin = $this->getMockForAbstractClass(AdminInterface::class);
70
        $this->barAdmin = $this->getMockForAbstractClass(AdminInterface::class);
71
72
        $container = $this->getMockForAbstractClass(ContainerInterface::class);
73
        $container
74
            ->method('get')
75
            ->willReturnCallback(function (string $id): AdminInterface {
76
                switch ($id) {
77
                    case 'foo_admin':
78
                        return $this->fooAdmin;
79
                    case 'bar_admin':
80
                        return $this->barAdmin;
81
                }
82
            });
83
84
        $logger = $this->getMockForAbstractClass(LoggerInterface::class);
85
86
        $this->pool = $this->getMockBuilder(Pool::class)
87
            ->disableOriginalConstructor()
88
            ->getMock();
89
        $this->pool
90
            ->method('getAdminServiceIds')
91
            ->willReturn(['foo_admin', 'bar_admin']);
92
        $this->pool
93
            ->method('getContainer')
94
            ->willReturn($container);
95
        $this->pool
96
            ->method('getAdminGroups')
97
            ->willReturn(['group' => [
98
                'label_catalogue' => 'admin_domain',
99
            ]]);
100
101
        $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.x. 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...
102
        $this->adminExtractor->setLogger($logger);
103
104
        $this->breadcrumbsBuilder = $this->getMockForAbstractClass(BreadcrumbsBuilderInterface::class);
105
        $this->adminExtractor->setBreadcrumbsBuilder($this->breadcrumbsBuilder);
106
    }
107
108
    public function testExtractEmpty(): void
109
    {
110
        $catalogue = $this->adminExtractor->extract();
111
112
        $this->assertInstanceOf(MessageCatalogue::class, $catalogue);
113
        $this->assertFalse($catalogue->has(new Message('foo', 'foo_admin_domain')));
114
    }
115
116
    public function testExtract(): void
117
    {
118
        $this->fooAdmin
0 ignored issues
show
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...
119
            ->method('getShow')
120
            ->willReturnCallback(function (): void {
121
                $this->assertSame('foo', $this->adminExtractor->trans('foo', [], 'foo_admin_domain'));
122
                $this->assertSame('foo', $this->adminExtractor->transChoice('foo', 1, [], 'foo_admin_domain'));
123
            });
124
        $this->fooAdmin
0 ignored issues
show
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...
125
            ->method('getLabel')
126
            ->willReturn('foo_label');
127
        $this->fooAdmin
0 ignored issues
show
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...
128
            ->method('getTranslationDomain')
129
            ->willReturn('foo_admin_domain');
130
131
        $catalogue = $this->adminExtractor->extract();
132
133
        $this->assertCount(2, $catalogue->getDomains());
134
135
        $this->assertTrue($catalogue->has(new Message('foo', 'foo_admin_domain')));
136
        $this->assertFalse($catalogue->has(new Message('nonexistent', 'foo_admin_domain')));
137
138
        $this->assertInstanceOf(Message::class, $catalogue->get('foo', 'foo_admin_domain'));
139
140
        $message = $catalogue->get('foo', 'foo_admin_domain');
141
        $this->assertSame('foo', $message->getId());
142
        $this->assertSame('foo_admin_domain', $message->getDomain());
143
144
        $this->assertTrue($catalogue->has(new Message('group', 'admin_domain')));
145
        $this->assertTrue($catalogue->has(new Message('foo_label', 'foo_admin_domain')));
146
    }
147
148
    public function testExtractWithException(): void
149
    {
150
        $this->expectException(\RuntimeException::class);
151
        $this->expectExceptionMessage('Foo throws exception');
152
153
        $this->fooAdmin
0 ignored issues
show
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...
154
            ->method('getShow')
155
            ->willReturnCallback(static function (): void {
156
                throw new \RuntimeException('Foo throws exception');
157
            });
158
159
        $this->adminExtractor->extract();
160
    }
161
162
    public function testExtractCallsBreadcrumbs(): void
163
    {
164
        $this->breadcrumbsBuilder->expects($this->exactly(2 * 6))
0 ignored issues
show
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...
165
            ->method('getBreadcrumbs');
166
        $this->adminExtractor->extract();
167
    }
168
}
169