forked from Sylius/Sylius
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Admin] Extracted notification fetching to services
- Loading branch information
1 parent
c276708
commit af6ec66
Showing
12 changed files
with
251 additions
and
45 deletions.
There are no files selected for viewing
86 changes: 86 additions & 0 deletions
86
src/Sylius/Bundle/AdminBundle/Controller/NotificationController.php
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,86 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Sylius package. | ||
* | ||
* (c) Paweł Jędrzejewski | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Sylius\Bundle\AdminBundle\Controller; | ||
|
||
use GuzzleHttp\ClientInterface; | ||
use GuzzleHttp\Exception\GuzzleException; | ||
use GuzzleHttp\Psr7\Uri; | ||
use Http\Message\MessageFactory; | ||
use Psr\Http\Message\UriInterface; | ||
use Sylius\Bundle\CoreBundle\Application\Kernel; | ||
use Symfony\Component\HttpFoundation\JsonResponse; | ||
use Symfony\Component\HttpFoundation\Request; | ||
|
||
/** | ||
* @author Jan Góralski <jan.goralski@lakion.com> | ||
*/ | ||
class NotificationController | ||
{ | ||
/** | ||
* @var ClientInterface | ||
*/ | ||
protected $client; | ||
|
||
/** | ||
* @var MessageFactory | ||
*/ | ||
protected $messageFactory; | ||
|
||
/** | ||
* @var UriInterface | ||
*/ | ||
protected $hubUri; | ||
|
||
/** | ||
* @param ClientInterface $client | ||
* @param MessageFactory $messageFactory | ||
* @param string $hubUri | ||
*/ | ||
public function __construct(ClientInterface $client, MessageFactory $messageFactory, $hubUri) | ||
{ | ||
$this->client = $client; | ||
$this->messageFactory = $messageFactory; | ||
$this->hubUri = new Uri($hubUri); | ||
} | ||
|
||
/** | ||
* @param Request $request | ||
* | ||
* @return JsonResponse | ||
*/ | ||
public function getVersionAction(Request $request) | ||
{ | ||
$content = [ | ||
'version' => Kernel::VERSION, | ||
'hostname' => $request->getHost(), | ||
'locale' => $request->getLocale(), | ||
'user_agent' => $request->headers->get('User-Agent'), | ||
]; | ||
|
||
$headers = ['Content-Type' => 'application/json']; | ||
|
||
$hubRequest = $this->messageFactory->createRequest( | ||
Request::METHOD_GET, | ||
$this->hubUri, | ||
$headers, | ||
json_encode($content) | ||
); | ||
|
||
try { | ||
$hubResponse = $this->client->send($hubRequest, ['verify' => false]); | ||
} catch (GuzzleException $exception) { | ||
return JsonResponse::create('', JsonResponse::HTTP_NO_CONTENT); | ||
} | ||
|
||
return JsonResponse::fromJsonString($hubResponse->getBody()->getContents()); | ||
} | ||
} |
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
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
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
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
108 changes: 108 additions & 0 deletions
108
src/Sylius/Bundle/AdminBundle/Tests/Controller/NotificationControllerTest.php
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,108 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Sylius package. | ||
* | ||
* (c) Paweł Jędrzejewski | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Sylius\Bundle\AdminBundle\Tests\Controller; | ||
|
||
use GuzzleHttp\ClientInterface; | ||
use GuzzleHttp\Exception\ConnectException; | ||
use Http\Message\MessageFactory; | ||
use Prophecy\Argument; | ||
use Psr\Http\Message\RequestInterface; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\StreamInterface; | ||
use Sylius\Bundle\AdminBundle\Controller\NotificationController; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\JsonResponse; | ||
|
||
/** | ||
* @author Jan Góralski <jan.goralski@lakion.com> | ||
*/ | ||
final class NotificationControllerTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* @var ClientInterface | ||
*/ | ||
private $client; | ||
|
||
/** | ||
* @var MessageFactory | ||
*/ | ||
private $messageFactory; | ||
|
||
/** | ||
* @var NotificationController | ||
*/ | ||
private $controller; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private static $hubUri = 'www.doesnotexist.test.com'; | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function it_returns_an_empty_json_response_upon_client_exception() | ||
{ | ||
$this->messageFactory->createRequest(Argument::cetera()) | ||
->willReturn($this->prophesize(RequestInterface::class)->reveal()) | ||
; | ||
|
||
$this->client->send(Argument::cetera())->willThrow(ConnectException::class); | ||
|
||
$emptyResponse = $this->controller->getVersionAction(new Request()); | ||
|
||
$this->assertEquals(JsonResponse::HTTP_NO_CONTENT, $emptyResponse->getStatusCode()); | ||
$this->assertEquals('""', $emptyResponse->getContent()); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function it_returns_json_response_from_client_on_success() | ||
{ | ||
$content = json_encode(['version' => '9001']); | ||
|
||
$this->messageFactory->createRequest(Argument::cetera()) | ||
->willReturn($this->prophesize(RequestInterface::class)->reveal()) | ||
; | ||
|
||
$stream = $this->prophesize(StreamInterface::class); | ||
$stream->getContents()->willReturn($content); | ||
|
||
$externalResponse = $this->prophesize(ResponseInterface::class); | ||
$externalResponse->getBody()->willReturn($stream->reveal()); | ||
|
||
$this->client->send(Argument::cetera())->willReturn($externalResponse->reveal()); | ||
|
||
$response = $this->controller->getVersionAction(new Request()); | ||
|
||
$this->assertEquals(JsonResponse::HTTP_OK, $response->getStatusCode()); | ||
$this->assertEquals($content, $response->getContent()); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function setUp() | ||
{ | ||
$this->client = $this->prophesize(ClientInterface::class); | ||
$this->messageFactory = $this->prophesize(MessageFactory::class); | ||
|
||
$this->controller = new NotificationController( | ||
$this->client->reveal(), | ||
$this->messageFactory->reveal(), | ||
self::$hubUri | ||
); | ||
|
||
parent::setUp(); | ||
} | ||
} |
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
Oops, something went wrong.