Skip to content

Commit

Permalink
Random minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
lchrusciel committed Sep 3, 2018
1 parent 782d2d1 commit 0193906
Show file tree
Hide file tree
Showing 15 changed files with 38 additions and 41 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
/composer.lock
/vendor/
/bin/*
!/bin/.gitkeep
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,7 @@ before_script:
script:
- composer validate --strict

- composer analyse

- vendor/bin/phpspec run --format dot -vvv --no-interaction
- vendor/bin/phpunit
Empty file added bin/.gitkeep
Empty file.
8 changes: 8 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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/"
Expand Down
6 changes: 2 additions & 4 deletions spec/Handler/UpdateCustomerHandlerSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ final class UpdateCustomerHandlerSpec extends ObjectBehavior
{
function let(
RepositoryInterface $customerRepository
)
{
) {
$this->beConstructedWith(
$customerRepository
);
Expand All @@ -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');
Expand Down
6 changes: 3 additions & 3 deletions spec/Provider/LoggedInUserProviderSpec.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
<?php

declare(strict_types=1);

namespace spec\Sylius\ShopApiPlugin\Provider;

use PhpSpec\ObjectBehavior;
use Sylius\Component\Core\Model\ShopUserInterface;
use Sylius\ShopApiPlugin\Provider\LoggedInUserProviderInterface;
use Symfony\Component\Security\Core\Exception\TokenNotFoundException;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Exception\TokenNotFoundException;

final class LoggedInUserProviderSpec extends ObjectBehavior
{
Expand Down Expand Up @@ -36,8 +37,7 @@ function it_returns_the_logged_in_user_if_there_is_one(
TokenStorageInterface $tokenStorage,
TokenInterface $token,
ShopUserInterface $shopUser
)
{
) {
$token->getUser()->shouldBeCalled()->willReturn($shopUser);
$tokenStorage->getToken()->shouldBeCalled()->willReturn($token);

Expand Down
6 changes: 2 additions & 4 deletions src/Controller/AddressBook/CreateAddressAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
8 changes: 3 additions & 5 deletions src/Controller/AddressBook/RemoveAddressAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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));
}
}
6 changes: 2 additions & 4 deletions src/Controller/AddressBook/SetDefaultAddressAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
8 changes: 3 additions & 5 deletions src/Controller/AddressBook/ShowAddressBookAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -47,18 +46,17 @@ public function __invoke(): Response
}

$customer = $user->getCustomer();

if ($customer instanceof Customer) {
$addressViews = $customer->getAddresses()->map(
function (AddressInterface $address) use ($customer) {
return $this->addressBookViewFactory->create($address, $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));
}
}
11 changes: 3 additions & 8 deletions src/Controller/AddressBook/UpdateAddressAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -63,8 +62,6 @@ public function __construct(
}

/**
* Updates an address in the address book
*
* @param Request $request
* @param string|int $id
*/
Expand Down Expand Up @@ -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));
}
}
9 changes: 5 additions & 4 deletions src/Validator/AddressExistsValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand All @@ -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);
Expand Down
1 change: 0 additions & 1 deletion tests/Controller/AddressBookShowApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,4 @@ public function it_returns_an_unauthorized_exception_if_there_is_no_logged_in_us

$this->assertResponseCode($response, Response::HTTP_UNAUTHORIZED);
}

}
2 changes: 1 addition & 1 deletion tests/Controller/CustomerUpdateCustomerApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
4 changes: 2 additions & 2 deletions tests/Request/UpdateCustomerRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 0193906

Please sign in to comment.