Exemplo n.º 1
0
 public function testPostElementRequest()
 {
     $dm = $this->_getDrestManager($this->_em);
     $representation = new \DrestCommon\Representation\Json();
     // id's added for comparison but not used for persistence (see DrestTests\Entities\Typical\User::populatePost())
     $user = array('id' => 1, 'username' => 'leedavis81', 'email_address' => '*****@*****.**', 'phone_numbers' => array(array('id' => 1, 'number' => '02087856589'), array('id' => 2, 'number' => '07584565445'), array('id' => 3, 'number' => '02078545896')));
     $representation->write(\DrestCommon\ResultSet::create($user, 'user'));
     $request = \Symfony\Component\HttpFoundation\Request::create('/user', 'POST', [], [], [], array('HTTP_CONTENT_TYPE' => $representation->getContentType()), $representation->__toString());
     $response = $dm->dispatch($request);
     $this->assertEquals(201, $response->getStatusCode());
     $this->assertEquals($dm->getRequest()->getUrl() . '/user/1', $response->getHttpHeader('Location'));
     // Ensure this item exists in persistence
     $query = $this->_em->createQuery('SELECT u, p FROM DrestTests\\Entities\\Typical\\User u JOIN u.phone_numbers p');
     $this->assertEquals($user, $query->getSingleResult(\Doctrine\ORM\Query::HYDRATE_ARRAY));
 }
Exemplo n.º 2
0
 public function testPutElementRequest()
 {
     $dm = $this->_getDrestManager($this->_em);
     $representation = new \DrestCommon\Representation\Json();
     $user = new \DrestTests\Entities\CMS\User();
     $user->setEmailAddress('*****@*****.**');
     $user->setUsername('leedavis81');
     $this->_em->persist($user);
     $this->_em->flush();
     $this->_em->refresh($user);
     $putEmail = '*****@*****.**';
     $representation->write(\DrestCommon\ResultSet::create(array('email_address' => $putEmail), 'user'));
     $request = \Symfony\Component\HttpFoundation\Request::create('/user/' . $user->getId(), 'PUT', array(), array(), array(), array('HTTP_CONTENT_TYPE' => $representation->getContentType()), $representation->__toString());
     $response = $dm->dispatch($request);
     $this->assertEquals(200, $response->getStatusCode());
     $putUser = $this->_em->find('DrestTests\\Entities\\CMS\\User', $user->getId());
     $this->assertEquals($putEmail, $putUser->getEmailAddress());
 }
Exemplo n.º 3
0
 public function testNoRouteMatchExceptionReturnedInAcceptedFormat()
 {
     $config = new Configuration();
     $config->setMetadataCacheImpl(new \Doctrine\Common\Cache\ArrayCache());
     $config->addPathsToConfigFiles(array(__DIR__ . '/../Entities/Typical'));
     $config->setDefaultRepresentations(array('Json'));
     $config->setDebugMode(false);
     // Triggers an actual return document
     $dm = $this->_getDrestManager(null, $config);
     $representation = new \DrestCommon\Representation\Json();
     $request = \Symfony\Component\HttpFoundation\Request::create('/this-doesnt-exist', 'GET', [], [], [], array('HTTP_ACCEPT' => $representation->getContentType()));
     /** @var \DrestCommon\Response\Response $response */
     $response = $dm->dispatch($request);
     $this->assertInstanceOf('DrestCommon\\Response\\Response', $response);
     // Ensure we can decode as a json string
     $responseDocument = json_decode($response->getBody(), true);
     $this->assertTrue(isset($responseDocument['error']));
 }
Exemplo n.º 4
0
 /**
  * @expectedException \Doctrine\ORM\ORMException
  */
 public function testDeleteElementRequest()
 {
     $dm = $this->_getDrestManager($this->_em);
     $representation = new \DrestCommon\Representation\Json();
     $user = new \DrestTests\Entities\CMS\User();
     $email = '*****@*****.**';
     $username = '******';
     $user->setEmailAddress($email);
     $user->setUsername($username);
     $this->_em->persist($user);
     $this->_em->flush();
     $this->_em->refresh($user);
     // Ensure they've been written to storage
     $entity = $this->_em->getRepository('DrestTests\\Entities\\CMS\\User')->find($user->getId());
     $this->assertEquals($entity, $user);
     $request = \Symfony\Component\HttpFoundation\Request::create('/user/' . $user->getId(), 'DELETE', array(), array(), array(), array('HTTP_ACCEPT' => $representation->getContentType()));
     $response = $dm->dispatch($request);
     $this->assertEquals(200, $response->getStatusCode());
     $deletedEntity = $this->_em->getRepository('DrestTests\\Entities\\CMS\\User')->find($user->getId());
     $this->assertNull($deletedEntity);
 }
Exemplo n.º 5
0
 public function testDeleteCollectionRequest()
 {
     $dm = $this->_getDrestManager($this->_em);
     $representation = new \DrestCommon\Representation\Json();
     $users = array(array('email_address' => '*****@*****.**', 'username' => 'frodo.baggins'), array('email_address' => '*****@*****.**', 'username' => 'samwise.gamgee'));
     foreach ($users as $user) {
         $userObj = new \DrestTests\Entities\CMS\User();
         $userObj->setEmailAddress($user['email_address']);
         $userObj->setUsername($user['username']);
         $this->_em->persist($userObj);
         $this->_em->flush();
         $this->_em->refresh($userObj);
         $entity = $this->_em->getRepository('DrestTests\\Entities\\CMS\\User')->find($userObj->getId());
         $this->assertEquals($userObj, $entity);
     }
     $request = \Symfony\Component\HttpFoundation\Request::create('/users', 'DELETE', array(), array(), array(), array('HTTP_ACCEPT' => $representation->getContentType()));
     $response = $dm->dispatch($request);
     $this->assertEquals(200, $response->getStatusCode());
     $deletedEntities = $this->_em->getRepository('DrestTests\\Entities\\CMS\\User')->findAll();
     $this->assertCount(0, $deletedEntities);
 }
Exemplo n.º 6
0
 public function testGetElementRequest()
 {
     $dm = $this->_getDrestManager($this->_em);
     $representation = new \DrestCommon\Representation\Json();
     $user = new \DrestTests\Entities\Typical\User();
     $email = '*****@*****.**';
     $username = '******';
     $user->setEmailAddress($email);
     $user->setUsername($username);
     $this->_em->persist($user);
     $this->_em->flush();
     $this->_em->refresh($user);
     $this->assertEquals($email, $user->getEmailAddress());
     $this->assertEquals($username, $user->getUsername());
     $request = \Symfony\Component\HttpFoundation\Request::create('/user/' . $user->getId(), 'GET', [], [], [], array('HTTP_ACCEPT' => $representation->getContentType()));
     $response = $dm->dispatch($request);
     $representation = $representation::createFromString($response->getBody());
     $userArray = $representation->toArray(false);
     $this->assertEquals($user->getEmailAddress(), $userArray['email_address']);
     $this->assertEquals($user->getUsername(), $userArray['username']);
 }
Exemplo n.º 7
0
 public function testGetCollectionRequest()
 {
     $dm = $this->_getDrestManager($this->_em);
     $representation = new \DrestCommon\Representation\Json();
     $users = array(array('email_address' => '*****@*****.**', 'username' => 'frodo.baggins'), array('email_address' => '*****@*****.**', 'username' => 'samwise.gamgee'));
     foreach ($users as $user) {
         $userObj = new \DrestTests\Entities\Typical\User();
         $userObj->setEmailAddress($user['email_address']);
         $userObj->setUsername($user['username']);
         $this->_em->persist($userObj);
         $this->_em->flush();
     }
     $request = \Symfony\Component\HttpFoundation\Request::create('/users', 'GET', [], [], [], array('HTTP_ACCEPT' => $representation->getContentType()));
     $response = $dm->dispatch($request);
     $representation = $representation::createFromString($response->getBody());
     $usersArray = $representation->toArray(false);
     for ($x = 0; $x < sizeof($users); $x++) {
         $this->assertEquals($users[$x]['email_address'], $usersArray[$x]['email_address']);
         $this->assertEquals($users[$x]['username'], $usersArray[$x]['username']);
     }
 }