Skip to content
This repository has been archived by the owner on Jan 17, 2022. It is now read-only.

Commit

Permalink
Test SwooleBundle HttpDriver
Browse files Browse the repository at this point in the history
  • Loading branch information
k911 committed May 29, 2018
1 parent d2bafe9 commit 846ca32
Show file tree
Hide file tree
Showing 3 changed files with 275 additions and 0 deletions.
92 changes: 92 additions & 0 deletions Bridge/Doctrine/ORM/EntityManagerHttpDriverTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

declare(strict_types=1);

namespace App\Tests\Bundle\SwooleBundle\Bridge\Doctrine\ORM;

use App\Bundle\SwooleBundle\Bridge\Doctrine\ORM\EntityManagerHttpDriver;
use App\Bundle\SwooleBundle\Driver\HttpDriverInterface;
use Doctrine\DBAL\Connection;
use Doctrine\ORM\EntityManagerInterface;
use PHPUnit\Framework\TestCase;
use Prophecy\Prophecy\ObjectProphecy;
use Swoole\Http\Request;
use Swoole\Http\Response;

class EntityManagerHttpDriverTest extends TestCase
{
/**
* @var EntityManagerHttpDriver
*/
private $httpDriver;

/**
* @var HttpDriverInterface|ObjectProphecy
*/
private $decoratedProphecy;

/**
* @var \Doctrine\ORM\EntityManagerInterface|ObjectProphecy
*/
private $entityManagerProphecy;

/**
* @var Connection|ObjectProphecy
*/
private $connectionProphecy;

protected function setUp(): void
{
$this->entityManagerProphecy = $this->prophesize(EntityManagerInterface::class);
$this->decoratedProphecy = $this->prophesize(HttpDriverInterface::class);
$this->connectionProphecy = $this->prophesize(Connection::class);

/** @var HttpDriverInterface $decoratedMock */
$decoratedMock = $this->decoratedProphecy->reveal();

/** @var EntityManagerInterface $emMock */
$emMock = $this->entityManagerProphecy->reveal();

$this->setUpEntityManagerConnection();
$this->httpDriver = new EntityManagerHttpDriver($decoratedMock, $emMock);
}

public function testBoot(): void
{
$this->decoratedProphecy->boot([])->shouldBeCalled();
$this->httpDriver->boot([]);
}

public function testHandleNoReconnect(): void
{
$this->connectionProphecy->ping()->willReturn(true)->shouldBeCalled();

$request = new Request();
$response = new Response();
$this->decoratedProphecy->handle($request, $response)->shouldBeCalled();

$this->entityManagerProphecy->clear()->shouldBeCalled();

$this->httpDriver->handle($request, $response);
}

public function testHandleWithReconnect(): void
{
$this->connectionProphecy->ping()->willReturn(false)->shouldBeCalled();
$this->connectionProphecy->close()->shouldBeCalled();
$this->connectionProphecy->connect()->willReturn(true)->shouldBeCalled();

$request = new Request();
$response = new Response();
$this->decoratedProphecy->handle($request, $response)->shouldBeCalled();

$this->entityManagerProphecy->clear()->shouldBeCalled();

$this->httpDriver->handle($request, $response);
}

private function setUpEntityManagerConnection(): void
{
$this->entityManagerProphecy->getConnection()->willReturn($this->connectionProphecy->reveal());
}
}
55 changes: 55 additions & 0 deletions Bridge/Symfony/HttpFoundation/CloudFrontRequestFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

declare(strict_types=1);

namespace App\Tests\Bundle\SwooleBundle\Bridge\Symfony\HttpFoundation;

use App\Bundle\SwooleBundle\Bridge\Symfony\HttpFoundation\CloudFrontRequestFactory;
use App\Bundle\SwooleBundle\Bridge\Symfony\HttpFoundation\RequestFactoryInterface;
use PHPUnit\Framework\TestCase;
use Prophecy\Prophecy\ObjectProphecy;
use Swoole\Http\Request as SwooleRequest;
use Symfony\Component\HttpFoundation\Request as HttpFoundationRequest;

class CloudFrontRequestFactoryTest extends TestCase
{
/**
* @var RequestFactoryInterface|ObjectProphecy
*/
private $decoratedProphecy;

/**
* @var CloudFrontRequestFactory
*/
private $requestFactory;

protected function setUp(): void
{
$this->decoratedProphecy = $this->prophesize(RequestFactoryInterface::class);

/** @var RequestFactoryInterface $decoratedMock */
$decoratedMock = $this->decoratedProphecy->reveal();
$this->requestFactory = new CloudFrontRequestFactory($decoratedMock);
}

public function testHandleNoCloudFrontHeader(): void
{
$swooleRequest = new SwooleRequest();
$httpFoundationRequest = new HttpFoundationRequest();

$this->decoratedProphecy->make($swooleRequest)->willReturn($httpFoundationRequest)->shouldBeCalled();

$this->assertSame($httpFoundationRequest, $this->requestFactory->make($swooleRequest));
}

public function testHandleCloudFrontHeader(): void
{
$swooleRequest = new SwooleRequest();
$httpFoundationRequest = new HttpFoundationRequest([], [], [], [], [], ['HTTP_CLOUDFRONT_FORWARDED_PROTO' => 'https']);

$this->decoratedProphecy->make($swooleRequest)->willReturn($httpFoundationRequest)->shouldBeCalled();

$this->assertSame($httpFoundationRequest, $this->requestFactory->make($swooleRequest));
$this->assertSame('https', $httpFoundationRequest->headers->get('x_forwarded_proto'));
}
}
128 changes: 128 additions & 0 deletions Bridge/Symfony/HttpKernel/HttpKernelHttpDriverTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
<?php

declare(strict_types=1);

namespace App\Tests\Bundle\SwooleBundle\Bridge\Symfony\HttpKernel;

use App\Bundle\SwooleBundle\Bridge\Symfony\HttpFoundation\RequestFactoryInterface;
use App\Bundle\SwooleBundle\Bridge\Symfony\HttpFoundation\ResponseProcessorInterface;
use App\Bundle\SwooleBundle\Bridge\Symfony\HttpKernel\HttpKernelHttpDriver;
use PHPUnit\Framework\TestCase;
use Prophecy\Prophecy\ObjectProphecy;
use Swoole\Http\Request as SwooleRequest;
use Swoole\Http\Response as SwooleResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Request as HttpFoundationRequest;
use Symfony\Component\HttpFoundation\Response as HttpFoundationResponse;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\HttpKernel\TerminableInterface;

class HttpKernelHttpDriverTest extends TestCase
{
/**
* @var HttpKernelHttpDriver
*/
private $httpDriver;

/**
* @var ResponseProcessorInterface|ObjectProphecy
*/
private $responseProcessor;

/**
* @var RequestFactoryInterface|ObjectProphecy
*/
private $requestFactoryProphecy;

/**
* @var KernelInterface|TerminableInterface|ObjectProphecy
*/
private $kernelProphecy;

protected function setUp(): void
{
$this->kernelProphecy = $this->prophesize(KernelInterface::class);
$this->requestFactoryProphecy = $this->prophesize(RequestFactoryInterface::class);
$this->responseProcessor = $this->prophesize(ResponseProcessorInterface::class);

/** @var KernelInterface $kernelMock */
$kernelMock = $this->kernelProphecy->reveal();
/** @var RequestFactoryInterface $requestFactoryMock */
$requestFactoryMock = $this->requestFactoryProphecy->reveal();
/** @var ResponseProcessorInterface $responseProcessorMock */
$responseProcessorMock = $this->responseProcessor->reveal();

$this->httpDriver = new HttpKernelHttpDriver($kernelMock, $requestFactoryMock, $responseProcessorMock);
}

public function testBoot(): void
{
$configuration = [
'trustedHosts' => ['127.0.0.1', 'localhost'],
'trustedProxies' => ['192.168.1.0/24', '73.41.22.1', 'varnish'],
'trustedHeaderSet' => Request::HEADER_X_FORWARDED_AWS_ELB,
];

$this->kernelProphecy->boot()->shouldBeCalled();

$this->httpDriver->boot($configuration);

$this->assertSame(['{127.0.0.1}i', '{localhost}i'], Request::getTrustedHosts());

$this->assertSame($configuration['trustedProxies'], Request::getTrustedProxies());
$this->assertSame($configuration['trustedHeaderSet'], Request::getTrustedHeaderSet());
}

/**
* @throws \Exception
*/
public function testHandleNonTerminable(): void
{
$swooleRequest = new SwooleRequest();
$swooleResponse = new SwooleResponse();

$httpFoundationResponse = new HttpFoundationResponse();
$httpFoundationRequest = new HttpFoundationRequest();

$this->requestFactoryProphecy->make($swooleRequest)->willReturn($httpFoundationRequest)->shouldBeCalled();
$this->kernelProphecy->handle($httpFoundationRequest)->willReturn($httpFoundationResponse)->shouldBeCalled();
$this->responseProcessor->process($httpFoundationResponse, $swooleResponse)->shouldBeCalled();

$this->httpDriver->handle($swooleRequest, $swooleResponse);
}

/**
* @throws \Exception
*/
public function testHandleTerminable(): void
{
$this->setUpTerminableKernel();

$swooleRequest = new SwooleRequest();
$swooleResponse = new SwooleResponse();

$httpFoundationResponse = new HttpFoundationResponse();
$httpFoundationRequest = new HttpFoundationRequest();

$this->requestFactoryProphecy->make($swooleRequest)->willReturn($httpFoundationRequest)->shouldBeCalled();
$this->kernelProphecy->handle($httpFoundationRequest)->willReturn($httpFoundationResponse)->shouldBeCalled();
$this->responseProcessor->process($httpFoundationResponse, $swooleResponse)->shouldBeCalled();
$this->kernelProphecy->terminate($httpFoundationRequest, $httpFoundationResponse)->shouldBeCalled();

$this->httpDriver->handle($swooleRequest, $swooleResponse);
}

private function setUpTerminableKernel(): void
{
$this->kernelProphecy = $this->prophesize(KernelInterface::class)->willImplement(TerminableInterface::class);

/** @var KernelInterface $kernelMock */
$kernelMock = $this->kernelProphecy->reveal();
/** @var RequestFactoryInterface $requestFactoryMock */
$requestFactoryMock = $this->requestFactoryProphecy->reveal();
/** @var ResponseProcessorInterface $responseProcessorMock */
$responseProcessorMock = $this->responseProcessor->reveal();

$this->httpDriver = new HttpKernelHttpDriver($kernelMock, $requestFactoryMock, $responseProcessorMock);
}
}

0 comments on commit 846ca32

Please sign in to comment.