Guest User

Untitled

a guest
Jul 29th, 2018
369
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.38 KB | None | 0 0
  1. namespace Bundle\AccountBundle\Documents;
  2.  
  3. /*
  4. * This file is part of The OpenSky Project
  5. */
  6.  
  7. /**
  8. * Description of Identity
  9. *
  10. * @author Matt Fitzgerald <matthewfitz@gmail.com>
  11. */
  12.  
  13. /**
  14. * @Document(db="opensky", collection="identities")
  15. */
  16. class Identity
  17. {
  18.  
  19. /** @Id */
  20. private $id;
  21.  
  22. /** @Field */
  23. private $password;
  24.  
  25. /**
  26. * @ReferenceMany(targetDocument="Bundle\AccountBundle\Documents\IdentityEmail")
  27. */
  28. private $emails;
  29.  
  30. /**
  31. * @ReferenceMany(targetDocument="Bundle\AccountBundle\Documents\Address")
  32. */
  33. private $addresses;
  34. /*
  35. public function __construct() {
  36. $this->emails = new \Doctrine\Common\Collections\ArrayCollection();
  37. $this->addresses = new \Doctrine\Common\Collections\ArrayCollection();
  38. }
  39. */
  40. public function getId() {
  41. return $this->id;
  42. }
  43.  
  44. public function getPassword() {
  45. return $this->password;
  46. }
  47.  
  48. public function setPassword($password) {
  49. $this->password = md5((string) $password);
  50. return $this;
  51. }
  52.  
  53. /**
  54. * Add email
  55. *
  56. * @param Bundle\AccountBundle\Documents\IdentityEmail $emails
  57. */
  58. public function addEmail(\Bundle\AccountBundle\Documents\IdentityEmail $email) {
  59. $this->emails[spl_object_hash($email)] = $email;
  60. return $this;
  61. }
  62.  
  63. /**
  64. * Get emails
  65. *
  66. * @return Doctrine\Common\Collections\Collection $emails
  67. */
  68. public function getEmails() {
  69. return $this->emails;
  70. }
  71.  
  72. /**
  73. * Remove email
  74. */
  75. public function removeEmail(\Bundle\AccountBundle\Documents\IdentityEmail $email) {
  76. $index = spl_object_hash($email);
  77. if (isset($this->emails[$index])) {
  78. unset($this->emails[$index]);
  79. }
  80. return $this;
  81. }
  82.  
  83. /**
  84. * Add addresses
  85. *
  86. * @param Bundle\AccountBundle\Documents\Address $addresses
  87. */
  88. public function addAddress(\Bundle\AccountBundle\Documents\Address $address) {
  89. $this->addresses[spl_object_hash($address)] = $address;
  90. return $this;
  91. }
  92.  
  93. /**
  94. * Get addresses
  95. *
  96. * @return Doctrine\Common\Collections\Collection $addresses
  97. */
  98. public function getAddresses() {
  99. return $this->addresses;
  100. }
  101.  
  102. /**
  103. * Remove addresses
  104. */
  105. public function removeAddress(\Bundle\AccountBundle\Documents\Address $address) {
  106. $index = spl_object_hash($address);
  107. if (isset($this->addresses[$index])) {
  108. unset($this->addresses[$index]);
  109. }
  110. return $this;
  111. }
  112. }
  113.  
  114. namespace Bundle\AccountBundle\Documents;
  115.  
  116. /*
  117. * This file is part of The OpenSky Project
  118. */
  119.  
  120. /**
  121. * Description of IdentityEmail
  122. *
  123. * @author Matt Fitzgerald <matthewfitz@gmail.com>
  124. */
  125.  
  126. /**
  127. * @Document(db="opensky", collection="identity_emails")
  128. */
  129. class IdentityEmail {
  130.  
  131. /**
  132. * @Id
  133. */
  134. private $id;
  135.  
  136. /**
  137. * @Field
  138. */
  139. private $address;
  140.  
  141. public function getId() {
  142. return $this->id;
  143. }
  144.  
  145. public function setAddress($address) {
  146. $this->address = (string) $address;
  147. return $this;
  148. }
  149.  
  150. public function getAddress() {
  151. return $this->address;
  152. }
  153. }
  154.  
  155. namespace Bundle\AccountBundle\Documents;
  156.  
  157. use Doctrine\ODM\MongoDB\Mongo;
  158.  
  159. /*
  160. * This file is part of The OpenSky Project
  161. */
  162.  
  163. /**
  164. * Description of IdentityTest
  165. *
  166. * @author Matt Fitzgerald <matthewfitz@gmail.com>
  167. */
  168. class IdentityTest extends \PHPUnit_Framework_TestCase
  169. {
  170.  
  171. private $identity;
  172.  
  173. private $documents = array(
  174. 'Bundle\AccountBundle\Documents\Identity',
  175. 'Bundle\AccountBundle\Documents\IdentityEmail',
  176. 'Bundle\AccountBundle\Documents\Address',
  177. );
  178.  
  179. private $dm;
  180.  
  181. protected function setUp() {
  182. $configuration = new \Doctrine\ODM\MongoDB\Configuration();
  183. $reader = new \Doctrine\Common\Annotations\AnnotationReader();
  184. $reader->setDefaultAnnotationNamespace('Doctrine\ODM\MongoDB\Mapping\\');
  185. $configuration->setMetadataDriverImpl(
  186. new \Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver($reader)
  187. );
  188. $configuration->setProxyDir(__DIR__ . '/../../../../account_manager/cache/local/Proxies');
  189. $configuration->setProxyNamespace('Proxies');
  190. $this->dm = \Doctrine\ODM\MongoDB\DocumentManager::create(new Mongo(), $configuration);
  191. $this->identity = new Identity();
  192. }
  193.  
  194. public function testGetId() {
  195. $this->assertNull($this->identity->getId());
  196. }
  197.  
  198. protected function tearDown() {
  199. $this->identity = null;
  200. foreach ($this->documents as $document) {
  201. $this->dm->getDocumentCollection($document)->drop();
  202. }
  203. }
  204.  
  205. public function testPassword() {
  206. $documentClass = 'Bundle\AccountBundle\Documents\Identity';
  207. $identityPassword = '111111x';
  208.  
  209. $simpleIdentity = new $documentClass();
  210. $simpleIdentity->setPassword($identityPassword);
  211.  
  212. $this->dm->persist($simpleIdentity);
  213. $this->dm->flush();
  214. $loadedIdentity = $this->dm->find($documentClass, $simpleIdentity->getId());
  215. $this->assertNotNull($loadedIdentity);
  216. $this->assertEquals(md5($identityPassword), $loadedIdentity->getPassword());
  217. }
  218.  
  219. public function testEmails() {
  220. $documentClass = 'Bundle\AccountBundle\Documents\Identity';
  221. $oneToManyClass = 'Bundle\AccountBundle\Documents\IdentityEmail';
  222.  
  223. $identityPassword = '111111x';
  224. $oneEmailAddress = 'matthewfitz@gmail.com';
  225.  
  226. $identity = new $documentClass();
  227. $identity->setPassword($identityPassword);
  228.  
  229. $oneEmail = new $oneToManyClass();
  230. $oneEmail->setAddress($oneEmailAddress);
  231.  
  232. $identity->addEmail($oneEmail);
  233.  
  234. $this->dm->persist($identity);
  235. $this->dm->flush();
  236.  
  237. $loadedIdentity = $this->dm->find($documentClass, $identity->getId());
  238. $this->assertNotNull($loadedIdentity);
  239. $this->assertEquals(1,count($loadedIdentity->getEmails()));
  240.  
  241. foreach ($loadedIdentity->getEmails() as $email) {
  242. $this->assertEquals($oneEmailAddress, $email->getAddress());
  243. }
  244.  
  245. $this->dm->persist($loadedIdentity);
  246.  
  247. foreach ($loadedIdentity->getEmails() as $email) {
  248. $loadedIdentity->removeEmail($email);
  249. }
  250. $this->assertEquals(0,count($loadedIdentity->getEmails()));
  251. $this->dm->flush();
  252.  
  253. $this->dm->detach($loadedIdentity);
  254. unset ($loadedIdentity);
  255.  
  256. $this->assertFalse(isset($loadedIdentity));
  257.  
  258. $loadedIdentity = $this->dm->find($documentClass, $identity->getId());
  259. $this->assertNotNull($loadedIdentity);
  260. $this->assertEquals(0,count($loadedIdentity->getEmails()));
  261.  
  262. }
  263.  
  264. public function testAddresses() {
  265. $documentClass = 'Bundle\AccountBundle\Documents\Identity';
  266. $oneToManyClass = 'Bundle\AccountBundle\Documents\Address';
  267.  
  268. $identityPassword = '111111x';
  269. $firstname = 'Matt Fitz';
  270.  
  271. $identity = new $documentClass();
  272. $identity->setPassword($identityPassword);
  273.  
  274. $oneAddress = new $oneToManyClass();
  275. $oneAddress->setFirstname($firstname);
  276.  
  277. $identity->addAddress($oneAddress);
  278.  
  279. $this->dm->persist($identity);
  280. $this->dm->flush();
  281.  
  282. $loadedIdentity = $this->dm->find($documentClass, $identity->getId());
  283. $this->assertNotNull($loadedIdentity);
  284. $this->assertEquals(1,count($loadedIdentity->getAddresses()));
  285.  
  286. foreach ($loadedIdentity->getAddresses() as $address) {
  287. $this->assertEquals($firstname, $address->getFirstname());
  288. }
  289.  
  290. $this->dm->persist($loadedIdentity);
  291.  
  292. foreach ($loadedIdentity->getAddresses() as $address) {
  293. $loadedIdentity->removeAddress($address);
  294. }
  295. $this->dm->flush();
  296.  
  297. $this->dm->detach($loadedIdentity);
  298. unset ($loadedIdentity);
  299.  
  300. $this->assertFalse(isset($loadedIdentity));
  301.  
  302. $loadedIdentity = $this->dm->find($documentClass, $identity->getId());
  303. $this->assertNotNull($loadedIdentity);
  304. $this->assertEquals(0,count($loadedIdentity->getAddresses()));
  305.  
  306. }
  307.  
  308. }
Add Comment
Please, Sign In to add comment