ExtensionODMTest.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. namespace Gedmo\Mapping;
  3. use Tool\BaseTestCaseMongoODM;
  4. use Doctrine\Common\EventManager;
  5. use Gedmo\Mapping\Mock\Extension\Encoder\EncoderListener;
  6. use Gedmo\Mapping\ExtensionMetadataFactory;
  7. use Doctrine\ODM\MongoDB\Event\LoadClassMetadataEventArgs;
  8. use Mapping\Fixture\Document\User;
  9. class ExtensionODMTest extends BaseTestCaseMongoODM
  10. {
  11. const USER = 'Mapping\\Fixture\\Document\\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->getMockDocumentManager($evm);
  21. }
  22. public function testExtensionMetadata()
  23. {
  24. $meta = $this->dm->getClassMetadata(self::USER);
  25. $config = $this->encoderListener->getConfiguration($this->dm, 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->dm->persist($user);
  43. $this->dm->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->dm->getClassMetadata(self::USER),
  54. $this->dm
  55. );
  56. $eventAdapter = $getEventAdapterMethod->invoke(
  57. $this->encoderListener,
  58. $loadClassMetadataEventArgs
  59. );
  60. $this->assertEquals('Gedmo\\Mapping\\Mock\\Extension\\Encoder\\Mapping\\Event\\Adapter\\ODM', get_class($eventAdapter));
  61. }
  62. }