From 350c2e4898c6a6ac43436b7bd0447670188fe411 Mon Sep 17 00:00:00 2001 From: aniaw Date: Tue, 26 Sep 2017 15:03:41 +0200 Subject: [PATCH 1/2] shorten the README --- README.md | 198 +----------------------------------------------------- 1 file changed, 2 insertions(+), 196 deletions(-) diff --git a/README.md b/README.md index b9bb458..d99537a 100644 --- a/README.md +++ b/README.md @@ -1,198 +1,6 @@ -# Symfony 2.8 - Exercise 5 - Simple CRUD on REST +# PHP Symfony 2.8 scaffolding for RealSkill - -## Summary - -Expected result of this task is an application which allows user to create/read/update/delete row in the table using REST requests. - -Sample employee structure: -``` -[ - 'id' => 1, - 'name' => 'Martin' - 'surname' => 'Fowler' - 'email' => 'martin.fowler@fake.pl' - 'daysInOffice' => 2, - 'bio' => 'Lorem ipsum dolor sit amet.' -] -``` - -To make working with JSON requests easy we have installed and configured **symfony-json-request-transformer** bundle so: -``` -A request with JSON content like: -{ - "foo": "bar" -} -will be decoded automatically so can access the foo property like: - -$request->request->get('foo'); -``` - -If you have solved **Symfony 2 - Exercise 4 - Simple CRUD** then You can copy `Employee` entity to this branch and step 1 will be done. -`git checkout task/4-simple-crud -- src/AppBundle/Entity/Employee.php` - -## Goals - -In order to complete this exercise you will need to follow these steps: - -1. Create `Employee` entity in `AppBundle/Entity/Employee.php` with proper `namespace`. Entity should contain properties listed below with setters and getters for each one. - - * id - primary key, autoincrement - * name - type: `string`, length: 64, required, assertions: - * not blank - * min length = 3 - * max length = 64 - * surname - type: `string`, length: 64, required, assertions: - * not blank - * min length = 3 - * max length = 64 - * email - type: `string`, length: 254, required, unique, assertions: - * not blank - * email - * max length = 254 - * bio - type: `string`, length: 400, not required, assertions: - * max length = 400 - * daysInOffice - type: `smallint`, required, assertions: - * not blank - * range(2,5) - * min range message = "You must be at least {{ limit }} days working in the office." - * max range message = "You cannot work more than {{ limit }} days in the office." - -2. Create endpoint to retrieve list of employees - * should be accessed via GET "/api/employee" - * should return array of employees as JSON - -3. Create endpoint to create new employee - * should be accessed via POST "/api/employee" - * should assign name, surname, email, bio, daysInOffice from request to `Employee` object - * should validate if `Employee` after assigning data is valid - * if `Employee` is invalid it should return JSON with property success equal false and errors containing array of error messages per each property with **PRECONDITION_FAILED** status code - use specially created for this purpose `violation_list_converter` service to convert errors from `validator` service to array eg: - ``` - { - "success": false, - "errors": { - "name": "This value should not be blank." - } - } - ``` - * if unique constraint will throw exception it should return JSON response with property success equal false and errors containing info for `email` property that `Such email exists in DB.` with **CONFLICT** status code like below: - ``` - { - "success": false, - "errors": { - "email": "Such email exists in DB." - } - } - ``` - * if `Employee` is valid it should create it and return JSON response with property `success` set to to true and id set to newly created object id like below: - ``` - { - "success": true, - "id": 15 - } - ``` - -4. Create endpoint to update existing employee - * should be accessed via PUT "/api/employee/{employeeId}" - * should assign name, surname, email, bio, daysInOffice from request to `Employee` object - * should validate if `Employee` after assigning data is valid - * if `Employee` with given `employeeId` not exist it should return **NOT_FOUND** status code and JSON with property `success` set to false - * if `Employee` is invalid it should return JSON with property success equal false and errors containing array of error messages per each property with **PRECONDITION_FAILED** status code - use specially created for this purpose `violation_list_converter` service to convert errors from `validator` service to array eg: - ``` - { - "success": false, - "errors": { - "name": "This value should not be blank." - } - } - ``` - * if unique constraint will throw exception it should return JSON response with property success equal false and errors containing info for `email` property that `Such email exists in DB.` with **CONFLICT** status code like below: - ``` - { - "success": false, - "errors": { - "email": "Such email exists in DB." - } - } - ``` - * if `Employee` is valid it should create it and return JSON response with property `success` set to to true and id set to newly created object id like below: - ``` - { - "success": true, - "id": 15 - } - ``` - -5. Create endpoint to delete existing employee - * should be accessed via DELETE "/api/employee/{employeeId}" - * if `Employee` with given `employeeId` not exist it should **NOT_FOUND** status code and JSON with property `success` set to false - * if `Employee` with given `employeeId` exist it should delete him and return JSON with property `success` set to true - -Expected result of `php app/composer test-dox` for completed exercise is listed below: -``` -AppBundle\Tests\Entity\Employee - [x] Should not allow to save employee without name - [x] Should not allow to save employee without surname - [x] Should not allow to save employee without email - [x] Should not allow to save employee without days in office - [x] Should not allow to save employee with same email that exists in db - [x] Should allow to save employee without bio - [x] Should have defined length of each string field and trim if is longer - -AppBundle\Tests\Feature\CreateEmployee - [x] Should return precondition failed with proper errors if empty data is send - [x] Should return precondition failed with proper errors if all send employee properties are empty - [x] Should return precondition failed with proper errors if name is to short - [x] Should return precondition failed with proper errors if name is to long - [x] Should return precondition failed with proper errors if surname is to short - [x] Should return precondition failed with proper errors if surname is to long - [x] Should return precondition failed with proper errors if email is invalid - [x] Should return precondition failed with proper errors if email is to long - [x] Should return precondition failed with proper errors if bio is to long - [x] Should return precondition failed with proper errors if days in office is invalid - [x] Should return precondition failed with proper errors if days in office has to low value - [x] Should return precondition failed with proper errors if days in office has to high value - [x] Should save employee if proper data is given and return its id - [x] Should not require bio - -AppBundle\Tests\Feature\DeleteEmployee - [x] Should return 404 if employee not exists and json with property status set to false - [x] Should delete certain employee - -AppBundle\Tests\Feature\RetrieveEmployee - [x] Should return employees array - -AppBundle\Tests\Feature\UpdateEmployee - [x] Should save employee if proper data is given and return its id - [x] Should not require bio - [x] Should return precondition failed with proper errors if empty data is send - [x] Should return precondition failed with proper errors if all send employee properties are empty - [x] Should return precondition failed with proper errors if name is to short - [x] Should return precondition failed with proper errors if name is to long - [x] Should return precondition failed with proper errors if surname is to short - [x] Should return precondition failed with proper errors if surname is to long - [x] Should return precondition failed with proper errors if email is invalid - [x] Should return precondition failed with proper errors if email is to long - [x] Should return precondition failed with proper errors if bio is to long - [x] Should return precondition failed with proper errors if days in office is invalid - [x] Should return precondition failed with proper errors if days in office has to low value - [x] Should return precondition failed with proper errors if days in office has to high value -``` - - -## Hints - -Most of changes should lay in `src` dir. You can also modify templates in `app/Resources/views`. If needed You can also modify config and other files in `app` dir. - -If You want to see what goals You have passed You should run: `php app/composer test-dox`. Each scenario with **[x]** has passed and those with **[ ]** has to be done. - -More info about errors during tests You can get running tests with command: `php app/composer test` - -This task is concerned as done when all tests are passing and when code-sniffer and mess-detector do not return errors nor warnings (ignore info about "Remaining deprecation notices" during test). - -Remember to commit changes before You change branch. - -Remember to install dependencies if You change branch. +You can quickly create Symfony tasks by cloning the scaffolding repo. You don't have to bother with configuration. ### Helpful links @@ -228,7 +36,6 @@ Please remember to read documentation for Symfony 2.8 because it can differ in n php app/composer code-sniffer - ## Run php server php app/console server:run @@ -247,7 +54,6 @@ mysql> grant usage on *.* to realskill@localhost identified by 'realskill'; mysql> grant all privileges on realskill.* to realskill@localhost ; ``` - **Now You can access website via http://127.0.0.1:8000** Good luck! From f7cbd7d80c32cddb2fb9c3ebfdf1bb9218085b44 Mon Sep 17 00:00:00 2001 From: aniaw Date: Wed, 27 Sep 2017 15:33:23 +0200 Subject: [PATCH 2/2] remove uncessesary files --- src/AppBundle/Tests/Entity/EmployeeTest.php | 135 ------ .../Tests/Feature/CreateEmployeeTest.php | 403 ------------------ .../Tests/Feature/DeleteEmployeeTest.php | 133 ------ .../Tests/Feature/RetrieveEmployeeTest.php | 70 --- .../Tests/Feature/UpdateEmployeeTest.php | 137 ------ .../GetDataArrayFromJsonResponseTrait.php | 19 - 6 files changed, 897 deletions(-) delete mode 100644 src/AppBundle/Tests/Feature/CreateEmployeeTest.php delete mode 100644 src/AppBundle/Tests/Feature/DeleteEmployeeTest.php delete mode 100644 src/AppBundle/Tests/Feature/RetrieveEmployeeTest.php delete mode 100644 src/AppBundle/Tests/Feature/UpdateEmployeeTest.php delete mode 100644 src/AppBundle/Tests/Traits/GetDataArrayFromJsonResponseTrait.php diff --git a/src/AppBundle/Tests/Entity/EmployeeTest.php b/src/AppBundle/Tests/Entity/EmployeeTest.php index f12ec1c..864251f 100644 --- a/src/AppBundle/Tests/Entity/EmployeeTest.php +++ b/src/AppBundle/Tests/Entity/EmployeeTest.php @@ -30,141 +30,6 @@ public function shouldNotAllowToSaveEmployeeWithoutName() $em->flush(); } - /** - * @test - * @expectedException Doctrine\DBAL\Exception\NotNullConstraintViolationException - */ - public function shouldNotAllowToSaveEmployeeWithoutSurname() - { - $employee = new Employee(); - $employee - ->setName('aaa') - ->setEmail('test@test.pl') - ->setDaysInOffice(3); - - $em = self::getEntityManager(); - - $em->persist($employee); - $em->flush(); - } - - /** - * @test - * @expectedException Doctrine\DBAL\Exception\NotNullConstraintViolationException - */ - public function shouldNotAllowToSaveEmployeeWithoutEmail() - { - $employee = new Employee(); - $employee - ->setName('aaa') - ->setSurname('aaa') - ->setDaysInOffice(3); - - $em = self::getEntityManager(); - - $em->persist($employee); - $em->flush(); - } - - /** - * @test - * @expectedException Doctrine\DBAL\Exception\NotNullConstraintViolationException - */ - public function shouldNotAllowToSaveEmployeeWithoutDaysInOffice() - { - $employee = new Employee(); - $employee - ->setName('aaa') - ->setSurname('aaa') - ->setEmail('test@test.pl'); - - $em = self::getEntityManager(); - - $em->persist($employee); - $em->flush(); - } - - /** - * @test - * @expectedException Doctrine\DBAL\Exception\UniqueConstraintViolationException - */ - public function shouldNotAllowToSaveEmployeeWithSameEmailThatExistsInDb() - { - $employee = new Employee(); - $employee - ->setName('aaa') - ->setSurname('aaa') - ->setEmail('martin.fowler@fake.pl') - ->setDaysInOffice(3); - - $em = self::getEntityManager(); - - $em->persist($employee); - $em->flush(); - } - - /** - * @test - */ - public function shouldAllowToSaveEmployeeWithoutBio() - { - $employee = new Employee(); - $employee - ->setName(str_repeat('a', 64)) - ->setSurname(str_repeat('a', 64)) - ->setEmail(str_repeat('a', 254)) - ->setDaysInOffice(3); - - $em = self::getEntityManager(); - - $em->persist($employee); - $em->flush(); - $em->detach($employee); - - /** - * @var Employee $savedEmployee - */ - $savedEmployee = $em->getRepository('AppBundle:Employee')->find(4); - - $this->assertEquals(64, strlen($savedEmployee->getName())); - $this->assertEquals(64, strlen($savedEmployee->getSurname())); - $this->assertEquals(254, strlen($savedEmployee->getEmail())); - $this->assertEquals(0, strlen($savedEmployee->getBio())); - $this->assertEquals(3, $savedEmployee->getDaysInOffice()); - } - - /** - * @test - */ - public function shouldHaveDefinedLengthOfEachStringFieldAndTrimIfIsLonger() - { - $employee = new Employee(); - $employee - ->setName(str_repeat('a', 999)) - ->setSurname(str_repeat('a', 999)) - ->setEmail(str_repeat('a', 999)) - ->setBio(str_repeat('a', 999)) - ->setDaysInOffice(3); - - $em = self::getEntityManager(); - - $em->persist($employee); - $em->flush(); - $em->detach($employee); - - /** - * @var Employee $savedEmployee - */ - $savedEmployee = $em->getRepository('AppBundle:Employee')->find(4); - - $this->assertEquals(64, strlen($savedEmployee->getName()), 'Name should has max length equal 64'); - $this->assertEquals(64, strlen($savedEmployee->getSurname()), 'Surname should has max length equal 64'); - $this->assertEquals(254, strlen($savedEmployee->getEmail()), 'Email should has max length equal 254'); - $this->assertEquals(400, strlen($savedEmployee->getBio()), 'Bio should has max length equal 400'); - $this->assertEquals(3, $savedEmployee->getDaysInOffice()); - } - - /** * @return \Doctrine\Common\Persistence\ObjectManager|object */ diff --git a/src/AppBundle/Tests/Feature/CreateEmployeeTest.php b/src/AppBundle/Tests/Feature/CreateEmployeeTest.php deleted file mode 100644 index 177d79c..0000000 --- a/src/AppBundle/Tests/Feature/CreateEmployeeTest.php +++ /dev/null @@ -1,403 +0,0 @@ - false, - 'errors' => [ - 'name' => 'This value should not be blank.', - 'surname' => 'This value should not be blank.', - 'email' => 'This value should not be blank.', - 'daysInOffice' => 'This value should not be blank.', - ], - ]; - $response = $this->sendData([]); - - $this->assertEquals(JsonResponse::HTTP_PRECONDITION_FAILED, $response->getStatusCode(), 'Should return PRECONDITION_FAILED status code'); - $this->assertEquals('application/json', $response->headers->get('Content-Type')); - $this->assertEquals($expectedData, $this->getDataArrayFromJsonResponse()); - } - - /** - * @test - */ - public function shouldReturnPreconditionFailedWithProperErrorsIfAllSendEmployeePropertiesAreEmpty() - { - $expectedData = [ - 'success' => false, - 'errors' => [ - 'name' => 'This value should not be blank.', - 'surname' => 'This value should not be blank.', - 'email' => 'This value should not be blank.', - 'daysInOffice' => 'This value should be a valid number.', - ], - ]; - $response = $this->sendData([ - 'name' => '', - 'surname' => '', - 'email' => '', - 'bio' => '', - 'daysInOffice' => '', - ]); - - $this->assertEquals(JsonResponse::HTTP_PRECONDITION_FAILED, $response->getStatusCode(), 'Should return PRECONDITION_FAILED status code'); - $this->assertEquals('application/json', $response->headers->get('Content-Type')); - $this->assertEquals($expectedData, $this->getDataArrayFromJsonResponse()); - } - - /** - * @test - */ - public function shouldReturnPreconditionFailedWithProperErrorsIfNameIsToShort() - { - $expectedData = [ - 'success' => false, - 'errors' => [ - 'name' => 'This value is too short. It should have 3 characters or more.', - ], - ]; - $response = $this->sendData([ - 'name' => 'aa', - 'surname' => 'aaa', - 'email' => 'test@test.pl', - 'bio' => '', - 'daysInOffice' => 3, - ]); - - $this->assertEquals(JsonResponse::HTTP_PRECONDITION_FAILED, $response->getStatusCode(), 'Should return PRECONDITION_FAILED status code'); - $this->assertEquals('application/json', $response->headers->get('Content-Type')); - $this->assertEquals($expectedData, $this->getDataArrayFromJsonResponse()); - } - - /** - * @test - */ - public function shouldReturnPreconditionFailedWithProperErrorsIfNameIsToLong() - { - $expectedData = [ - 'success' => false, - 'errors' => [ - 'name' => 'This value is too long. It should have 64 characters or less.', - ], - ]; - $response = $this->sendData([ - 'name' => str_repeat('a', 65), - 'surname' => 'aaa', - 'email' => 'test@test.pl', - 'bio' => '', - 'daysInOffice' => 3, - ]); - - $this->assertEquals(JsonResponse::HTTP_PRECONDITION_FAILED, $response->getStatusCode(), 'Should return PRECONDITION_FAILED status code'); - $this->assertEquals('application/json', $response->headers->get('Content-Type')); - $this->assertEquals($expectedData, $this->getDataArrayFromJsonResponse()); - } - - /** - * @test - */ - public function shouldReturnPreconditionFailedWithProperErrorsIfSurnameIsToShort() - { - $expectedData = [ - 'success' => false, - 'errors' => [ - 'surname' => 'This value is too short. It should have 3 characters or more.', - ], - ]; - $response = $this->sendData([ - 'name' => 'aaa', - 'surname' => 'aa', - 'email' => 'test@test.pl', - 'bio' => '', - 'daysInOffice' => 3, - ]); - - $this->assertEquals(JsonResponse::HTTP_PRECONDITION_FAILED, $response->getStatusCode(), 'Should return PRECONDITION_FAILED status code'); - $this->assertEquals('application/json', $response->headers->get('Content-Type')); - $this->assertEquals($expectedData, $this->getDataArrayFromJsonResponse()); - } - - /** - * @test - */ - public function shouldReturnPreconditionFailedWithProperErrorsIfSurnameIsToLong() - { - $expectedData = [ - 'success' => false, - 'errors' => [ - 'surname' => 'This value is too long. It should have 64 characters or less.', - ], - ]; - $response = $this->sendData([ - 'name' => 'aaa', - 'surname' => str_repeat('a', 65), - 'email' => 'test@test.pl', - 'bio' => '', - 'daysInOffice' => 3, - ]); - - $this->assertEquals(JsonResponse::HTTP_PRECONDITION_FAILED, $response->getStatusCode(), 'Should return PRECONDITION_FAILED status code'); - $this->assertEquals('application/json', $response->headers->get('Content-Type')); - $this->assertEquals($expectedData, $this->getDataArrayFromJsonResponse()); - } - - /** - * @test - */ - public function shouldReturnPreconditionFailedWithProperErrorsIfEmailIsInvalid() - { - $expectedData = [ - 'success' => false, - 'errors' => [ - 'email' => 'This value is not a valid email address.', - ], - ]; - $response = $this->sendData([ - 'name' => 'aaa', - 'surname' => 'aaa', - 'email' => 'test', - 'bio' => '', - 'daysInOffice' => 3, - ]); - - $this->assertEquals(JsonResponse::HTTP_PRECONDITION_FAILED, $response->getStatusCode(), 'Should return PRECONDITION_FAILED status code'); - $this->assertEquals('application/json', $response->headers->get('Content-Type')); - $this->assertEquals($expectedData, $this->getDataArrayFromJsonResponse()); - } - - /** - * @test - */ - public function shouldReturnPreconditionFailedWithProperErrorsIfEmailIsToLong() - { - $expectedData = [ - 'success' => false, - 'errors' => [ - 'email' => 'This value is too long. It should have 254 characters or less.', - ], - ]; - $response = $this->sendData([ - 'name' => 'aaa', - 'surname' => 'aaa', - 'email' => str_repeat('a', 247).'@test.pl', //length: 255 - 'bio' => '', - 'daysInOffice' => 3, - ]); - - $this->assertEquals(JsonResponse::HTTP_PRECONDITION_FAILED, $response->getStatusCode(), 'Should return PRECONDITION_FAILED status code'); - $this->assertEquals('application/json', $response->headers->get('Content-Type')); - $this->assertEquals($expectedData, $this->getDataArrayFromJsonResponse()); - } - - /** - * @test - */ - public function shouldReturnPreconditionFailedWithProperErrorsIfBioIsToLong() - { - $expectedData = [ - 'success' => false, - 'errors' => [ - 'bio' => 'This value is too long. It should have 400 characters or less.', - ], - ]; - $response = $this->sendData([ - 'name' => 'aaa', - 'surname' => 'aaa', - 'email' => 'test@test.pl', - 'bio' => str_repeat('a', 401), - 'daysInOffice' => 3, - ]); - - $this->assertEquals(JsonResponse::HTTP_PRECONDITION_FAILED, $response->getStatusCode(), 'Should return PRECONDITION_FAILED status code'); - $this->assertEquals('application/json', $response->headers->get('Content-Type')); - $this->assertEquals($expectedData, $this->getDataArrayFromJsonResponse()); - } - - /** - * @test - */ - public function shouldReturnPreconditionFailedWithProperErrorsIfDaysInOfficeIsInvalid() - { - $expectedData = [ - 'success' => false, - 'errors' => [ - 'daysInOffice' => 'This value should be a valid number.', - ], - ]; - $response = $this->sendData([ - 'name' => 'aaa', - 'surname' => 'aaa', - 'email' => 'test@test.pl', - 'bio' => '', - 'daysInOffice' => 'a', - ]); - - $this->assertEquals(JsonResponse::HTTP_PRECONDITION_FAILED, $response->getStatusCode(), 'Should return PRECONDITION_FAILED status code'); - $this->assertEquals('application/json', $response->headers->get('Content-Type')); - $this->assertEquals($expectedData, $this->getDataArrayFromJsonResponse()); - } - - /** - * @test - */ - public function shouldReturnPreconditionFailedWithProperErrorsIfDaysInOfficeHasToLowValue() - { - $expectedData = [ - 'success' => false, - 'errors' => [ - 'daysInOffice' => 'You must be at least 2 days working in the office.', - ], - ]; - $response = $this->sendData([ - 'name' => 'aaa', - 'surname' => 'aaa', - 'email' => 'test@test.pl', - 'bio' => '', - 'daysInOffice' => 1, - ]); - - $this->assertEquals(JsonResponse::HTTP_PRECONDITION_FAILED, $response->getStatusCode(), 'Should return PRECONDITION_FAILED status code'); - $this->assertEquals('application/json', $response->headers->get('Content-Type')); - $this->assertEquals($expectedData, $this->getDataArrayFromJsonResponse()); - } - - /** - * @test - */ - public function shouldReturnPreconditionFailedWithProperErrorsIfDaysInOfficeHasToHighValue() - { - $expectedData = [ - 'success' => false, - 'errors' => [ - 'daysInOffice' => 'You cannot work more than 5 days in the office.', - ], - ]; - $response = $this->sendData([ - 'name' => 'aaa', - 'surname' => 'aaa', - 'email' => 'test@test.pl', - 'bio' => '', - 'daysInOffice' => 6, - ]); - - $this->assertEquals(JsonResponse::HTTP_PRECONDITION_FAILED, $response->getStatusCode(), 'Should return PRECONDITION_FAILED status code'); - $this->assertEquals('application/json', $response->headers->get('Content-Type')); - $this->assertEquals($expectedData, $this->getDataArrayFromJsonResponse()); - } - - /** - * @test - */ - public function shouldSaveEmployeeIfProperDataIsGivenAndReturnItsId() - { - $expectedData = [ - 'success' => true, - 'id' => 1, - ]; - $data = [ - 'name' => str_repeat('a', 64), - 'surname' => str_repeat('a', 64), - 'email' => str_repeat('a', 246).'@test.pl', //length: 254 - 'bio' => str_repeat('a', 400), - 'daysInOffice' => 5, - ]; - $response = $this->sendData($data); - - $this->assertEquals(JsonResponse::HTTP_OK, $response->getStatusCode(), 'Should return OK(200) status code'); - $this->assertEquals('application/json', $response->headers->get('Content-Type')); - $this->assertEquals($expectedData, $this->getDataArrayFromJsonResponse()); - - $this->client->request('GET', '/api/employee'); - - $data['id'] = 1; - $expectedData = [$data]; - - $this->assertEquals(JsonResponse::HTTP_OK, $response->getStatusCode(), 'Should return OK(200) status code'); - $this->assertEquals('application/json', $response->headers->get('Content-Type')); - $this->assertEquals($expectedData, $this->getDataArrayFromJsonResponse()); - } - - /** - * @test - */ - public function shouldNotRequireBio() - { - $expectedData = [ - 'success' => true, - 'id' => 1, - ]; - $data = [ - 'name' => str_repeat('a', 64), - 'surname' => str_repeat('a', 64), - 'email' => str_repeat('a', 246).'@test.pl', //length: 254 - 'bio' => '', - 'daysInOffice' => 5, - ]; - $response = $this->sendData($data); - - $this->assertEquals(JsonResponse::HTTP_OK, $response->getStatusCode(), 'Should return OK(200) status code'); - $this->assertEquals('application/json', $response->headers->get('Content-Type')); - $this->assertEquals($expectedData, $this->getDataArrayFromJsonResponse()); - - $this->client->request('GET', '/api/employee'); - - $data['id'] = 1; - $expectedData = [$data]; - - $this->assertEquals(JsonResponse::HTTP_OK, $response->getStatusCode(), 'Should return OK(200) status code'); - $this->assertEquals('application/json', $response->headers->get('Content-Type')); - $this->assertEquals($expectedData, $this->getDataArrayFromJsonResponse()); - } - - /** - * @return array - */ - protected function getFixtures() - { - return []; - } - - /** - * @return string - */ - protected function getPath() - { - return '/api/employee'; - } - - protected function getMethod() - { - return 'POST'; - } - - /** - * @param array $data - * @return Response - */ - protected function sendData(array $data) - { - $this->client->request($this->getMethod(), $this->getPath(), $data); - - return $this->client->getResponse(); - } -} diff --git a/src/AppBundle/Tests/Feature/DeleteEmployeeTest.php b/src/AppBundle/Tests/Feature/DeleteEmployeeTest.php deleted file mode 100644 index a9cbcec..0000000 --- a/src/AppBundle/Tests/Feature/DeleteEmployeeTest.php +++ /dev/null @@ -1,133 +0,0 @@ - false, - ]; - - $response = $this->deleteEmployeeById(4); - $this->assertEquals(Response::HTTP_NOT_FOUND, $response->getStatusCode(), 'Should return NOT_FOUND status code'); - $this->assertEquals('application/json', $response->headers->get('Content-Type')); - $this->assertEquals($expectedData, $this->getDataArrayFromJsonResponse()); - } - - /** - * @test - */ - public function shouldDeleteCertainEmployee() - { - $expectedData = [ - 'success' => true, - ]; - $expectedEmployees = [ - [ - 'id' => 1, - 'name' => 'Martin', - 'surname' => 'Fowler', - 'email' => 'martin.fowler@fake.pl', - 'bio' => 'A British software developer, author and international public speaker on software development, specializing in object-oriented analysis and design, UML, patterns, and agile software development methodologies, including extreme programming.', - 'daysInOffice' => 2, - ], - [ - 'id' => 2, - 'name' => 'Kent', - 'surname' => 'Beck', - 'email' => 'kent.beck@fake.pl', - 'bio' => 'An American software engineer and the creator of extreme programming, a software development methodology which eschews rigid formal specification for a collaborative and iterative design process.', - 'daysInOffice' => 5, - ], - [ - 'id' => 3, - 'name' => 'Robert Cecil', - 'surname' => 'Martin', - 'email' => 'robert.martin@fake.pl', - 'bio' => 'An American software engineer and author. He is a co-author of the Agile Manifesto. He now runs a consulting firm called Clean Code.', - 'daysInOffice' => 4, - ], - ]; - - $response = $this->requestList(); - $this->assertEquals(Response::HTTP_OK, $response->getStatusCode(), 'Should return OK(200) status code'); - $this->assertEquals('application/json', $response->headers->get('Content-Type')); - $this->assertEquals($expectedEmployees, $this->getDataArrayFromJsonResponse()); - - $response = $this->deleteEmployeeById(2); - $this->assertEquals(Response::HTTP_OK, $response->getStatusCode(), 'Should return OK(200) status code'); - $this->assertEquals('application/json', $response->headers->get('Content-Type')); - $this->assertEquals($expectedData, $this->getDataArrayFromJsonResponse()); - - $response = $this->requestList(); - $this->assertEquals(Response::HTTP_OK, $response->getStatusCode(), 'Should return OK(200) status code'); - $this->assertEquals('application/json', $response->headers->get('Content-Type')); - $this->assertEquals([$expectedEmployees[0], $expectedEmployees[2]], $this->getDataArrayFromJsonResponse()); - - $response = $this->deleteEmployeeById(3); - $this->assertEquals(Response::HTTP_OK, $response->getStatusCode(), 'Should return OK(200) status code'); - $this->assertEquals('application/json', $response->headers->get('Content-Type')); - $this->assertEquals($expectedData, $this->getDataArrayFromJsonResponse()); - - $response = $this->requestList(); - $this->assertEquals(Response::HTTP_OK, $response->getStatusCode(), 'Should return OK(200) status code'); - $this->assertEquals('application/json', $response->headers->get('Content-Type')); - $this->assertEquals([$expectedEmployees[0]], $this->getDataArrayFromJsonResponse()); - - $response = $this->deleteEmployeeById(1); - $this->assertEquals(Response::HTTP_OK, $response->getStatusCode(), 'Should return OK(200) status code'); - $this->assertEquals('application/json', $response->headers->get('Content-Type')); - $this->assertEquals($expectedData, $this->getDataArrayFromJsonResponse()); - - $response = $this->requestList(); - $this->assertEquals(Response::HTTP_OK, $response->getStatusCode(), 'Should return OK(200) status code'); - $this->assertEquals('application/json', $response->headers->get('Content-Type')); - $this->assertEquals([], $this->getDataArrayFromJsonResponse()); - } - - /** - * @return array - */ - protected function getFixtures() - { - return [new LoadEmployeeData()]; - } - - /** - * @return Response - */ - protected function requestList() - { - $this->client->request('GET', '/api/employee'); - - return $this->client->getResponse(); - } - - /** - * @param int $id - * @return Response - */ - protected function deleteEmployeeById($id) - { - $this->client->request('DELETE', '/api/employee/'.$id); - - return $this->client->getResponse(); - } -} diff --git a/src/AppBundle/Tests/Feature/RetrieveEmployeeTest.php b/src/AppBundle/Tests/Feature/RetrieveEmployeeTest.php deleted file mode 100644 index 54c2d91..0000000 --- a/src/AppBundle/Tests/Feature/RetrieveEmployeeTest.php +++ /dev/null @@ -1,70 +0,0 @@ - 1, - 'name' => 'Martin', - 'surname' => 'Fowler', - 'email' => 'martin.fowler@fake.pl', - 'bio' => 'A British software developer, author and international public speaker on software development, specializing in object-oriented analysis and design, UML, patterns, and agile software development methodologies, including extreme programming.', - 'daysInOffice' => 2, - ], - [ - 'id' => 2, - 'name' => 'Kent', - 'surname' => 'Beck', - 'email' => 'kent.beck@fake.pl', - 'bio' => 'An American software engineer and the creator of extreme programming, a software development methodology which eschews rigid formal specification for a collaborative and iterative design process.', - 'daysInOffice' => 5, - ], - [ - 'id' => 3, - 'name' => 'Robert Cecil', - 'surname' => 'Martin', - 'email' => 'robert.martin@fake.pl', - 'bio' => 'An American software engineer and author. He is a co-author of the Agile Manifesto. He now runs a consulting firm called Clean Code.', - 'daysInOffice' => 4, - ], - ]; - - $this->assertEquals('application/json', $this->client->getResponse()->headers->get('Content-Type')); - - $returnedData = json_decode($this->client->getResponse()->getContent(), true); - $this->assertEquals($expectedData, $returnedData); - } - - /** - * @return array - */ - protected function getFixtures() - { - return [new LoadEmployeeData()]; - } - - /** - * @before - */ - protected function doGetRequestAndAssertOkStatusCode() - { - $this->crawler = $this->client->request('GET', '/api/employee'); - - $this->assertEquals(Response::HTTP_OK, $this->client->getResponse()->getStatusCode()); - } -} diff --git a/src/AppBundle/Tests/Feature/UpdateEmployeeTest.php b/src/AppBundle/Tests/Feature/UpdateEmployeeTest.php deleted file mode 100644 index b31d157..0000000 --- a/src/AppBundle/Tests/Feature/UpdateEmployeeTest.php +++ /dev/null @@ -1,137 +0,0 @@ - true, - 'id' => 1, - ]; - $data = [ - 'name' => str_repeat('a', 64), - 'surname' => str_repeat('a', 64), - 'email' => str_repeat('a', 246).'@test.pl', //length: 254 - 'bio' => str_repeat('a', 400), - 'daysInOffice' => 5, - ]; - $response = $this->sendData($data); - - $this->assertEquals(JsonResponse::HTTP_OK, $response->getStatusCode(), 'Should return OK(200) status code'); - $this->assertEquals('application/json', $response->headers->get('Content-Type')); - $this->assertEquals($expectedData, $this->getDataArrayFromJsonResponse()); - - $this->client->request('GET', '/api/employee'); - - $data['id'] = 1; - $expectedData = [ - $data, - [ - 'id' => 2, - 'name' => 'Kent', - 'surname' => 'Beck', - 'email' => 'kent.beck@fake.pl', - 'bio' => 'An American software engineer and the creator of extreme programming, a software development methodology which eschews rigid formal specification for a collaborative and iterative design process.', - 'daysInOffice' => 5, - ], - [ - 'id' => 3, - 'name' => 'Robert Cecil', - 'surname' => 'Martin', - 'email' => 'robert.martin@fake.pl', - 'bio' => 'An American software engineer and author. He is a co-author of the Agile Manifesto. He now runs a consulting firm called Clean Code.', - 'daysInOffice' => 4, - ], - ]; - - $this->assertEquals(JsonResponse::HTTP_OK, $response->getStatusCode(), 'Should return OK(200) status code'); - $this->assertEquals('application/json', $response->headers->get('Content-Type')); - $this->assertEquals($expectedData, $this->getDataArrayFromJsonResponse()); - } - - /** - * @test - */ - public function shouldNotRequireBio() - { - $expectedData = [ - 'success' => true, - 'id' => 1, - ]; - $data = [ - 'name' => str_repeat('a', 64), - 'surname' => str_repeat('a', 64), - 'email' => str_repeat('a', 246).'@test.pl', //length: 254 - 'bio' => '', - 'daysInOffice' => 5, - ]; - $response = $this->sendData($data); - - $this->assertEquals(JsonResponse::HTTP_OK, $response->getStatusCode(), 'Should return OK(200) status code'); - $this->assertEquals('application/json', $response->headers->get('Content-Type')); - $this->assertEquals($expectedData, $this->getDataArrayFromJsonResponse()); - - $this->client->request('GET', '/api/employee'); - - $data['id'] = 1; - $expectedData = [ - $data, - [ - 'id' => 2, - 'name' => 'Kent', - 'surname' => 'Beck', - 'email' => 'kent.beck@fake.pl', - 'bio' => 'An American software engineer and the creator of extreme programming, a software development methodology which eschews rigid formal specification for a collaborative and iterative design process.', - 'daysInOffice' => 5, - ], - [ - 'id' => 3, - 'name' => 'Robert Cecil', - 'surname' => 'Martin', - 'email' => 'robert.martin@fake.pl', - 'bio' => 'An American software engineer and author. He is a co-author of the Agile Manifesto. He now runs a consulting firm called Clean Code.', - 'daysInOffice' => 4, - ], - ]; - - $this->assertEquals(JsonResponse::HTTP_OK, $response->getStatusCode(), 'Should return OK(200) status code'); - $this->assertEquals('application/json', $response->headers->get('Content-Type')); - $this->assertEquals($expectedData, $this->getDataArrayFromJsonResponse()); - } - - /** - * @return array - */ - protected function getFixtures() - { - return [new LoadEmployeeData()]; - } - - /** - * @return string - */ - protected function getPath() - { - return '/api/employee/1'; - } - - /** - * @return string - */ - protected function getMethod() - { - return 'PUT'; - } -} diff --git a/src/AppBundle/Tests/Traits/GetDataArrayFromJsonResponseTrait.php b/src/AppBundle/Tests/Traits/GetDataArrayFromJsonResponseTrait.php deleted file mode 100644 index 749533a..0000000 --- a/src/AppBundle/Tests/Traits/GetDataArrayFromJsonResponseTrait.php +++ /dev/null @@ -1,19 +0,0 @@ -client->getResponse()->getContent(), true); - } -}