-
-
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.
Audit readers MUST be tagged with this tag so we can use a service locator instead of injecting the whole container in the next major version.
- Loading branch information
Showing
9 changed files
with
279 additions
and
8 deletions.
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
53 changes: 53 additions & 0 deletions
53
src/DependencyInjection/Compiler/AddAuditReadersCompilerPass.php
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,53 @@ | ||
<?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\DependencyInjection\Compiler; | ||
|
||
use Sonata\AdminBundle\Model\AuditReaderInterface; | ||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\Compiler\ServiceLocatorTagPass; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Exception\LogicException; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
|
||
final class AddAuditReadersCompilerPass implements CompilerPassInterface | ||
{ | ||
public const AUDIT_READER_TAG = 'sonata.admin.audit_reader'; | ||
|
||
public function process(ContainerBuilder $container) | ||
{ | ||
if (!$container->has('sonata.admin.audit.manager')) { | ||
return; | ||
} | ||
|
||
$definition = $container->getDefinition('sonata.admin.audit.manager'); | ||
$readers = []; | ||
|
||
foreach ($container->findTaggedServiceIds(self::AUDIT_READER_TAG, true) as $id => $attributes) { | ||
$serviceDefinition = $container->getDefinition($id); | ||
|
||
if (!is_subclass_of($serviceDefinition->getClass(), AuditReaderInterface::class)) { | ||
throw new LogicException(sprintf( | ||
'Service "%s" MUST implement "%s".', | ||
$id, | ||
AuditReaderInterface::class | ||
)); | ||
} | ||
|
||
$readers[$id] = new Reference($id); | ||
} | ||
|
||
// NEXT_MAJOR: Change index from 1 to 0. | ||
$definition->replaceArgument(1, ServiceLocatorTagPass::register($container, $readers)); | ||
} | ||
} |
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
81 changes: 81 additions & 0 deletions
81
tests/DependencyInjection/Compiler/AddAuditReadersCompilerPassTest.php
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,81 @@ | ||
<?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\DependencyInjection\Compiler; | ||
|
||
use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractCompilerPassTestCase; | ||
use Sonata\AdminBundle\DependencyInjection\Compiler\AddAuditReadersCompilerPass; | ||
use Sonata\AdminBundle\Model\AuditManager; | ||
use Sonata\AdminBundle\Tests\Fixtures\Model\AuditReader; | ||
use Symfony\Component\DependencyInjection\Container; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Definition; | ||
use Symfony\Component\DependencyInjection\Exception\LogicException; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
|
||
final class AddAuditReadersCompilerPassTest extends AbstractCompilerPassTestCase | ||
{ | ||
public function testProcess(): void | ||
{ | ||
$auditManagerDefinition = new Definition(AuditManager::class, [ | ||
// NEXT_MAJOR: Remove next line. | ||
new Container(), | ||
null, | ||
]); | ||
|
||
$this->container | ||
->setDefinition('sonata.admin.audit.manager', $auditManagerDefinition); | ||
|
||
$auditReader = new Definition(AuditReader::class); | ||
$auditReader | ||
->addTag(AddAuditReadersCompilerPass::AUDIT_READER_TAG); | ||
|
||
$this->container | ||
->setDefinition('std_audit_reader', $auditReader); | ||
|
||
$this->compile(); | ||
|
||
$this->assertContainerBuilderHasServiceLocator( | ||
// NEXT_MAJOR: Change index from 1 to 0. | ||
(string) $this->container->getDefinition('sonata.admin.audit.manager')->getArgument(1), | ||
[ | ||
'std_audit_reader' => new Reference('std_audit_reader'), | ||
] | ||
); | ||
} | ||
|
||
public function testServiceTaggedMustImplementInterface(): void | ||
{ | ||
$auditManagerDefinition = new Definition(AuditManager::class); | ||
|
||
$this->container | ||
->setDefinition('sonata.admin.audit.manager', $auditManagerDefinition); | ||
|
||
$auditReader = new Definition(\stdClass::class); | ||
$auditReader | ||
->addTag(AddAuditReadersCompilerPass::AUDIT_READER_TAG); | ||
|
||
$this->container | ||
->setDefinition('std_audit_reader', $auditReader); | ||
|
||
$this->expectException(LogicException::class); | ||
$this->expectExceptionMessage('Service "std_audit_reader" must implement "Sonata\AdminBundle\Model\AuditReaderInterface".'); | ||
|
||
$this->compile(); | ||
} | ||
|
||
protected function registerCompilerPass(ContainerBuilder $container): void | ||
{ | ||
$container->addCompilerPass(new AddAuditReadersCompilerPass()); | ||
} | ||
} |
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,49 @@ | ||
<?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\Fixtures\Model; | ||
|
||
use Sonata\AdminBundle\Model\AuditReaderInterface; | ||
|
||
// NEXT_MAJOR: Add type declarations | ||
final class AuditReader implements AuditReaderInterface | ||
{ | ||
public function find($className, $id, $revision): object | ||
{ | ||
return new \stdClass(); | ||
} | ||
|
||
public function findRevisionHistory($className, $limit = 20, $offset = 0): array | ||
{ | ||
return [ | ||
new \stdClass(), | ||
]; | ||
} | ||
|
||
public function findRevision($classname, $revision): object | ||
{ | ||
return new \stdClass(); | ||
} | ||
|
||
public function findRevisions($className, $id): array | ||
{ | ||
return [ | ||
new \stdClass(), | ||
]; | ||
} | ||
|
||
public function diff($className, $id, $oldRevision, $newRevision): array | ||
{ | ||
return []; | ||
} | ||
} |
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 |
---|---|---|
|
@@ -16,21 +16,23 @@ | |
use PHPUnit\Framework\TestCase; | ||
use Sonata\AdminBundle\Model\AuditManager; | ||
use Sonata\AdminBundle\Model\AuditReaderInterface; | ||
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait; | ||
use Symfony\Component\DependencyInjection\Container; | ||
|
||
/** | ||
* Test for AuditManager. | ||
* | ||
* @author Andrej Hudec <[email protected]> | ||
*/ | ||
class AuditManagerTest extends TestCase | ||
final class AuditManagerTest extends TestCase | ||
{ | ||
// NEXT_MAJOR: Remove next line. | ||
use ExpectDeprecationTrait; | ||
|
||
public function testGetReader(): void | ||
{ | ||
$container = new Container(); | ||
|
||
$fooReader = $this->getMockForAbstractClass(AuditReaderInterface::class); | ||
$barReader = $this->getMockForAbstractClass(AuditReaderInterface::class); | ||
$fooReader = $this->createStub(AuditReaderInterface::class); | ||
$barReader = $this->createStub(AuditReaderInterface::class); | ||
|
||
$container->set('foo_reader', $fooReader); | ||
$container->set('bar_reader', $barReader); | ||
|
@@ -54,4 +56,24 @@ public function testGetReaderWithException(): void | |
|
||
$auditManager->getReader('Foo\Foo'); | ||
} | ||
|
||
/** | ||
* NEXT_MAJOR: Remove this test. | ||
* | ||
* @group legacy | ||
*/ | ||
public function testReaderShouldBeTagged(): void | ||
{ | ||
$container = new Container(); | ||
|
||
$fooReader = $this->createStub(AuditReaderInterface::class); | ||
|
||
$container->set('foo_reader', $fooReader); | ||
|
||
$auditManager = new AuditManager($container, new Container()); | ||
|
||
$this->expectDeprecation('Not registering the audit reader "foo_reader" with tag "sonata.admin.audit_reader" is deprecated since sonata-project/admin-bundle 3.x and will not work in 4.0. You MUST add "sonata.admin.audit_reader" tag to the service "foo_reader".'); | ||
|
||
$auditManager->setReader('foo_reader', ['Foo\Foo1', 'Foo\Foo2']); | ||
} | ||
} |