//});
Router::prefix('api/v1', function (RouteBuilder $routes) {
- $routes->extensions(['json', 'xml']);
- $routes->resources('Users', ['id' => '[0-9A-Za-z-_ ]+']);
- $routes->resources('Connectors', ['id' => '[0-9A-Za-z-_ ]+'], function(RouteBuilder $routes) {
- $routes->resources('Services', ['id' => '[0-9A-Za-z-_ ]+']);
- });
-// $routes->resources('Users');
-});
-
-Router::prefix('api/test', function (RouteBuilder $routes) {
- $routes->extensions(['json', 'xml']);
- $routes->resources('Users', ['id' => '[0-9A-Za-z-_ ]+'], function($routes) {
-// $routes->connect('/roles', ['controller' => 'Users', 'action' => 'roles', '_method' => 'GET']);
- });
-// $routes->resources('Users', [
-// 'map' => [
-// 'view' => ['action' => 'view', 'method' => 'GET', 'path' => ':name'],
-// ]
-// ]);
-
-// $routes->connect('/users', ['controller' => 'Users', 'action' => 'index', '_method' => 'GET']);
-// $routes->connect('/users/*', ['controller' => 'Users', 'action' => 'view', '_method' => 'GET']);
-//
-// $routes->connect('/users', ['controller' => 'Users', 'action' => 'add', '_method' => 'POST']);
-
+ $routes->extensions(['json', 'xml']);
+ $routes->resources('Users');
+
+ $routes->resources('Connectors', ['id' => '[0-9A-Za-z-_ ]+'], function(RouteBuilder $routes) {
+ $routes->resources('Services', ['id' => '[0-9A-Za-z-_ ]+']);
+ });
});
Router::scope('/', function (RouteBuilder $routes) {
- $routes->extensions(['json', 'xml']);
+ $routes->extensions(['json', 'xml']);
- /**
- * Here, we are connecting '/' (base path) to a controller called 'Pages',
- * its action called 'display', and we pass a param to select the view file
- * to use (in this case, src/Template/Pages/home.ctp)...
- */
- $routes->connect('/', ['controller' => 'Pages', 'action' => 'display', 'home']);
+ /**
+ * Here, we are connecting '/' (base path) to a controller called 'Pages',
+ * its action called 'display', and we pass a param to select the view file
+ * to use (in this case, src/Template/Pages/home.ctp)...
+ */
+ $routes->connect('/', ['controller' => 'Pages', 'action' => 'display', 'home']);
- /**
- * ...and connect the rest of 'Pages' controller's URLs.
- */
- $routes->connect('/pages/*', ['controller' => 'Pages', 'action' => 'display']);
+ /**
+ * ...and connect the rest of 'Pages' controller's URLs.
+ */
+ $routes->connect('/pages/*', ['controller' => 'Pages', 'action' => 'display']);
- /**
- * Connect catchall routes for all controllers.
- *
- * Using the argument `DashedRoute`, the `fallbacks` method is a shortcut for
- * `$routes->connect('/:controller', ['action' => 'index'], ['routeClass' => 'DashedRoute']);`
- * `$routes->connect('/:controller/:action/*', [], ['routeClass' => 'DashedRoute']);`
- *
- * Any route class can be used with this method, such as:
- * - DashedRoute
- * - InflectedRoute
- * - Route
- * - Or your own route class
- *
- * You can remove these routes once you've connected the
- * routes you want in your application.
- */
- $routes->fallbacks('DashedRoute');
+ /**
+ * Connect catchall routes for all controllers.
+ *
+ * Using the argument `DashedRoute`, the `fallbacks` method is a shortcut for
+ * `$routes->connect('/:controller', ['action' => 'index'], ['routeClass' => 'DashedRoute']);`
+ * `$routes->connect('/:controller/:action/*', [], ['routeClass' => 'DashedRoute']);`
+ *
+ * Any route class can be used with this method, such as:
+ * - DashedRoute
+ * - InflectedRoute
+ * - Route
+ * - Or your own route class
+ *
+ * You can remove these routes once you've connected the
+ * routes you want in your application.
+ */
+ $routes->fallbacks('DashedRoute');
});
/**
'limit' => 20,
'maxLimit' => 100,
'fields' => [
- 'name', 'mail', 'firstname', 'lastname'
+ 'id', 'name', 'mail', 'firstname', 'lastname'
],
'sortWhitelist' => [
- 'name', 'mail', 'firstname', 'lastname'
+ 'id', 'name', 'mail', 'firstname', 'lastname'
]
];
/**
* View method
*
- * @param string $name User name.
+ * @param string $id User id.
* @return \Cake\Network\Response|null
* @throws UserNotFoundException When record not found.
*/
- public function view($name) {
+ public function view($id) {
$this->request->allowMethod('get');
-
- $user = $this->Users->findByName($name)->first();
-
- if (!$user) {
- throw new UserNotFoundException(__("The user $name does not exist"));
+
+ try {
+ $user = $this->Users->get($id);
+ }
+ catch (\Exception $e) {
+ throw new UserNotFoundException(__("The user with the id $id does not exist"));
}
$this->set('user', $user);
/**
* Edit method
*
- * @param string $name User name.
+ * @param string $id User id.
* @return \Cake\Network\Response|void Redirects on successful edit, renders view otherwise.
* @throws UserNotFoundException When record not found.
*/
- public function edit($name) {
+ public function edit($id) {
$this->request->allowMethod('put');
- $user = $this->Users->findByName($name)->first();
- if (!$user) {
- throw new UserNotFoundException(__("The user $name does not exist"));
+
+ try {
+ $oldUser = $this->Users->get($id);
+ }
+ catch (\Exception $e) {
+ throw new UserNotFoundException(__("The user with the id $id does not exist"));
}
if (isset($this->request->data['name'])) {
unset($this->request->data['name']);
}
- $user = $this->Users->patchEntity($user, $this->request->data);
+ $newUser = $this->Users->patchEntity($oldUser, $this->request->data);
- if ($this->Users->save($user)) {
+ if ($this->Users->save($newUser)) {
$this->response->statusCode(200);
- $this->set('user', $user);
+ $this->set('user', $newUser);
$this->set('_serialize', 'user');
}
else {
$this->response->statusCode(400);
- throw new ValidationException($user);
+ throw new ValidationException($newUser);
}
}
/**
* Delete method
*
- * @param string $name User name.
+ * @param string $id User id.
* @return \Cake\Network\Response|null Redirects to index.
* @throws UserNotFoundException When record not found.
*/
- public function delete($name) {
+ public function delete($id) {
$this->request->allowMethod('delete');
- $user = $this->Users->findByName($name)->first();
-
- if (!$user) {
- throw new UserNotFoundException(__("The user $name does not exist"));
+
+ try {
+ $user = $this->Users->get($id);
+ }
+ catch (\Exception $e) {
+ throw new UserNotFoundException(__("The user with the id $id does not exist"));
}
$this->Users->delete($user);
* @var array
*/
protected $_hidden = [
- 'password', 'id'
+ 'password'
];
}
*/
class UsersFixture extends TestFixture {
- /**
- * Fields
- *
- * @var array
- */
- // @codingStandardsIgnoreStart
- public $fields = [
- 'id' => ['type' => 'integer', 'length' => 10, 'autoIncrement' => true, 'default' => null, 'null' => false, 'comment' => null, 'precision' => null, 'unsigned' => null],
- 'name' => ['type' => 'string', 'length' => 255, 'default' => null, 'null' => false, 'comment' => null, 'precision' => null, 'fixed' => null],
- 'password' => ['type' => 'string', 'length' => 255, 'default' => null, 'null' => false, 'comment' => null, 'precision' => null, 'fixed' => null],
- 'mail' => ['type' => 'string', 'length' => 255, 'default' => null, 'null' => false, 'comment' => null, 'precision' => null, 'fixed' => null],
- 'firstname' => ['type' => 'string', 'length' => 255, 'default' => null, 'null' => false, 'comment' => null, 'precision' => null, 'fixed' => null],
- 'lastname' => ['type' => 'string', 'length' => 255, 'default' => null, 'null' => false, 'comment' => null, 'precision' => null, 'fixed' => null],
- '_constraints' => [
- 'primary' => ['type' => 'primary', 'columns' => ['id'], 'length' => []],
- 'users_name_UNIQUE' => ['type' => 'unique', 'columns' => ['name'], 'length' => []],
- 'users_email_UNIQUE' => ['type' => 'unique', 'columns' => ['mail'], 'length' => []],
- ],
- ];
- // @codingStandardsIgnoreEnd
+ /**
+ * Fields
+ *
+ * @var array
+ */
+ // @codingStandardsIgnoreStart
+ public $fields = [
+ 'id' => ['type' => 'integer', 'length' => 10, 'autoIncrement' => true, 'default' => null, 'null' => false, 'comment' => null, 'precision' => null, 'unsigned' => null],
+ 'name' => ['type' => 'string', 'length' => 255, 'default' => null, 'null' => false, 'comment' => null, 'precision' => null, 'fixed' => null],
+ 'password' => ['type' => 'string', 'length' => 255, 'default' => null, 'null' => false, 'comment' => null, 'precision' => null, 'fixed' => null],
+ 'mail' => ['type' => 'string', 'length' => 255, 'default' => null, 'null' => false, 'comment' => null, 'precision' => null, 'fixed' => null],
+ 'firstname' => ['type' => 'string', 'length' => 255, 'default' => null, 'null' => false, 'comment' => null, 'precision' => null, 'fixed' => null],
+ 'lastname' => ['type' => 'string', 'length' => 255, 'default' => null, 'null' => false, 'comment' => null, 'precision' => null, 'fixed' => null],
+ '_constraints' => [
+ 'primary' => ['type' => 'primary', 'columns' => ['id'], 'length' => []],
+ 'users_name_UNIQUE' => ['type' => 'unique', 'columns' => ['name'], 'length' => []],
+ 'users_email_UNIQUE' => ['type' => 'unique', 'columns' => ['mail'], 'length' => []],
+ ],
+ ];
+ // @codingStandardsIgnoreEnd
- /**
- * Records
- *
- * @var array
- */
- public $records = [
- [
- 'name' => 'mreyrolle',
- 'password' => 'mreyrolle',
- 'mail' => 'maxime.reyrolle@example.org',
- 'firstname' => 'Maxime',
- 'lastname' => 'Reyrolle'
- ],
- [
- 'name' => 'aauzolat',
- 'password' => 'aauzolat',
- 'mail' => 'arnaud.auzolat@example.org',
- 'firstname' => 'Arnaud',
- 'lastname' => 'Auzolat'
- ],
- ];
+ /**
+ * Records
+ *
+ * @var array
+ */
+ public $records = [
+ [
+ 'name' => 'mreyrolle',
+ 'password' => 'mreyrolle',
+ 'mail' => 'maxime.reyrolle@example.org',
+ 'firstname' => 'Maxime',
+ 'lastname' => 'Reyrolle'
+ ],
+ [
+ 'name' => 'aauzolat',
+ 'password' => 'aauzolat',
+ 'mail' => 'arnaud.auzolat@example.org',
+ 'firstname' => 'Arnaud',
+ 'lastname' => 'Auzolat'
+ ],
+ ];
}
$expected = [
'users' => [
[
- "name" => "mreyrolle", "mail" => "maxime.reyrolle@example.org", "firstname" => "Maxime", "lastname" => "Reyrolle"
+ "id" => 1, "name" => "mreyrolle", "mail" => "maxime.reyrolle@example.org", "firstname" => "Maxime", "lastname" => "Reyrolle"
],
[
- "name" => "aauzolat", "mail" => "arnaud.auzolat@example.org", "firstname" => "Arnaud", "lastname" => "Auzolat"
+ "id" => 2, "name" => "aauzolat", "mail" => "arnaud.auzolat@example.org", "firstname" => "Arnaud", "lastname" => "Auzolat"
]
],
'pagination' => [
}
public function testWhenAddingANewUser() {
+ $this->loadFixtures('Users');
+
$data = [
'name' => 'username',
'password' => 'password',
'name' => 'username',
'mail' => 'mail@example.org',
'firstname' => 'The firstname of the user',
- 'lastname' => 'The lastname of the user'
+ 'lastname' => 'The lastname of the user',
+ 'id' => 3
];
$expectedBody = $this->getExpectedValue($expected);
$this->loadFixtures('Users');
- $this->iSendAGetRequestTo('/users/aauzolat.json');
+ $this->iSendAGetRequestTo('/users/2.json');
$this->theResponseCodeShouldBe(200);
$expected = [
+ 'id' => 2,
'name' => 'aauzolat',
'mail' => 'arnaud.auzolat@example.org',
'firstname' => 'Arnaud',
public function testWhenFindingAnUnknownUser() {
$this->loadFixtures('Users');
- $this->iSendAGetRequestTo('/users/donotexist.json');
+ $this->iSendAGetRequestTo('/users/4.json');
$this->theResponseCodeShouldBe(404);
$expected = [
- 'message' => 'The user donotexist does not exist',
- 'url' => '/api/v1/users/donotexist.json',
+ 'message' => 'The user with the id 4 does not exist',
+ 'url' => '/api/v1/users/4.json',
'code' => 404
];
$expectedBody = $this->getExpectedValue($expected);
public function testWhenDeletingANotExistingUser() {
- $this->iSendADeleteRequestTo('/users/donotexist.json');
+ $this->iSendADeleteRequestTo('/users/4.json');
$this->theResponseCodeShouldBe(404);
$expected = [
- 'message' => 'The user donotexist does not exist',
- 'url' => '/api/v1/users/donotexist.json',
+ 'message' => 'The user with the id 4 does not exist',
+ 'url' => '/api/v1/users/4.json',
'code' => 404
];
$expectedBody = $this->getExpectedValue($expected);
public function testWhenDeletingAnExistingUser() {
$this->loadFixtures('Users');
- $this->iSendADeleteRequestTo('/users/aauzolat.json');
+ $this->iSendADeleteRequestTo('/users/2.json');
$this->theResponseCodeShouldBe(204);
}
];
$this->givenIHaveSomeDataToSend($data);
- $this->iSendAPutRequestTo('/users/aauzolat.json');
+ $this->iSendAPutRequestTo('/users/2.json');
$this->theResponseCodeShouldBe(200);
$expected = [
+ 'id' => 2,
'name' => 'aauzolat',
'mail' => 'new_mail@example.org',
'firstname' => 'new firstname',
];
$this->givenIHaveSomeDataToSend($data);
- $this->iSendAPutRequestTo('/users/donotexist.json');
+ $this->iSendAPutRequestTo('/users/4.json');
$this->theResponseCodeShouldBe(404);
$expected = [
- 'message' => 'The user donotexist does not exist',
- 'url' => '/api/v1/users/donotexist.json',
+ 'message' => 'The user with the id 4 does not exist',
+ 'url' => '/api/v1/users/4.json',
'code' => 404
];
-
+
$expectedBody = $this->getExpectedValue($expected);
$this->theResponseBodyShouldBe($expectedBody);
}
];
$this->givenIHaveSomeDataToSend($data);
- $this->iSendAPutRequestTo('/users/mreyrolle.json');
+ $this->iSendAPutRequestTo('/users/1.json');
$this->theResponseCodeShouldBe(400);
$expected = [
'code' => 400,
- 'url' => '/api/v1/users/mreyrolle.json',
+ 'url' => '/api/v1/users/1.json',
'message' => '3 validation errors occurred',
'errorCount' => 3,
'errors' => [
*/
class UsersTableTest extends TestCase {
- /**
- * Test subject
- *
- * @var \App\Model\Table\UsersTable
- */
- public $Users;
+ /**
+ * Test subject
+ *
+ * @var \App\Model\Table\UsersTable
+ */
+ public $Users;
- /**
- * Fixtures
- *
- * @var array
- */
- public $fixtures = [
- 'app.users',
+ /**
+ * Fixtures
+ *
+ * @var array
+ */
+ public $fixtures = [
+ 'app.users',
// 'app.users_memberships'
- ];
+ ];
- /**
- * setUp method
- *
- * @return void
- */
- public function setUp() {
- parent::setUp();
- $config = TableRegistry::exists('Users') ? [] : ['className' => 'App\Model\Table\UsersTable'];
- $this->Users = TableRegistry::get('Users', $config);
- }
+ /**
+ * setUp method
+ *
+ * @return void
+ */
+ public function setUp() {
+ parent::setUp();
+ $config = TableRegistry::exists('Users') ? [] : ['className' => 'App\Model\Table\UsersTable'];
+ $this->Users = TableRegistry::get('Users', $config);
+ }
- /**
- * tearDown method
- *
- * @return void
- */
- public function tearDown() {
- unset($this->Users);
+ /**
+ * tearDown method
+ *
+ * @return void
+ */
+ public function tearDown() {
+ unset($this->Users);
- parent::tearDown();
- }
+ parent::tearDown();
+ }
- /**
- * Test initialize method
- *
- * @return void
- */
- public function testInitialize() {
- $this->markTestIncomplete('Not implemented yet.');
- }
+ /**
+ * Test initialize method
+ *
+ * @return void
+ */
+ public function testInitialize() {
+ $this->markTestIncomplete('Not implemented yet.');
+ }
- /**
- * Test validationDefault method
- *
- * @return void
- */
- public function testValidationDefault() {
- $this->markTestIncomplete('Not implemented yet.');
- }
+ /**
+ * Test validationDefault method
+ *
+ * @return void
+ */
+// public function testValidationDefault() {
+// $this->markTestIncomplete('Not implemented yet.');
+// }
- /**
- * Test buildRules method
- *
- * @return void
- */
- public function testBuildRules() {
- $this->markTestIncomplete('Not implemented yet.');
- }
+ /**
+ * Test buildRules method
+ *
+ * @return void
+ */
+// public function testBuildRules() {
+// $this->markTestIncomplete('Not implemented yet.');
+// }
// public function testAddNewUser() {
// $userData = [
// $result = $this->Users->save($user);
// $this->assertNotFalse($result);
// }
-
}