From 01939069fc4711f121fc324b8b6dda58e0b38f96 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Chru=C5=9Bciel?= Date: Fri, 31 Aug 2018 19:28:20 +0200 Subject: [PATCH] Random minor fixes --- .gitignore | 2 ++ .travis.yml | 2 ++ bin/.gitkeep | 0 composer.json | 8 ++++++++ spec/Handler/UpdateCustomerHandlerSpec.php | 6 ++---- spec/Provider/LoggedInUserProviderSpec.php | 6 +++--- src/Controller/AddressBook/CreateAddressAction.php | 6 ++---- src/Controller/AddressBook/RemoveAddressAction.php | 8 +++----- .../AddressBook/SetDefaultAddressAction.php | 6 ++---- src/Controller/AddressBook/ShowAddressBookAction.php | 8 +++----- src/Controller/AddressBook/UpdateAddressAction.php | 11 +++-------- src/Validator/AddressExistsValidator.php | 9 +++++---- tests/Controller/AddressBookShowApiTest.php | 1 - tests/Controller/CustomerUpdateCustomerApiTest.php | 2 +- tests/Request/UpdateCustomerRequestTest.php | 4 ++-- 15 files changed, 38 insertions(+), 41 deletions(-) create mode 100644 bin/.gitkeep 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..822c0820f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -45,5 +45,7 @@ before_script: script: - composer validate --strict + - composer analyse + - vendor/bin/phpspec run --format dot -vvv --no-interaction - vendor/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..5568fa7cf 100644 --- a/composer.json +++ b/composer.json @@ -24,6 +24,14 @@ "Sylius\\ShopApiPlugin\\": "src/" } }, + "scripts": { + "fix": [ + "vendor/bin/ecs check --ansi --no-progress-bar spec src tests/Controller tests/DataFixtures tests/Request --fix" + ], + "analyse": [ + "vendor/bin/ecs check --ansi --no-progress-bar spec src tests/Controller tests/DataFixtures tests/Request" + ] + }, "autoload-dev": { "psr-4": { "Tests\\Sylius\\ShopApiPlugin\\": "tests/" 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 ad95ea745..f0c52d15c 100644 --- a/src/Controller/AddressBook/CreateAddressAction.php +++ b/src/Controller/AddressBook/CreateAddressAction.php @@ -84,12 +84,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)); } /** Returns the id that was inserted last in the address book */ diff --git a/src/Controller/AddressBook/RemoveAddressAction.php b/src/Controller/AddressBook/RemoveAddressAction.php index 80e951ece..f8b68b83a 100644 --- a/src/Controller/AddressBook/RemoveAddressAction.php +++ b/src/Controller/AddressBook/RemoveAddressAction.php @@ -48,7 +48,6 @@ public function __construct( $this->loggedInUserProvider = $loggedInUserProvider; } - /** Removes an address from the address book */ public function __invoke(Request $request): Response { $removeAddressRequest = new RemoveAddressRequest($request); @@ -70,11 +69,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 6224ff885..71826579f 100644 --- a/src/Controller/AddressBook/SetDefaultAddressAction.php +++ b/src/Controller/AddressBook/SetDefaultAddressAction.php @@ -71,11 +71,9 @@ public function __invoke(Request $request): Response 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 f5864750c..ea4568527 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; @@ -47,6 +46,7 @@ public function __invoke(): Response } $customer = $user->getCustomer(); + if ($customer instanceof Customer) { $addressViews = $customer->getAddresses()->map( function (AddressInterface $address) use ($customer) { @@ -54,11 +54,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 3a1bc427b..46c181e87 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; @@ -63,8 +62,6 @@ public function __construct( } /** - * Updates an address in the address book - * * @param Request $request * @param string|int $id */ @@ -93,14 +90,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 1fe98e401..6a683d7ba 100644 --- a/src/Validator/AddressExistsValidator.php +++ b/src/Validator/AddressExistsValidator.php @@ -15,15 +15,16 @@ final class AddressExistsValidator extends ConstraintValidator { /** @var AddressRepositoryInterface */ private $addressRepository; + /** @var LoggedInUserProviderInterface */ - private $currentUserProvider; + private $loggedInUserProvider; public function __construct( AddressRepositoryInterface $addressRepository, - LoggedInUserProviderInterface $currentUserProvider + LoggedInUserProviderInterface $loggedInUserProvider ) { $this->addressRepository = $addressRepository; - $this->currentUserProvider = $currentUserProvider; + $this->loggedInUserProvider = $loggedInUserProvider; } /** @@ -38,7 +39,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(