Skip to content

Commit

Permalink
Random minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
lchrusciel committed Aug 31, 2018
1 parent 56c8602 commit f9c8d42
Show file tree
Hide file tree
Showing 15 changed files with 40 additions and 44 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
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
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,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"
}
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 @@ -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));
}

/**
Expand Down
9 changes: 4 additions & 5 deletions src/Controller/AddressBook/RemoveAddressAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function __construct(

/**
* Removes an address from the address book
*
*
* @param Request $request
*/
public function __invoke(Request $request): Response
Expand All @@ -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));
}
}
6 changes: 2 additions & 4 deletions src/Controller/AddressBook/SetDefaultAddressAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
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 @@ -60,18 +59,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));
}
}
12 changes: 4 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 @@ -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(
Expand All @@ -88,7 +86,7 @@ public function __construct(

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

/**
Expand All @@ -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);
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 f9c8d42

Please sign in to comment.