ExtensionORMTest.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. namespace Gedmo\Mapping;
  3. use Doctrine\Common\EventManager;
  4. use Tool\BaseTestCaseORM;
  5. use Gedmo\Mapping\Mock\Extension\Encoder\EncoderListener;
  6. use Gedmo\Mapping\ExtensionMetadataFactory;
  7. use Doctrine\ORM\Event\LoadClassMetadataEventArgs;
  8. use Mapping\Fixture\User;
  9. class ExtensionORMTest extends BaseTestCaseORM
  10. {
  11. const USER = 'Mapping\\Fixture\\User';
  12. private $encoderListener;
  13. protected function setUp()
  14. {
  15. parent::setUp();
  16. require_once __DIR__.'/Mock/Extension/Encoder/Mapping/Annotations.php';
  17. $evm = new EventManager;
  18. $this->encoderListener = new EncoderListener;
  19. $evm->addEventSubscriber($this->encoderListener);
  20. $this->getMockSqliteEntityManager($evm);
  21. }
  22. public function testExtensionMetadata()
  23. {
  24. $meta = $this->em->getClassMetadata(self::USER);
  25. $config = $this->encoderListener->getConfiguration($this->em, self::USER);
  26. $this->assertArrayHasKey('encode', $config);
  27. $this->assertCount(2, $config['encode']);
  28. $this->assertArrayHasKey('name', $config['encode']);
  29. $options = $config['encode']['name'];
  30. $this->assertEquals('sha1', $options['type']);
  31. $this->assertEquals('xxx', $options['secret']);
  32. $this->assertArrayHasKey('password', $config['encode']);
  33. $options = $config['encode']['password'];
  34. $this->assertEquals('md5', $options['type']);
  35. $this->assertEmpty($options['secret']);
  36. }
  37. public function testGeneratedValues()
  38. {
  39. $user = new User;
  40. $user->setName('encode me');
  41. $user->setPassword('secret');
  42. $this->em->persist($user);
  43. $this->em->flush();
  44. $this->assertEquals('c12fead75b49a41d43804e8229cb049d3b91bf42', $user->getName());
  45. $this->assertEquals('5ebe2294ecd0e0f08eab7690d2a6ee69', $user->getPassword());
  46. }
  47. public function testEventAdapterUsed()
  48. {
  49. $mappedSubscriberClass = new \ReflectionClass('Gedmo\\Mapping\\MappedEventSubscriber');
  50. $getEventAdapterMethod = $mappedSubscriberClass->getMethod('getEventAdapter');
  51. $getEventAdapterMethod->setAccessible(true);
  52. $loadClassMetadataEventArgs = new LoadClassMetadataEventArgs(
  53. $this->em->getClassMetadata(self::USER),
  54. $this->em
  55. );
  56. $eventAdapter = $getEventAdapterMethod->invoke(
  57. $this->encoderListener,
  58. $loadClassMetadataEventArgs
  59. );
  60. $this->assertEquals('Gedmo\\Mapping\\Mock\\Extension\\Encoder\\Mapping\\Event\\Adapter\\ORM', get_class($eventAdapter));
  61. }
  62. protected function getUsedEntityFixtures()
  63. {
  64. return array(
  65. self::USER
  66. );
  67. }
  68. }