PageRenderTime 37ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/Doctrine/Tests/REST/FunctionalTest.php

https://github.com/ner0tic/rest
PHP | 241 lines | 182 code | 43 blank | 16 comment | 13 complexity | 7f5c75a0daf1ad5574b2266393c1388d MD5 | raw file
  1. <?php
  2. namespace Doctrine\Tests\REST;
  3. use Doctrine\ORM\EntityManager,
  4. Doctrine\REST\Client\Manager,
  5. Doctrine\REST\Client\Request,
  6. Doctrine\REST\Client\Entity,
  7. Doctrine\REST\Client\EntityConfiguration,
  8. Doctrine\REST\Client\Client,
  9. Doctrine\REST\Server\Server;
  10. require_once __DIR__ . '/TestInit.php';
  11. class FunctionalTest extends \PHPUnit_Framework_TestCase
  12. {
  13. private $_manager;
  14. private $_client;
  15. public function setUpRest($type)
  16. {
  17. $config = new \Doctrine\ORM\Configuration();
  18. $config->setMetadataCacheImpl(new \Doctrine\Common\Cache\ArrayCache);
  19. $config->setProxyDir('/tmp');
  20. $config->setProxyNamespace('Proxies');
  21. $config->setMetadataDriverImpl($config->newDefaultAnnotationDriver());
  22. $connectionOptions = array(
  23. 'driver' => 'pdo_sqlite',
  24. 'memory' => true
  25. );
  26. $em = \Doctrine\ORM\EntityManager::create($connectionOptions, $config);
  27. $classes = array($em->getMetadataFactory()->getMetadataFor('Doctrine\Tests\REST\DoctrineUser'));
  28. $schemaTool = new \Doctrine\ORM\Tools\SchemaTool($em);
  29. $schemaTool->dropSchema($classes);
  30. $schemaTool->createSchema($classes);
  31. if ($type === 'orm') {
  32. $this->_client = new TestFunctionalClient('user', $em);
  33. } else {
  34. $this->_client = new TestFunctionalClient('user', $em->getConnection());
  35. }
  36. $this->_manager = new Manager($this->_client);
  37. $this->_manager->registerEntity('Doctrine\Tests\REST\User');
  38. Entity::setManager($this->_manager);
  39. }
  40. public function testOrm()
  41. {
  42. $this->setUpRest('orm');
  43. $this->_testActiveRecordApi();
  44. }
  45. public function testDbal()
  46. {
  47. $this->setUpRest('dbal');
  48. $this->_testActiveRecordApi();
  49. }
  50. private function _testActiveRecordApi()
  51. {
  52. $user1 = new User();
  53. $user1->setUsername('jwage');
  54. $user1->save();
  55. $this->assertEquals(1, $user1->getId());
  56. $user2 = new User();
  57. $user2->setUsername('fabpot');
  58. $user2->save();
  59. $this->assertEquals(2, $user2->getId());
  60. $user3 = new User();
  61. $user3->setUsername('romanb');
  62. $user3->save();
  63. $this->assertEquals(3, $user3->getId());
  64. $user3->setUsername('romanb_new');
  65. $user3->save();
  66. $user3test = User::find($user3->getId());
  67. $this->assertEquals('romanb_new', $user3test->getUsername());
  68. $test = User::findAll();
  69. $this->assertEquals(3, count($test));
  70. $this->assertTrue($user1 === $test[0]);
  71. $this->assertTrue($user2 === $test[1]);
  72. $this->assertTrue($user3 === $test[2]);
  73. $user3->delete();
  74. $test = User::findAll();
  75. $this->assertEquals(2, count($test));
  76. }
  77. }
  78. class TestFunctionalClient extends Client
  79. {
  80. public $name;
  81. public $source;
  82. public $data = array();
  83. public $count = 0;
  84. public function __construct($name, $source)
  85. {
  86. $this->name = $name;
  87. $this->source = $source;
  88. }
  89. public function execServer($request, $requestArray, $parameters = array(), $responseType = 'xml')
  90. {
  91. $requestArray = array_merge($requestArray, (array) $parameters);
  92. $server = new Server($this->source, $requestArray);
  93. if ($this->source instanceof EntityManager) {
  94. $server->setEntityAlias('Doctrine\Tests\REST\DoctrineUser', 'user');
  95. }
  96. $response = $server->getRequestHandler()->execute();
  97. $data = $request->getResponseTransformerImpl()->transform($response->getContent());
  98. return $data;
  99. }
  100. public function execute(Request $request)
  101. {
  102. $url = $request->getUrl();
  103. $method = $request->getMethod();
  104. $parameters = $request->getParameters();
  105. $responseType = $request->getResponseType();
  106. // GET api/user/1.xml (get)
  107. if ($method === 'GET' && preg_match_all('/api\/' . $this->name . '\/([0-9]).xml/', $url, $matches)) {
  108. $id = $matches[1][0];
  109. return $this->execServer($request, array(
  110. '_method' => $method,
  111. '_format' => $responseType,
  112. '_entity' => $this->name,
  113. '_action' => 'get',
  114. '_id' => $id
  115. ), $parameters, $responseType);
  116. }
  117. // GET api/user.xml (list)
  118. if ($method === 'GET' && preg_match_all('/api\/' . $this->name . '.xml/', $url, $matches)) {
  119. return $this->execServer($request, array(
  120. '_method' => $method,
  121. '_format' => $responseType,
  122. '_entity' => $this->name,
  123. '_action' => 'list'
  124. ), $parameters, $responseType);
  125. }
  126. // PUT api/user.xml (insert)
  127. if ($method === 'PUT' && preg_match_all('/api\/' . $this->name . '.xml/', $url, $matches)) {
  128. return $this->execServer($request, array(
  129. '_method' => $method,
  130. '_format' => $responseType,
  131. '_entity' => $this->name,
  132. '_action' => 'insert'
  133. ), $parameters, $responseType);
  134. }
  135. // POST api/user/1.xml (update)
  136. if ($method === 'POST' && preg_match_all('/api\/' . $this->name . '\/([0-9]).xml/', $url, $matches)) {
  137. return $this->execServer($request, array(
  138. '_method' => $method,
  139. '_format' => $responseType,
  140. '_entity' => $this->name,
  141. '_action' => 'update',
  142. '_id' => $parameters['id']
  143. ), $parameters, $responseType);
  144. }
  145. // DELETE api/user/1.xml (delete)
  146. if ($method === 'DELETE' && preg_match_all('/api\/' . $this->name . '\/([0-9]).xml/', $url, $matches)) {
  147. return $this->execServer($request, array(
  148. '_method' => $method,
  149. '_format' => $responseType,
  150. '_entity' => $this->name,
  151. '_action' => 'delete',
  152. '_id' => $matches[1][0]
  153. ), $parameters, $responseType);
  154. }
  155. }
  156. }
  157. class User extends Entity
  158. {
  159. protected $id;
  160. protected $username;
  161. public static function configure(EntityConfiguration $entityConfiguration)
  162. {
  163. $entityConfiguration->setUrl('api');
  164. $entityConfiguration->setName('user');
  165. }
  166. public function getId()
  167. {
  168. return $this->id;
  169. }
  170. public function getUsername()
  171. {
  172. return $this->username;
  173. }
  174. public function setUsername($username)
  175. {
  176. $this->username = $username;
  177. }
  178. }
  179. /**
  180. * @Entity
  181. * @Table(name="user")
  182. */
  183. class DoctrineUser
  184. {
  185. /**
  186. * @Id @Column(type="integer")
  187. * @GeneratedValue(strategy="AUTO")
  188. */
  189. private $id;
  190. /**
  191. * @Column(type="string", length=255, unique=true)
  192. */
  193. private $username;
  194. public function setUsername($username)
  195. {
  196. $this->username = $username;
  197. }
  198. }