Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace Bundle\AccountBundle\Documents;
- /*
- * This file is part of The OpenSky Project
- */
- /**
- * Description of Identity
- *
- * @author Matt Fitzgerald <matthewfitz@gmail.com>
- */
- /**
- * @Document(db="opensky", collection="identities")
- */
- class Identity
- {
- /** @Id */
- private $id;
- /** @Field */
- private $password;
- /**
- * @ReferenceMany(targetDocument="Bundle\AccountBundle\Documents\IdentityEmail")
- */
- private $emails;
- /**
- * @ReferenceMany(targetDocument="Bundle\AccountBundle\Documents\Address")
- */
- private $addresses;
- /*
- public function __construct() {
- $this->emails = new \Doctrine\Common\Collections\ArrayCollection();
- $this->addresses = new \Doctrine\Common\Collections\ArrayCollection();
- }
- */
- public function getId() {
- return $this->id;
- }
- public function getPassword() {
- return $this->password;
- }
- public function setPassword($password) {
- $this->password = md5((string) $password);
- return $this;
- }
- /**
- * Add email
- *
- * @param Bundle\AccountBundle\Documents\IdentityEmail $emails
- */
- public function addEmail(\Bundle\AccountBundle\Documents\IdentityEmail $email) {
- $this->emails[spl_object_hash($email)] = $email;
- return $this;
- }
- /**
- * Get emails
- *
- * @return Doctrine\Common\Collections\Collection $emails
- */
- public function getEmails() {
- return $this->emails;
- }
- /**
- * Remove email
- */
- public function removeEmail(\Bundle\AccountBundle\Documents\IdentityEmail $email) {
- $index = spl_object_hash($email);
- if (isset($this->emails[$index])) {
- unset($this->emails[$index]);
- }
- return $this;
- }
- /**
- * Add addresses
- *
- * @param Bundle\AccountBundle\Documents\Address $addresses
- */
- public function addAddress(\Bundle\AccountBundle\Documents\Address $address) {
- $this->addresses[spl_object_hash($address)] = $address;
- return $this;
- }
- /**
- * Get addresses
- *
- * @return Doctrine\Common\Collections\Collection $addresses
- */
- public function getAddresses() {
- return $this->addresses;
- }
- /**
- * Remove addresses
- */
- public function removeAddress(\Bundle\AccountBundle\Documents\Address $address) {
- $index = spl_object_hash($address);
- if (isset($this->addresses[$index])) {
- unset($this->addresses[$index]);
- }
- return $this;
- }
- }
- namespace Bundle\AccountBundle\Documents;
- /*
- * This file is part of The OpenSky Project
- */
- /**
- * Description of IdentityEmail
- *
- * @author Matt Fitzgerald <matthewfitz@gmail.com>
- */
- /**
- * @Document(db="opensky", collection="identity_emails")
- */
- class IdentityEmail {
- /**
- * @Id
- */
- private $id;
- /**
- * @Field
- */
- private $address;
- public function getId() {
- return $this->id;
- }
- public function setAddress($address) {
- $this->address = (string) $address;
- return $this;
- }
- public function getAddress() {
- return $this->address;
- }
- }
- namespace Bundle\AccountBundle\Documents;
- use Doctrine\ODM\MongoDB\Mongo;
- /*
- * This file is part of The OpenSky Project
- */
- /**
- * Description of IdentityTest
- *
- * @author Matt Fitzgerald <matthewfitz@gmail.com>
- */
- class IdentityTest extends \PHPUnit_Framework_TestCase
- {
- private $identity;
- private $documents = array(
- 'Bundle\AccountBundle\Documents\Identity',
- 'Bundle\AccountBundle\Documents\IdentityEmail',
- 'Bundle\AccountBundle\Documents\Address',
- );
- private $dm;
- protected function setUp() {
- $configuration = new \Doctrine\ODM\MongoDB\Configuration();
- $reader = new \Doctrine\Common\Annotations\AnnotationReader();
- $reader->setDefaultAnnotationNamespace('Doctrine\ODM\MongoDB\Mapping\\');
- $configuration->setMetadataDriverImpl(
- new \Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver($reader)
- );
- $configuration->setProxyDir(__DIR__ . '/../../../../account_manager/cache/local/Proxies');
- $configuration->setProxyNamespace('Proxies');
- $this->dm = \Doctrine\ODM\MongoDB\DocumentManager::create(new Mongo(), $configuration);
- $this->identity = new Identity();
- }
- public function testGetId() {
- $this->assertNull($this->identity->getId());
- }
- protected function tearDown() {
- $this->identity = null;
- foreach ($this->documents as $document) {
- $this->dm->getDocumentCollection($document)->drop();
- }
- }
- public function testPassword() {
- $documentClass = 'Bundle\AccountBundle\Documents\Identity';
- $identityPassword = '111111x';
- $simpleIdentity = new $documentClass();
- $simpleIdentity->setPassword($identityPassword);
- $this->dm->persist($simpleIdentity);
- $this->dm->flush();
- $loadedIdentity = $this->dm->find($documentClass, $simpleIdentity->getId());
- $this->assertNotNull($loadedIdentity);
- $this->assertEquals(md5($identityPassword), $loadedIdentity->getPassword());
- }
- public function testEmails() {
- $documentClass = 'Bundle\AccountBundle\Documents\Identity';
- $oneToManyClass = 'Bundle\AccountBundle\Documents\IdentityEmail';
- $identityPassword = '111111x';
- $oneEmailAddress = 'matthewfitz@gmail.com';
- $identity = new $documentClass();
- $identity->setPassword($identityPassword);
- $oneEmail = new $oneToManyClass();
- $oneEmail->setAddress($oneEmailAddress);
- $identity->addEmail($oneEmail);
- $this->dm->persist($identity);
- $this->dm->flush();
- $loadedIdentity = $this->dm->find($documentClass, $identity->getId());
- $this->assertNotNull($loadedIdentity);
- $this->assertEquals(1,count($loadedIdentity->getEmails()));
- foreach ($loadedIdentity->getEmails() as $email) {
- $this->assertEquals($oneEmailAddress, $email->getAddress());
- }
- $this->dm->persist($loadedIdentity);
- foreach ($loadedIdentity->getEmails() as $email) {
- $loadedIdentity->removeEmail($email);
- }
- $this->assertEquals(0,count($loadedIdentity->getEmails()));
- $this->dm->flush();
- $this->dm->detach($loadedIdentity);
- unset ($loadedIdentity);
- $this->assertFalse(isset($loadedIdentity));
- $loadedIdentity = $this->dm->find($documentClass, $identity->getId());
- $this->assertNotNull($loadedIdentity);
- $this->assertEquals(0,count($loadedIdentity->getEmails()));
- }
- public function testAddresses() {
- $documentClass = 'Bundle\AccountBundle\Documents\Identity';
- $oneToManyClass = 'Bundle\AccountBundle\Documents\Address';
- $identityPassword = '111111x';
- $firstname = 'Matt Fitz';
- $identity = new $documentClass();
- $identity->setPassword($identityPassword);
- $oneAddress = new $oneToManyClass();
- $oneAddress->setFirstname($firstname);
- $identity->addAddress($oneAddress);
- $this->dm->persist($identity);
- $this->dm->flush();
- $loadedIdentity = $this->dm->find($documentClass, $identity->getId());
- $this->assertNotNull($loadedIdentity);
- $this->assertEquals(1,count($loadedIdentity->getAddresses()));
- foreach ($loadedIdentity->getAddresses() as $address) {
- $this->assertEquals($firstname, $address->getFirstname());
- }
- $this->dm->persist($loadedIdentity);
- foreach ($loadedIdentity->getAddresses() as $address) {
- $loadedIdentity->removeAddress($address);
- }
- $this->dm->flush();
- $this->dm->detach($loadedIdentity);
- unset ($loadedIdentity);
- $this->assertFalse(isset($loadedIdentity));
- $loadedIdentity = $this->dm->find($documentClass, $identity->getId());
- $this->assertNotNull($loadedIdentity);
- $this->assertEquals(0,count($loadedIdentity->getAddresses()));
- }
- }
Add Comment
Please, Sign In to add comment