-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
265 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the Sonata Project package. | ||
* | ||
* (c) Thomas Rabaix <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Sonata\AdminBundle\Translator\Extractor; | ||
|
||
use Sonata\AdminBundle\Admin\BreadcrumbsBuilderInterface; | ||
use Sonata\AdminBundle\Admin\Pool; | ||
use Sonata\AdminBundle\Translator\LabelTranslatorStrategyInterface; | ||
use Symfony\Component\Translation\Extractor\ExtractorInterface; | ||
use Symfony\Component\Translation\MessageCatalogue; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final class AdminExtractor implements ExtractorInterface, LabelTranslatorStrategyInterface | ||
{ | ||
private const PUBLIC_ADMIN_METHODS = [ | ||
'getShow', | ||
'getDatagrid', | ||
'getList', | ||
'getForm', | ||
]; | ||
|
||
private const BREADCRUMB_ACTIONS = [ | ||
'list', | ||
'edit', | ||
'create', | ||
'update', | ||
'batch', | ||
'delete', | ||
]; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $prefix = ''; | ||
|
||
/** | ||
* @var MessageCatalogue|null | ||
*/ | ||
private $catalogue; | ||
|
||
/** | ||
* @var Pool | ||
*/ | ||
private $adminPool; | ||
|
||
/** | ||
* @var LabelTranslatorStrategyInterface|null | ||
*/ | ||
private $labelStrategy; | ||
|
||
/** | ||
* @var string|null | ||
*/ | ||
private $domain; | ||
|
||
/** | ||
* @var BreadcrumbsBuilderInterface | ||
*/ | ||
private $breadcrumbsBuilder; | ||
|
||
public function __construct(Pool $adminPool, BreadcrumbsBuilderInterface $breadcrumbsBuilder) | ||
{ | ||
$this->adminPool = $adminPool; | ||
$this->breadcrumbsBuilder = $breadcrumbsBuilder; | ||
} | ||
|
||
public function extract($resource, MessageCatalogue $catalogue) | ||
{ | ||
$this->catalogue = $catalogue; | ||
|
||
foreach ($this->adminPool->getAdminGroups() as $name => $group) { | ||
$catalogue->set($name, $this->prefix.$name, $group['label_catalogue']); | ||
} | ||
|
||
foreach ($this->adminPool->getAdminServiceIds() as $id) { | ||
$admin = $this->adminPool->getInstance($id); | ||
|
||
$this->labelStrategy = $admin->getLabelTranslatorStrategy(); | ||
$this->domain = $admin->getTranslationDomain(); | ||
|
||
$label = $admin->getLabel(); | ||
if (!empty($label)) { | ||
$catalogue->set($label, $this->prefix.$label, $admin->getTranslationDomain()); | ||
} | ||
|
||
$admin->setLabelTranslatorStrategy($this); | ||
|
||
foreach (self::PUBLIC_ADMIN_METHODS as $method) { | ||
$admin->$method(); | ||
} | ||
|
||
foreach (self::BREADCRUMB_ACTIONS as $action) { | ||
$this->breadcrumbsBuilder->getBreadcrumbs($admin, $action); | ||
} | ||
} | ||
} | ||
|
||
public function setPrefix($prefix): void | ||
{ | ||
$this->prefix = $prefix; | ||
} | ||
|
||
public function getLabel($label, $context = '', $type = ''): string | ||
{ | ||
$label = $this->labelStrategy->getLabel($label, $context, $type); | ||
|
||
$this->catalogue->set($label, $this->prefix.$label, $this->domain); | ||
|
||
return $label; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the Sonata Project package. | ||
* | ||
* (c) Thomas Rabaix <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Sonata\AdminBundle\Tests\Translator\Extractor; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Sonata\AdminBundle\Admin\AdminInterface; | ||
use Sonata\AdminBundle\Admin\BreadcrumbsBuilderInterface; | ||
use Sonata\AdminBundle\Admin\Pool; | ||
use Sonata\AdminBundle\Translator\Extractor\AdminExtractor; | ||
use Symfony\Component\DependencyInjection\Container; | ||
use Symfony\Component\Translation\MessageCatalogue; | ||
|
||
final class AdminExtractorTest extends TestCase | ||
{ | ||
/** | ||
* @var AdminExtractor | ||
*/ | ||
private $adminExtractor; | ||
|
||
/** | ||
* @var Pool | ||
*/ | ||
private $pool; | ||
|
||
/** | ||
* @var AdminInterface | ||
*/ | ||
private $fooAdmin; | ||
|
||
/** | ||
* @var AdminInterface | ||
*/ | ||
private $barAdmin; | ||
|
||
/** | ||
* @var BreadcrumbsBuilderInterface | ||
*/ | ||
private $breadcrumbsBuilder; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->fooAdmin = $this->createStub(AdminInterface::class); | ||
$this->barAdmin = $this->createStub(AdminInterface::class); | ||
|
||
$container = new Container(); | ||
$container->set('foo_admin', $this->fooAdmin); | ||
$container->set('bar_admin', $this->barAdmin); | ||
|
||
$this->pool = new Pool($container, 'title', 'logo_title'); | ||
$this->pool->setAdminServiceIds(['foo_admin', 'bar_admin']); | ||
$this->pool->setAdminGroups(['group' => [ | ||
'label_catalogue' => 'admin_domain', | ||
]]); | ||
|
||
$this->breadcrumbsBuilder = $this->createMock(BreadcrumbsBuilderInterface::class); | ||
$this->adminExtractor = new AdminExtractor($this->pool, $this->breadcrumbsBuilder); | ||
} | ||
|
||
public function testExtractEmpty(): void | ||
{ | ||
$catalogue = new MessageCatalogue('en'); | ||
|
||
$this->adminExtractor->extract([], $catalogue); | ||
$this->assertFalse($catalogue->has('foo', 'foo_admin_domain')); | ||
} | ||
|
||
public function testExtract(): void | ||
{ | ||
$this->fooAdmin | ||
->method('getLabel') | ||
->willReturn('foo_label'); | ||
$this->fooAdmin | ||
->method('getTranslationDomain') | ||
->willReturn('foo_admin_domain'); | ||
|
||
$catalogue = new MessageCatalogue('en'); | ||
|
||
$this->adminExtractor->extract([], $catalogue); | ||
|
||
$this->assertCount(2, $catalogue->getDomains()); | ||
$message = $catalogue->get('foo', 'foo_admin_domain'); | ||
$this->assertSame('foo', $message); | ||
|
||
$this->assertTrue($catalogue->has('group', 'admin_domain')); | ||
$this->assertTrue($catalogue->has('foo_label', 'foo_admin_domain')); | ||
} | ||
|
||
public function testExtractWithException(): void | ||
{ | ||
$this->fooAdmin | ||
->method('getShow') | ||
->willThrowException(new \RuntimeException('Foo throws exception')); | ||
|
||
$catalogue = new MessageCatalogue('en'); | ||
|
||
$this->expectException(\RuntimeException::class); | ||
$this->expectExceptionMessage('Foo throws exception'); | ||
|
||
$this->adminExtractor->extract([], $catalogue); | ||
} | ||
|
||
public function testExtractCallsBreadcrumbs(): void | ||
{ | ||
$numberOfAdmins = \count($this->pool->getAdminServiceIds()); | ||
$numberOfActionsToCheck = 6; | ||
|
||
$this->breadcrumbsBuilder->expects($this->exactly($numberOfAdmins * $numberOfActionsToCheck)) | ||
->method('getBreadcrumbs'); | ||
$catalogue = new MessageCatalogue('en'); | ||
|
||
$this->adminExtractor->extract([], $catalogue); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,6 +27,10 @@ | |
/** | ||
* Test for AdminExtractor. | ||
* | ||
* NEXT_MAJOR: Remove this class. | ||
* | ||
* @group legacy | ||
* | ||
* @author Andrej Hudec <[email protected]> | ||
*/ | ||
class AdminExtractorTest extends TestCase | ||
|