Skip to content

Commit

Permalink
bug #573 Extending customer details tests (mamazu)
Browse files Browse the repository at this point in the history
This PR was merged into the 1.0-dev branch.

Discussion
----------

This PR fixes/changes the folloing things:
* (DX) In the tests the customer details test the login request is replaced with the trait that does the request
* (DX) Removed credentials from "customer is not logged in" test.
* (DX) Extracted requesting the endpoint into a function (and now with globally defined `CONTENT_TYPE_HEADER`)
* Fixing type bug. If you instanciate the `UpdateCustomer` with null for `subscribedToNewsletter` then the getter for it will crash as null is not part of the return type.

Commits
-------

33d5e9c Extending customer details tests
42f81a7 Fixing tests
024ab41 Adding test for missing newsletter parameter
  • Loading branch information
lchrusciel authored Oct 25, 2019
2 parents 288e872 + 024ab41 commit c684c4d
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 34 deletions.
2 changes: 1 addition & 1 deletion src/Command/Customer/UpdateCustomer.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function __construct(
$this->birthday = $birthday;
$this->gender = $gender;
$this->phoneNumber = $phoneNumber;
$this->subscribedToNewsletter = $subscribedToNewsletter;
$this->subscribedToNewsletter = $subscribedToNewsletter ?? false;
}

public function firstName(): string
Expand Down
43 changes: 12 additions & 31 deletions tests/Controller/Customer/LoggedInCustomerDetailsActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,22 @@

use Symfony\Component\HttpFoundation\Response;
use Tests\Sylius\ShopApiPlugin\Controller\JsonApiTestCase;
use Tests\Sylius\ShopApiPlugin\Controller\Utils\ShopUserLoginTrait;

final class LoggedInCustomerDetailsActionTest extends JsonApiTestCase
{
use ShopUserLoginTrait;

/**
* @test
*/
public function it_shows_currently_logged_in_customer_details(): void
{
$this->loadFixturesFromFiles(['channel.yml', 'customer.yml']);

$data =
<<<JSON
{
"email": "oliver@queen.com",
"password": "123password"
}
JSON;

$this->client->request('POST', '/shop-api/login', [], [], self::CONTENT_TYPE_HEADER, $data);

$response = json_decode($this->client->getResponse()->getContent(), true);
$this->client->setServerParameter('HTTP_Authorization', sprintf('Bearer %s', $response['token']));

$this->client->request('GET', '/shop-api/me', [], [], [
'CONTENT_TYPE' => 'application/json',
'ACCEPT' => 'application/json',
]);
$this->logInUser('oliver@queen.com', '123password');

$response = $this->client->getResponse();
$response = $this->getCustomerDetails();
$this->assertResponse($response, 'customer/logged_in_customer_details_response', Response::HTTP_OK);
}

Expand All @@ -45,20 +32,14 @@ public function it_does_not_allow_to_show_customer_details_without_being_logged_
{
$this->loadFixturesFromFiles(['channel.yml', 'customer.yml']);

$data =
<<<JSON
{
"email": "oliver@queen.com",
"password": "123password"
}
JSON;
$response = $this->getCustomerDetails();
$this->assertResponseCode($response, Response::HTTP_UNAUTHORIZED);
}

$this->client->request('GET', '/shop-api/me', [], [], [
'CONTENT_TYPE' => 'application/json',
'ACCEPT' => 'application/json',
]);
private function getCustomerDetails(): Response
{
$this->client->request('GET', '/shop-api/me', [], [], self::CONTENT_TYPE_HEADER);

$response = $this->client->getResponse();
$this->assertResponseCode($response, Response::HTTP_UNAUTHORIZED);
return $this->client->getResponse();
}
}
34 changes: 34 additions & 0 deletions tests/Controller/Customer/UpdateCustomerApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,40 @@ public function it_updates_customer(): void
Assert::assertEquals($customer->isSubscribedToNewsletter(), true);
}

public function it_updates_customer_when_newsletter_is_not_set(): void
{
$this->loadFixturesFromFiles(['channel.yml', 'customer.yml']);
$this->logInUser('oliver@queen.com', '123password');

/** @var CustomerRepositoryInterface $customerRepository */
$customerRepository = $this->get('sylius.repository.customer');

$data =
<<<JSON
{
"firstName": "New name",
"lastName": "New lastName",
"birthday": "2017-11-01",
"gender": "m",
"phoneNumber": "0918972132",
}
JSON;
$this->client->request('PUT', '/shop-api/me', [], [], self::CONTENT_TYPE_HEADER, $data);
$response = $this->client->getResponse();
$this->assertResponse($response, 'customer/update_customer', Response::HTTP_OK);

/** @var CustomerInterface $customer */
$customer = $customerRepository->findOneByEmail('oliver@queen.com');

Assert::assertEquals($customer->getFirstName(), 'New name');
Assert::assertEquals($customer->getLastName(), 'New lastName');
Assert::assertEquals($customer->getEmail(), 'oliver@queen.com');
Assert::assertEquals($customer->getBirthday(), new \DateTimeImmutable('2017-11-01'));
Assert::assertEquals($customer->getGender(), 'm');
Assert::assertEquals($customer->getPhoneNumber(), '0918972132');
Assert::assertEquals($customer->isSubscribedToNewsletter(), false);
}

/**
* @test
*/
Expand Down
1 change: 1 addition & 0 deletions tests/DataFixtures/ORM/customer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Sylius\Component\Core\Model\Customer:
gender: "m"
group: "@retail"
phoneNumber: "0212115512"
birthday: <dateTimeBetween("-200 days", "now")>

hater:
firstName: "Slade"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"firstName": "Oliver",
"lastName": "Queen",
"gender": "m",
"birthday": "@string@.isDateTime()",
"group": {
"id": @integer@,
"code": "retail",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"firstName": "Oliver",
"lastName": "Queen",
"gender": "m",
"birthday": "@string@.isDateTime()",
"group": {
"id": @integer@,
"code": "retail",
Expand Down Expand Up @@ -56,6 +57,7 @@
"firstName": "Oliver",
"lastName": "Queen",
"gender": "m",
"birthday": "@string@.isDateTime()",
"group": {
"id": @integer@,
"code": "retail",
Expand Down Expand Up @@ -95,6 +97,7 @@
"firstName": "Oliver",
"lastName": "Queen",
"gender": "m",
"birthday": "@string@.isDateTime()",
"group": {
"id": @integer@,
"code": "retail",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"lastName": "Queen",
"email": "oliver@queen.com",
"gender": "m",
"birthday": "@string@.isDateTime()",
"phoneNumber": "0212115512",
"subscribedToNewsletter": false
}
4 changes: 2 additions & 2 deletions tests/Responses/Expected/customer/update_customer.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"id": @integer@,
"id": "@integer@",
"firstName": "New name",
"lastName": "New lastName",
"email": "oliver@queen.com",
"birthday": "@string@",
"birthday": "@string@.isDateTime()",
"gender": "m",
"phoneNumber": "0918972132",
"subscribedToNewsletter": true
Expand Down

0 comments on commit c684c4d

Please sign in to comment.