-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #200 from Zales0123/me-endpoint
Currently logged in customer endpoint
- Loading branch information
Showing
7 changed files
with
140 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sylius\ShopApiPlugin\Controller\Customer; | ||
|
||
use FOS\RestBundle\View\View; | ||
use FOS\RestBundle\View\ViewHandlerInterface; | ||
use Sylius\Component\Core\Model\ShopUserInterface; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; | ||
use Webmozart\Assert\Assert; | ||
|
||
final class LoggedInCustomerDetailsAction | ||
{ | ||
/** | ||
* @var ViewHandlerInterface | ||
*/ | ||
private $viewHandler; | ||
|
||
/** | ||
* @var TokenStorageInterface | ||
*/ | ||
private $tokenStorage; | ||
|
||
/** | ||
* @param ViewHandlerInterface $viewHandler | ||
* @param TokenStorageInterface $tokenStorage | ||
*/ | ||
public function __construct(ViewHandlerInterface $viewHandler, TokenStorageInterface $tokenStorage) | ||
{ | ||
$this->viewHandler = $viewHandler; | ||
$this->tokenStorage = $tokenStorage; | ||
} | ||
|
||
public function __invoke(Request $request): Response | ||
{ | ||
/** @var ShopUserInterface $user */ | ||
$user = $this->tokenStorage->getToken()->getUser(); | ||
|
||
Assert::isInstanceOf($user, ShopUserInterface::class); | ||
|
||
$customer = $user->getCustomer(); | ||
|
||
return $this->viewHandler->handle(View::create([ | ||
'firstName' => $customer->getFirstName(), | ||
'lastName' => $customer->getLastName(), | ||
'email' => $customer->getEmail(), | ||
], Response::HTTP_OK)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
shop_api_me: | ||
path: /me | ||
methods: [GET] | ||
defaults: | ||
_controller: sylius.shop_api_plugin.controller.customer.logged_in_customer_details_action |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\Sylius\ShopApiPlugin\Controller\Customer; | ||
|
||
use Lakion\ApiTestCase\JsonApiTestCase; | ||
use Symfony\Component\HttpFoundation\Response; | ||
|
||
final class LoggedInCustomerDetailsActionTest extends JsonApiTestCase | ||
{ | ||
/** | ||
* @test | ||
*/ | ||
public function it_shows_currently_logged_in_customer_details() | ||
{ | ||
$this->loadFixturesFromFile('customer.yml'); | ||
|
||
$data = | ||
<<<EOT | ||
{ | ||
"_username": "oliver@queen.com", | ||
"_password": "123pa\$\$word" | ||
} | ||
EOT; | ||
|
||
$this->client->request('POST', '/shop-api/login_check', [], [], ['CONTENT_TYPE' => 'application/json', 'ACCEPT' => 'application/json'], $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', | ||
]); | ||
|
||
$response = $this->client->getResponse(); | ||
$this->assertResponse($response, 'customer/logged_in_customer_details_response', Response::HTTP_OK); | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
tests/Responses/Expected/customer/logged_in_customer_details_response.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"firstName": "Oliver", | ||
"lastName": "Queen", | ||
"email": "oliver@queen.com" | ||
} |