Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions Tests/Unit/DependencyInjection/DoctrineEncryptExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function testConfigLoadHalite()
$container = $this->createContainer();
$this->extension->load([[]], $container);

$this->assertSame(HaliteEncryptor::class, $container->getParameter('ambta_doctrine_encrypt.encryptor_class_name'));
self::assertSame(HaliteEncryptor::class, $container->getParameter('ambta_doctrine_encrypt.encryptor_class_name'));
}

public function testConfigLoadDefuse()
Expand All @@ -38,7 +38,7 @@ public function testConfigLoadDefuse()
];
$this->extension->load([$config], $container);

$this->assertSame(DefuseEncryptor::class, $container->getParameter('ambta_doctrine_encrypt.encryptor_class_name'));
self::assertSame(DefuseEncryptor::class, $container->getParameter('ambta_doctrine_encrypt.encryptor_class_name'));
}

public function testConfigLoadCustom()
Expand All @@ -49,17 +49,15 @@ public function testConfigLoadCustom()
];
$this->extension->load([$config], $container);

$this->markTestSkipped();
self::markTestSkipped();

$this->assertSame(self::class, $container->getParameter('ambta_doctrine_encrypt.encryptor_class_name'));
self::assertSame(self::class, $container->getParameter('ambta_doctrine_encrypt.encryptor_class_name'));
}

private function createContainer()
{
$container = new ContainerBuilder(
return new ContainerBuilder(
new ParameterBag(['kernel.debug' => false])
);

return $container;
}
}
135 changes: 75 additions & 60 deletions Tests/Unit/Subscribers/DoctrineEncryptSubscriberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,31 +30,26 @@ class DoctrineEncryptSubscriberTest extends TestCase
*/
private $encryptor;

/**
* @var Reader|MockObject
*/
private $reader;

protected function setUp()
{
$this->encryptor = $this->createMock(EncryptorInterface::class);
$this->encryptor
->expects($this->any())
->expects(self::any())
->method('encrypt')
->willReturnCallback(function (string $arg) {
return 'encrypted-'.$arg;
})
;
$this->encryptor
->expects($this->any())
->expects(self::any())
->method('decrypt')
->willReturnCallback(function (string $arg) {
return preg_replace('/^encrypted-/', '', $arg);
})
;

$this->reader = $this->createMock(Reader::class);
$this->reader->expects($this->any())
$reader = $this->createMock(Reader::class);
$reader->expects(self::any())
->method('getPropertyAnnotation')
->willReturnCallback(function (\ReflectionProperty $reflProperty, string $class) {
if (Encrypted::class === $class) {
Expand All @@ -68,126 +63,146 @@ protected function setUp()
})
;

$this->subscriber = new DoctrineEncryptSubscriber($this->reader, $this->encryptor);
$this->subscriber = new DoctrineEncryptSubscriber($reader, $this->encryptor);
}

public function testSetRestorEncryptor()
public function testSetRestoreEncryptor()
{
$replaceEncryptor = $this->createMock(EncryptorInterface::class);

$this->assertSame($this->encryptor, $this->subscriber->getEncryptor());
self::assertSame($this->encryptor, $this->subscriber->getEncryptor());
$this->subscriber->setEncryptor($replaceEncryptor);
$this->assertSame($replaceEncryptor, $this->subscriber->getEncryptor());
self::assertSame($replaceEncryptor, $this->subscriber->getEncryptor());
$this->subscriber->restoreEncryptor();
$this->assertSame($this->encryptor, $this->subscriber->getEncryptor());
self::assertSame($this->encryptor, $this->subscriber->getEncryptor());
}

public function testProcessFieldsEncrypt()
{
$user = new User('David', 'Switzerland');

$this->subscriber->processFields($user, true);
$em = $this->createMock(EntityManagerInterface::class);

$this->subscriber->processFields($user, $em, true);

$this->assertStringStartsWith('encrypted-', $user->name);
$this->assertStringStartsWith('encrypted-', $user->getAddress());
self::assertStringStartsWith('encrypted-', $user->name);
self::assertStringStartsWith('encrypted-', $user->getAddress());
}

public function testProcessFieldsEncryptExtend()
{
$user = new ExtendedUser('David', 'Switzerland', 'extra');

$this->subscriber->processFields($user, true);
$em = $this->createMock(EntityManagerInterface::class);

$this->subscriber->processFields($user, $em, true);

$this->assertStringStartsWith('encrypted-', $user->name);
$this->assertStringStartsWith('encrypted-', $user->getAddress());
$this->assertStringStartsWith('encrypted-', $user->extra);
self::assertStringStartsWith('encrypted-', $user->name);
self::assertStringStartsWith('encrypted-', $user->getAddress());
self::assertStringStartsWith('encrypted-', $user->extra);
}

public function testProcessFieldsEncryptEmbedded()
{
$withUser = new WithUser('Thing', 'foo', new User('David', 'Switzerland'));

$this->subscriber->processFields($withUser, true);
$em = $this->createMock(EntityManagerInterface::class);

$this->subscriber->processFields($withUser, $em, true);

$this->assertStringStartsWith('encrypted-', $withUser->name);
$this->assertSame('foo', $withUser->foo);
$this->assertStringStartsWith('encrypted-', $withUser->user->name);
$this->assertStringStartsWith('encrypted-', $withUser->user->getAddress());
self::assertStringStartsWith('encrypted-', $withUser->name);
self::assertSame('foo', $withUser->foo);
self::assertStringStartsWith('encrypted-', $withUser->user->name);
self::assertStringStartsWith('encrypted-', $withUser->user->getAddress());
}

public function testProcessFieldsEncryptNull()
{
$user = new User('David', null);

$this->subscriber->processFields($user, true);
$em = $this->createMock(EntityManagerInterface::class);

$this->subscriber->processFields($user, $em, true);

$this->assertStringStartsWith('encrypted-', $user->name);
$this->assertNull($user->getAddress());
self::assertStringStartsWith('encrypted-', $user->name);
self::assertNull($user->getAddress());
}

public function testProcessFieldsNoEncryptor()
{
$user = new User('David', 'Switzerland');

$em = $this->createMock(EntityManagerInterface::class);

$this->subscriber->setEncryptor(null);
$this->subscriber->processFields($user, true);
$this->subscriber->processFields($user, $em, true);

$this->assertSame('David', $user->name);
$this->assertSame('Switzerland', $user->getAddress());
self::assertSame('David', $user->name);
self::assertSame('Switzerland', $user->getAddress());
}

public function testProcessFieldsDecrypt()
{
$user = new User('encrypted-David<ENC>', 'encrypted-Switzerland<ENC>');

$this->subscriber->processFields($user, false);
$em = $this->createMock(EntityManagerInterface::class);

$this->subscriber->processFields($user, $em, false);

$this->assertSame('David', $user->name);
$this->assertSame('Switzerland', $user->getAddress());
self::assertSame('David', $user->name);
self::assertSame('Switzerland', $user->getAddress());
}

public function testProcessFieldsDecryptExtended()
{
$user = new ExtendedUser('encrypted-David<ENC>', 'encrypted-Switzerland<ENC>', 'encrypted-extra<ENC>');

$this->subscriber->processFields($user, false);
$em = $this->createMock(EntityManagerInterface::class);

$this->subscriber->processFields($user, $em, false);

$this->assertSame('David', $user->name);
$this->assertSame('Switzerland', $user->getAddress());
$this->assertSame('extra', $user->extra);
self::assertSame('David', $user->name);
self::assertSame('Switzerland', $user->getAddress());
self::assertSame('extra', $user->extra);
}

public function testProcessFieldsDecryptEmbedded()
{
$withUser = new WithUser('encrypted-Thing<ENC>', 'foo', new User('encrypted-David<ENC>', 'encrypted-Switzerland<ENC>'));

$this->subscriber->processFields($withUser, false);
$em = $this->createMock(EntityManagerInterface::class);

$this->assertSame('Thing', $withUser->name);
$this->assertSame('foo', $withUser->foo);
$this->assertSame('David', $withUser->user->name);
$this->assertSame('Switzerland', $withUser->user->getAddress());
$this->subscriber->processFields($withUser, $em, false);

self::assertSame('Thing', $withUser->name);
self::assertSame('foo', $withUser->foo);
self::assertSame('David', $withUser->user->name);
self::assertSame('Switzerland', $withUser->user->getAddress());
}

public function testProcessFieldsDecryptNull()
{
$user = new User('encrypted-David<ENC>', null);

$this->subscriber->processFields($user, false);
$em = $this->createMock(EntityManagerInterface::class);

$this->subscriber->processFields($user, $em, false);

$this->assertSame('David', $user->name);
$this->assertNull($user->getAddress());
self::assertSame('David', $user->name);
self::assertNull($user->getAddress());
}

public function testProcessFieldsDecryptNonEncrypted()
{
// no trailing <ENC> but somethint that our mock decrypt would change if called
$user = new User('encrypted-David', 'encrypted-Switzerland');

$this->subscriber->processFields($user, false);
$em = $this->createMock(EntityManagerInterface::class);

$this->subscriber->processFields($user, $em, false);

$this->assertSame('encrypted-David', $user->name);
$this->assertSame('encrypted-Switzerland', $user->getAddress());
self::assertSame('encrypted-David', $user->name);
self::assertSame('encrypted-Switzerland', $user->getAddress());
}

/**
Expand All @@ -198,25 +213,25 @@ public function testOnFlush()
$user = new User('David', 'Switzerland');

$uow = $this->createMock(UnitOfWork::class);
$uow->expects($this->any())
$uow->expects(self::any())
->method('getScheduledEntityInsertions')
->willReturn([$user])
;
$em = $this->createMock(EntityManagerInterface::class);
$em->expects($this->any())
$em->expects(self::any())
->method('getUnitOfWork')
->willReturn($uow)
;
$classMetaData = $this->createMock(ClassMetadata::class);
$em->expects($this->once())->method('getClassMetadata')->willReturn($classMetaData);
$uow->expects($this->once())->method('recomputeSingleEntityChangeSet');
$em->expects(self::any())->method('getClassMetadata')->willReturn($classMetaData);
$uow->expects(self::any())->method('recomputeSingleEntityChangeSet');

$onFlush = new OnFlushEventArgs($em);

$this->subscriber->onFlush($onFlush);

$this->assertStringStartsWith('encrypted-', $user->name);
$this->assertStringStartsWith('encrypted-', $user->getAddress());
self::assertStringStartsWith('encrypted-', $user->name);
self::assertStringStartsWith('encrypted-', $user->getAddress());
}

/**
Expand All @@ -227,20 +242,20 @@ public function testPostFlush()
$user = new User('encrypted-David<ENC>', 'encrypted-Switzerland<ENC>');

$uow = $this->createMock(UnitOfWork::class);
$uow->expects($this->any())
$uow->expects(self::any())
->method('getIdentityMap')
->willReturn([[$user]])
;
$em = $this->createMock(EntityManagerInterface::class);
$em->expects($this->any())
$em->expects(self::any())
->method('getUnitOfWork')
->willReturn($uow)
;
$postFlush = new PostFlushEventArgs($em);

$this->subscriber->postFlush($postFlush);

$this->assertSame('David', $user->name);
$this->assertSame('Switzerland', $user->getAddress());
self::assertSame('David', $user->name);
self::assertSame('Switzerland', $user->getAddress());
}
}
2 changes: 1 addition & 1 deletion src/Command/DoctrineEncryptDatabaseCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$output->writeln(sprintf('Processing <comment>%s</comment>', $metaData->name));
$progressBar = new ProgressBar($output, $totalCount);
foreach ($iterator as $row) {
$this->subscriber->processFields($row[0]);
$this->subscriber->processFields($row[0], $this->entityManager);

if (($i % $batchSize) === 0) {
$this->entityManager->flush();
Expand Down
Loading