From 060d5795f4bb888877da8a657ee09f104d17b1b7 Mon Sep 17 00:00:00 2001 From: Albert Casdemont Date: Sat, 19 Jan 2019 20:10:25 +0100 Subject: [PATCH] improve tests, replace psr7 implementation --- Bridges/HttpKernel.php | 2 +- composer.json | 10 ++- tests/Fixtures/ProcessSlaveDouble.php | 13 +++ .../Symfony/Controller/GetController.php | 18 ++++ .../Symfony/Controller/PostJsonController.php | 24 +++++ .../Symfony/Controller/StreamedController.php | 20 +++++ .../Symfony/Controller/UploadController.php | 26 ++++++ tests/Fixtures/Symfony/Kernel.php | 50 +++++++++++ tests/Fixtures/Symfony/config/bundles.php | 5 ++ tests/Fixtures/Symfony/config/routes.yaml | 3 + tests/Fixtures/Symfony/config/services.yaml | 3 + tests/SymfonyBootstrapTest.php | 79 ++++++++--------- tests/SymfonyMocks/Container.php | 13 --- tests/SymfonyMocks/Kernel.php | 87 ------------------- 14 files changed, 205 insertions(+), 148 deletions(-) create mode 100644 tests/Fixtures/ProcessSlaveDouble.php create mode 100644 tests/Fixtures/Symfony/Controller/GetController.php create mode 100644 tests/Fixtures/Symfony/Controller/PostJsonController.php create mode 100644 tests/Fixtures/Symfony/Controller/StreamedController.php create mode 100644 tests/Fixtures/Symfony/Controller/UploadController.php create mode 100644 tests/Fixtures/Symfony/Kernel.php create mode 100644 tests/Fixtures/Symfony/config/bundles.php create mode 100644 tests/Fixtures/Symfony/config/routes.yaml create mode 100644 tests/Fixtures/Symfony/config/services.yaml delete mode 100644 tests/SymfonyMocks/Container.php delete mode 100644 tests/SymfonyMocks/Kernel.php diff --git a/Bridges/HttpKernel.php b/Bridges/HttpKernel.php index 104334e..e4f1c58 100644 --- a/Bridges/HttpKernel.php +++ b/Bridges/HttpKernel.php @@ -9,7 +9,7 @@ use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\UploadedFileInterface; -use RingCentral\Psr7; +use GuzzleHttp\Psr7; use Symfony\Component\HttpFoundation\Cookie; use Symfony\Component\HttpFoundation\File\UploadedFile as SymfonyFile; use Symfony\Component\HttpFoundation\Request as SymfonyRequest; diff --git a/composer.json b/composer.json index 896dd1c..c8ac298 100644 --- a/composer.json +++ b/composer.json @@ -3,12 +3,14 @@ "license": "MIT", "require": { "php-pm/php-pm": "^1.0.5", - "symfony/http-foundation": "^2.6|^3.0|^4", - "symfony/http-kernel": "^2.6|^3.0|^4", - "ringcentral/psr7": "^1.2" + "symfony/http-foundation": "^3.4|^4", + "symfony/http-kernel": "^3.4|^4", + "guzzlehttp/psr7": "^1.5" }, "require-dev": { - "phpunit/phpunit": "^5.7" + "phpunit/phpunit": "^5.7", + "symfony/framework-bundle": "^3.4|^4", + "doctrine/annotations": "^1.6" }, "autoload": { "psr-4": { diff --git a/tests/Fixtures/ProcessSlaveDouble.php b/tests/Fixtures/ProcessSlaveDouble.php new file mode 100644 index 0000000..e21f24d --- /dev/null +++ b/tests/Fixtures/ProcessSlaveDouble.php @@ -0,0 +1,13 @@ +watchedFiles[] = $file; + } +} diff --git a/tests/Fixtures/Symfony/Controller/GetController.php b/tests/Fixtures/Symfony/Controller/GetController.php new file mode 100644 index 0000000..22f1a9e --- /dev/null +++ b/tests/Fixtures/Symfony/Controller/GetController.php @@ -0,0 +1,18 @@ +getContent(), true); + if ($request->getContent() == null || !$body) { + throw new \Exception('Invalid JSON body'); + } + + return new Response('Received JSON: '.$request->getContent(), 201); + } +} diff --git a/tests/Fixtures/Symfony/Controller/StreamedController.php b/tests/Fixtures/Symfony/Controller/StreamedController.php new file mode 100644 index 0000000..0b2b83d --- /dev/null +++ b/tests/Fixtures/Symfony/Controller/StreamedController.php @@ -0,0 +1,20 @@ +getClientOriginalName(); + }, $request->files->all()); + + return new Response('Uploaded files: '.implode(',', $mappedFileNames), 201); + } +} diff --git a/tests/Fixtures/Symfony/Kernel.php b/tests/Fixtures/Symfony/Kernel.php new file mode 100644 index 0000000..f7c7738 --- /dev/null +++ b/tests/Fixtures/Symfony/Kernel.php @@ -0,0 +1,50 @@ +getProjectDir() . '/config/bundles.php'; + foreach ($contents as $class => $envs) { + if (isset($envs['all']) || isset($envs[$this->environment])) { + yield new $class(); + } + } + } + + protected function configureContainer(ContainerBuilder $container, LoaderInterface $loader) + { + $container->addResource(new FileResource($this->getProjectDir() . '/config/bundles.php')); + // Feel free to remove the "container.autowiring.strict_mode" parameter + // if you are using symfony/dependency-injection 4.0+ as it's the default behavior + $container->setParameter('container.autowiring.strict_mode', true); + $container->setParameter('container.dumper.inline_class_loader', true); + $confDir = $this->getProjectDir() . '/config'; + + $loader->load($confDir . '/{services}' . self::CONFIG_EXTS, 'glob'); + } + + protected function configureRoutes(RouteCollectionBuilder $routes) + { + $confDir = $this->getProjectDir() . '/config'; + + $routes->import($confDir . '/{routes}' . self::CONFIG_EXTS, '/', 'glob'); + } + + public function getProjectDir() + { + return __DIR__; + } +} diff --git a/tests/Fixtures/Symfony/config/bundles.php b/tests/Fixtures/Symfony/config/bundles.php new file mode 100644 index 0000000..49d3fb6 --- /dev/null +++ b/tests/Fixtures/Symfony/config/bundles.php @@ -0,0 +1,5 @@ + ['all' => true], +]; diff --git a/tests/Fixtures/Symfony/config/routes.yaml b/tests/Fixtures/Symfony/config/routes.yaml new file mode 100644 index 0000000..3550fe4 --- /dev/null +++ b/tests/Fixtures/Symfony/config/routes.yaml @@ -0,0 +1,3 @@ +controllers: + resource: '../Controller/' + type: annotation diff --git a/tests/Fixtures/Symfony/config/services.yaml b/tests/Fixtures/Symfony/config/services.yaml new file mode 100644 index 0000000..c56190f --- /dev/null +++ b/tests/Fixtures/Symfony/config/services.yaml @@ -0,0 +1,3 @@ +services: +framework: + secret: foobar diff --git a/tests/SymfonyBootstrapTest.php b/tests/SymfonyBootstrapTest.php index fd8e01e..611ff7d 100644 --- a/tests/SymfonyBootstrapTest.php +++ b/tests/SymfonyBootstrapTest.php @@ -2,32 +2,42 @@ namespace PHPPM\Tests; +use PHPPM\ProcessSlave; +use PHPPM\Tests\Fixtures\ProcessSlaveDouble; use PHPUnit\Framework\TestCase; use PHPPM\Bridges\HttpKernel; -use Psr\Http\Message\ServerRequestInterface; -use Psr\Http\Message\UploadedFileInterface; +use GuzzleHttp\Psr7\ServerRequest; +use GuzzleHttp\Psr7\UploadedFile; +use Symfony\Component\Filesystem\Filesystem; class SymfonyBootstrapTest extends TestCase { + public function setUp() + { + ProcessSlave::$slave = new ProcessSlaveDouble(); + } + + public static function tearDownAfterClass() + { + $fs = new Filesystem(); + $fs->remove(__DIR__.'/Fixtures/Symfony/var'); + } /** * @runInSeparateProcess */ public function testGetRequest() { - putenv('APP_KERNEL_NAMESPACE=PHPPM\\Tests\\SymfonyMocks\\'); + putenv('APP_KERNEL_NAMESPACE=PHPPM\\Tests\\Fixtures\\Symfony\\'); $bridge = new HttpKernel(); $bridge->bootstrap('Symfony', 'test', true); - $request = $this->getMockBuilder(ServerRequestInterface::class)->getMock(); - $request->method('getHeader')->with('Cookie')->willReturn([]); - $request->method('getQueryParams')->willReturn([]); - $request->method('getUploadedFiles')->willReturn([]); - $request->method('getMethod')->willReturn('GET'); + $request = new ServerRequest('GET', '/get'); + $_SERVER['REQUEST_URI'] = (string) $request->getUri(); $response = $bridge->handle($request); $this->assertEquals(200, $response->getStatusCode()); - $this->assertEquals('Success', (string)$response->getBody()); + $this->assertEquals('Success', (string) $response->getBody()); } /** @@ -35,27 +45,18 @@ public function testGetRequest() */ public function testFileUpload() { - putenv('APP_KERNEL_NAMESPACE=PHPPM\\Tests\\SymfonyMocks\\'); + putenv('APP_KERNEL_NAMESPACE=PHPPM\\Tests\\Fixtures\\Symfony\\'); $bridge = new HttpKernel(); $bridge->bootstrap('Symfony', 'test', true); - $fileOK = $this->getMockBuilder(UploadedFileInterface::class)->getMock(); - $fileOK->method('getClientFilename')->willReturn('testOK.pdf'); - $fileOK->method('getClientMediaType')->willReturn('pdf'); - $fileOK->method('getSize')->willReturn(1000); - $fileOK->method('getError')->willReturn(UPLOAD_ERR_OK); - - $fileErr = $this->getMockBuilder(UploadedFileInterface::class)->getMock(); - $fileErr->method('getClientFilename')->willReturn('testErr.pdf'); - $fileErr->method('getClientMediaType')->willReturn('pdf'); - $fileErr->method('getSize')->willReturn(0); - $fileErr->method('getError')->willReturn(UPLOAD_ERR_NO_FILE); - - $request = $this->getMockBuilder(ServerRequestInterface::class)->getMock(); - $request->method('getHeader')->with('Cookie')->willReturn([]); - $request->method('getQueryParams')->willReturn([]); - $request->method('getUploadedFiles')->willReturn([$fileOK, $fileErr]); - $request->method('getMethod')->willReturn('POST'); + $request = new ServerRequest('POST', '/upload'); + $dummyStream = fopen('data:text/plain,dummy', 'r'); + $uploadedFiles = [ + new UploadedFile($dummyStream, 1000, UPLOAD_ERR_OK, 'testOK.pdf', 'pdf'), + new UploadedFile($dummyStream, 0, UPLOAD_ERR_NO_FILE, 'testErr.pdf', 'pdf'), + ]; + $request = $request->withUploadedFiles($uploadedFiles); + $_SERVER['REQUEST_URI'] = (string) $request->getUri(); $response = $bridge->handle($request); $this->assertEquals(201, $response->getStatusCode()); @@ -67,17 +68,14 @@ public function testFileUpload() */ public function testPostJSON() { - putenv('APP_KERNEL_NAMESPACE=PHPPM\\Tests\\SymfonyMocks\\'); + putenv('APP_KERNEL_NAMESPACE=PHPPM\\Tests\\Fixtures\\Symfony\\'); $bridge = new HttpKernel(); $bridge->bootstrap('Symfony', 'test', true); - $_SERVER["CONTENT_TYPE"] = 'application/json'; - $request = $this->getMockBuilder(ServerRequestInterface::class)->getMock(); - $request->method('getHeader')->with('Cookie')->willReturn([]); - $request->method('getQueryParams')->willReturn([]); - $request->method('getUploadedFiles')->willReturn([]); - $request->method('getBody')->willReturn('{"some_json_array":[{"map1":"value1"},{"map2":"value2"}]}'); - $request->method('getMethod')->willReturn('POST'); + $request = new ServerRequest('POST', '/json', [ + 'CONTENT_TYPE' => 'application/json', + ], '{"some_json_array":[{"map1":"value1"},{"map2":"value2"}]}'); + $_SERVER['REQUEST_URI'] = (string) $request->getUri(); $response = $bridge->handle($request); $this->assertEquals(201, $response->getStatusCode()); @@ -89,17 +87,12 @@ public function testPostJSON() */ public function testStreamedResponse() { - putenv('APP_KERNEL_NAMESPACE=PHPPM\\Tests\\SymfonyMocks\\'); + putenv('APP_KERNEL_NAMESPACE=PHPPM\\Tests\\Fixtures\\Symfony\\'); $bridge = new HttpKernel(); $bridge->bootstrap('Symfony', 'test', true); - $request = $this->getMockBuilder(ServerRequestInterface::class)->getMock(); - $request->method('getHeader')->with('Cookie')->willReturn([]); - $request->method('getQueryParams')->willReturn([]); - $request->method('getUploadedFiles')->willReturn([]); - $request->method('getMethod')->willReturn('GET'); - - $_SERVER['REQUEST_URI'] = '/streamed'; + $request = new ServerRequest('GET', '/streamed'); + $_SERVER['REQUEST_URI'] = (string) $request->getUri(); $response = $bridge->handle($request); $this->assertEquals(200, $response->getStatusCode()); diff --git a/tests/SymfonyMocks/Container.php b/tests/SymfonyMocks/Container.php deleted file mode 100644 index 4467305..0000000 --- a/tests/SymfonyMocks/Container.php +++ /dev/null @@ -1,13 +0,0 @@ -bundlesInitialized = true; - $this->bundles = []; - } - - public function initializeContainer() - { - $this->containerInitialized = true; - $this->container = new Container(); - } - - public function getBundles() - { - if (!$this->bundlesInitialized) { - throw new \Exception('Bundles not initialized'); - } - - return $this->bundles; - } - - public function getContainer() - { - if (!$this->containerInitialized) { - throw new \Exception('Container not initialized'); - } - - return $this->container; - } - - public function handle(Request $request) - { - if (!$this->bundlesInitialized) { - throw new \Exception('Bundles not initialized'); - } - if (!$this->containerInitialized) { - throw new \Exception('Container not initialized'); - } - - if ($request->getMethod() == 'POST') { - if (count($request->files->all()) > 0) { - $mappedFileNames = array_map(function ($f) { - if (!isset($f)) { - return 'NULL'; - } - return $f->getClientOriginalName(); - }, $request->files->all()); - return new Response('Uploaded files: '.implode(',', $mappedFileNames), 201); - } - if ($request->getContentType() == 'json') { - $body = json_decode($request->getContent(), true); - if ($request->getContent() == null || !$body) { - throw new \Exception('Invalid JSON body'); - } - return new Response('Received JSON: '.$request->getContent(), 201); - } - } elseif ($request->getMethod() == 'GET') { - if ($request->getPathInfo() == '/streamed') { - return new StreamedResponse(function () { - echo 'streamed data'; - }, 200); - } - - // Simple get request - return new Response('Success', 200); - } - } -}