diff --git a/.gitignore b/.gitignore index c8153b578..8ae504cf2 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ /composer.lock /vendor/ +/bin/* +!/bin/.gitkeep diff --git a/.travis.yml b/.travis.yml index 38eb90b9a..757fce177 100644 --- a/.travis.yml +++ b/.travis.yml @@ -45,5 +45,5 @@ before_script: script: - composer validate --strict - - vendor/bin/phpspec run --format dot -vvv --no-interaction - - vendor/bin/phpunit + - bin/phpspec run --format dot -vvv --no-interaction + - bin/phpunit diff --git a/bin/.gitkeep b/bin/.gitkeep new file mode 100644 index 000000000..e69de29bb diff --git a/composer.json b/composer.json index 59c8d7e8b..b8cf5d582 100644 --- a/composer.json +++ b/composer.json @@ -24,12 +24,20 @@ "Sylius\\ShopApiPlugin\\": "src/" } }, + "scripts": { + "fix": [ + "bin/ecs check --ansi --no-progress-bar spec src tests/Controller tests/DataFixtures tests/Request --fix" + ] + }, "autoload-dev": { "psr-4": { "Tests\\Sylius\\ShopApiPlugin\\": "tests/" }, "classmap": ["tests/Application/app/AppKernel.php"] }, + "config": { + "bin-dir": "bin" + }, "suggest": { "nelmio/cors-bundle": "allows you to send Cross-Origin Ajax API Request" } diff --git a/spec/Handler/UpdateCustomerHandlerSpec.php b/spec/Handler/UpdateCustomerHandlerSpec.php index 6aeac609a..782765c4e 100644 --- a/spec/Handler/UpdateCustomerHandlerSpec.php +++ b/spec/Handler/UpdateCustomerHandlerSpec.php @@ -13,8 +13,7 @@ final class UpdateCustomerHandlerSpec extends ObjectBehavior { function let( RepositoryInterface $customerRepository - ) - { + ) { $this->beConstructedWith( $customerRepository ); @@ -23,8 +22,7 @@ function let( function it_updates_customer( RepositoryInterface $customerRepository, CustomerInterface $customer - ) - { + ) { $customerRepository->findOneBy(['email' => 'sherlock@holmes.com'])->willReturn($customer); $customer->getId()->willReturn('USER_ID'); diff --git a/spec/Provider/LoggedInUserProviderSpec.php b/spec/Provider/LoggedInUserProviderSpec.php index 80ee4304c..bdf36eed9 100644 --- a/spec/Provider/LoggedInUserProviderSpec.php +++ b/spec/Provider/LoggedInUserProviderSpec.php @@ -1,4 +1,5 @@ getUser()->shouldBeCalled()->willReturn($shopUser); $tokenStorage->getToken()->shouldBeCalled()->willReturn($token); diff --git a/src/Controller/AddressBook/CreateAddressAction.php b/src/Controller/AddressBook/CreateAddressAction.php index 5d0014065..07aaca537 100644 --- a/src/Controller/AddressBook/CreateAddressAction.php +++ b/src/Controller/AddressBook/CreateAddressAction.php @@ -112,12 +112,10 @@ public function __invoke(Request $request): Response if (($customer = $user->getCustomer()) !== null) { $this->bus->handle(new CreateAddress($addressModel, $user->getEmail())); - $view = View::create($this->getLastInsertedAddress($customer), Response::HTTP_CREATED); - } else { - $view = View::create(['message' => 'The user is not a customer'], Response::HTTP_BAD_REQUEST); + return $this->viewHandler->handle(View::create($this->getLastInsertedAddress($customer), Response::HTTP_CREATED)); } - return $this->viewHandler->handle($view); + return $this->viewHandler->handle(View::create(['message' => 'The user is not a customer'], Response::HTTP_BAD_REQUEST)); } /** diff --git a/src/Controller/AddressBook/RemoveAddressAction.php b/src/Controller/AddressBook/RemoveAddressAction.php index e153df307..ada3361c4 100644 --- a/src/Controller/AddressBook/RemoveAddressAction.php +++ b/src/Controller/AddressBook/RemoveAddressAction.php @@ -67,7 +67,7 @@ public function __construct( /** * Removes an address from the address book - * + * * @param Request $request */ public function __invoke(Request $request): Response @@ -91,11 +91,10 @@ public function __invoke(Request $request): Response if ($user->getCustomer() !== null) { $this->bus->handle(new RemoveAddress($removeAddressRequest->id(), $user->getEmail())); - $view = View::create(null, Response::HTTP_NO_CONTENT); - } else { - $view = View::create(['message' => 'The user is not a customer'], Response::HTTP_BAD_REQUEST); + + return $this->viewHandler->handle(View::create(null, Response::HTTP_NO_CONTENT)); } - return $this->viewHandler->handle($view); + return $this->viewHandler->handle(View::create(['message' => 'The user is not a customer'], Response::HTTP_BAD_REQUEST)); } } diff --git a/src/Controller/AddressBook/SetDefaultAddressAction.php b/src/Controller/AddressBook/SetDefaultAddressAction.php index 6b996c40a..651d511bd 100644 --- a/src/Controller/AddressBook/SetDefaultAddressAction.php +++ b/src/Controller/AddressBook/SetDefaultAddressAction.php @@ -94,11 +94,9 @@ public function __invoke(Request $request) if ($user->getCustomer() !== null) { $this->bus->handle(new SetDefaultAddress($request->attributes->get('id'), $user->getEmail())); - $view = View::create(null, Response::HTTP_NO_CONTENT); - } else { - $view = View::create(['message' => 'The user is not a customer'], Response::HTTP_BAD_REQUEST); + return $this->viewHandler->handle(View::create(null, Response::HTTP_NO_CONTENT)); } - return $this->viewHandler->handle($view); + return $this->viewHandler->handle(View::create(['message' => 'The user is not a customer'], Response::HTTP_BAD_REQUEST)); } } diff --git a/src/Controller/AddressBook/ShowAddressBookAction.php b/src/Controller/AddressBook/ShowAddressBookAction.php index 87d3b5013..c5edf02e9 100644 --- a/src/Controller/AddressBook/ShowAddressBookAction.php +++ b/src/Controller/AddressBook/ShowAddressBookAction.php @@ -11,7 +11,6 @@ use Sylius\Component\Core\Model\ShopUserInterface; use Sylius\ShopApiPlugin\Factory\AddressBookViewFactoryInterface; use Sylius\ShopApiPlugin\Provider\LoggedInUserProviderInterface; -use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Security\Core\Exception\TokenNotFoundException; @@ -60,6 +59,7 @@ public function __invoke(): Response } $customer = $user->getCustomer(); + if ($customer instanceof Customer) { $addressViews = $customer->getAddresses()->map( function (AddressInterface $address) use ($customer) { @@ -67,11 +67,9 @@ function (AddressInterface $address) use ($customer) { } ); - $view = View::create($addressViews, Response::HTTP_OK); - } else { - $view = View::create(['message' => 'The user is not a customer'], Response::HTTP_BAD_REQUEST); + return $this->viewHandler->handle(View::create($addressViews, Response::HTTP_OK)); } - return $this->viewHandler->handle($view); + return $this->viewHandler->handle(View::create(['message' => 'The user is not a customer'], Response::HTTP_BAD_REQUEST)); } } diff --git a/src/Controller/AddressBook/UpdateAddressAction.php b/src/Controller/AddressBook/UpdateAddressAction.php index 3a0a7d7f2..b58daf60a 100644 --- a/src/Controller/AddressBook/UpdateAddressAction.php +++ b/src/Controller/AddressBook/UpdateAddressAction.php @@ -17,7 +17,6 @@ use Sylius\ShopApiPlugin\Provider\LoggedInUserProviderInterface; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; -use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; use Symfony\Component\Security\Core\Exception\TokenNotFoundException; use Symfony\Component\Validator\Validator\ValidatorInterface; @@ -65,7 +64,6 @@ final class UpdateAddressAction * @param ValidationErrorViewFactoryInterface $validationErrorViewFactory * @param AddressBookViewFactoryInterface $addressViewFactory * @param AddressRepositoryInterface $addressRepository - * @param TokenStorageInterface $tokenStorage * @param LoggedInUserProviderInterface $loggedInUserProvider */ public function __construct( @@ -88,7 +86,7 @@ public function __construct( /** * Updates an address in the address book - * + * * @param Request $request * @param string|int $id */ @@ -117,14 +115,12 @@ public function __invoke(Request $request, $id): Response /** @var AddressInterface $updatedAddress */ $updatedAddress = $this->addressRepository->findOneBy(['id' => $id]); - $view = View::create( + return $this->viewHandler->handle(View::create( $this->addressBookViewFactory->create($updatedAddress, $user->getCustomer()), Response::HTTP_OK - ); - } else { - $view = View::create(['message' => 'The user is not a customer'], Response::HTTP_BAD_REQUEST); + )); } - return $this->viewHandler->handle($view); + return $this->viewHandler->handle(View::create(['message' => 'The user is not a customer'], Response::HTTP_BAD_REQUEST)); } } diff --git a/src/Validator/AddressExistsValidator.php b/src/Validator/AddressExistsValidator.php index 4e435ad1e..dee78d9c7 100644 --- a/src/Validator/AddressExistsValidator.php +++ b/src/Validator/AddressExistsValidator.php @@ -20,20 +20,20 @@ final class AddressExistsValidator extends ConstraintValidator /** * @var LoggedInUserProviderInterface */ - private $currentUserProvider; + private $loggedInUserProvider; /** * AddressExistsValidator constructor. * * @param AddressRepositoryInterface $addressRepository - * @param LoggedInUserProviderInterface $currentUserProvider + * @param LoggedInUserProviderInterface $loggedInUserProvider */ public function __construct( AddressRepositoryInterface $addressRepository, - LoggedInUserProviderInterface $currentUserProvider + LoggedInUserProviderInterface $loggedInUserProvider ) { $this->addressRepository = $addressRepository; - $this->currentUserProvider = $currentUserProvider; + $this->loggedInUserProvider = $loggedInUserProvider; } /** @@ -48,7 +48,7 @@ public function validate($id, Constraint $constraint) return $this->context->addViolation($constraint->message); } - $user = $this->currentUserProvider->provide(); + $user = $this->loggedInUserProvider->provide(); if ($address->getCustomer()->getEmail() !== $user->getEmail()) { return $this->context->addViolation($constraint->message); diff --git a/tests/Controller/AddressBookShowApiTest.php b/tests/Controller/AddressBookShowApiTest.php index 243aa886b..1112c71e3 100644 --- a/tests/Controller/AddressBookShowApiTest.php +++ b/tests/Controller/AddressBookShowApiTest.php @@ -35,5 +35,4 @@ public function it_returns_an_unauthorized_exception_if_there_is_no_logged_in_us $this->assertResponseCode($response, Response::HTTP_UNAUTHORIZED); } - } diff --git a/tests/Controller/CustomerUpdateCustomerApiTest.php b/tests/Controller/CustomerUpdateCustomerApiTest.php index b506f94fa..e0e81be45 100644 --- a/tests/Controller/CustomerUpdateCustomerApiTest.php +++ b/tests/Controller/CustomerUpdateCustomerApiTest.php @@ -44,7 +44,7 @@ public function it_updates_customer() $this->assertResponse($response, 'customer/update_customer', Response::HTTP_OK); /** @var CustomerInterface $customer */ - $customer = $customerRepository->findOneByEmail("oliver@queen.com"); + $customer = $customerRepository->findOneByEmail('oliver@queen.com'); Assert::assertEquals($customer->getFirstName(), 'New name'); Assert::assertEquals($customer->getLastName(), 'New lastName'); diff --git a/tests/Request/UpdateCustomerRequestTest.php b/tests/Request/UpdateCustomerRequestTest.php index 5096de54c..c6233436a 100644 --- a/tests/Request/UpdateCustomerRequestTest.php +++ b/tests/Request/UpdateCustomerRequestTest.php @@ -22,8 +22,8 @@ public function it_creates_update_customer_command() 'email' => 'ivan.matas@locastic.com', 'birthday' => '2017-11-01', 'gender' => 'm', - 'phoneNumber' => "125125112", - 'subscribedToNewsletter' => true + 'phoneNumber' => '125125112', + 'subscribedToNewsletter' => true, ], [])) ; $this->assertEquals($updateCustomerRequest->getCommand(), new UpdateCustomer(