Skip to content

Commit 6210935

Browse files
acasademontandig
authored andcommitted
improve tests, replace psr7 implementation (#152)
1 parent 53ea496 commit 6210935

14 files changed

+205
-148
lines changed

Bridges/HttpKernel.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
use Psr\Http\Message\ServerRequestInterface;
1010
use Psr\Http\Message\ResponseInterface;
1111
use Psr\Http\Message\UploadedFileInterface;
12-
use RingCentral\Psr7;
12+
use GuzzleHttp\Psr7;
1313
use Symfony\Component\HttpFoundation\Cookie;
1414
use Symfony\Component\HttpFoundation\File\UploadedFile as SymfonyFile;
1515
use Symfony\Component\HttpFoundation\Request as SymfonyRequest;

composer.json

+6-4
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33
"license": "MIT",
44
"require": {
55
"php-pm/php-pm": "^1.0.5",
6-
"symfony/http-foundation": "^2.6|^3.0|^4",
7-
"symfony/http-kernel": "^2.6|^3.0|^4",
8-
"ringcentral/psr7": "^1.2"
6+
"symfony/http-foundation": "^3.4|^4",
7+
"symfony/http-kernel": "^3.4|^4",
8+
"guzzlehttp/psr7": "^1.5"
99
},
1010
"require-dev": {
11-
"phpunit/phpunit": "^5.7"
11+
"phpunit/phpunit": "^5.7",
12+
"symfony/framework-bundle": "^3.4|^4",
13+
"doctrine/annotations": "^1.6"
1214
},
1315
"autoload": {
1416
"psr-4": {

tests/Fixtures/ProcessSlaveDouble.php

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace PHPPM\Tests\Fixtures;
4+
5+
class ProcessSlaveDouble
6+
{
7+
private $watchedFiles = [];
8+
9+
public function registerFile($file)
10+
{
11+
$this->watchedFiles[] = $file;
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace PHPPM\Tests\Fixtures\Symfony\Controller;
4+
5+
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
6+
use Symfony\Component\HttpFoundation\Response;
7+
use Symfony\Component\Routing\Annotation\Route;
8+
9+
class GetController extends Controller
10+
{
11+
/**
12+
* @Route("/get")
13+
*/
14+
public function __invoke()
15+
{
16+
return new Response('Success', 200);
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace PHPPM\Tests\Fixtures\Symfony\Controller;
4+
5+
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
6+
use Symfony\Component\HttpFoundation\Request;
7+
use Symfony\Component\HttpFoundation\Response;
8+
use Symfony\Component\Routing\Annotation\Route;
9+
10+
class PostJsonController extends Controller
11+
{
12+
/**
13+
* @Route("/json", methods={"POST"})
14+
*/
15+
public function __invoke(Request $request)
16+
{
17+
$body = json_decode($request->getContent(), true);
18+
if ($request->getContent() == null || !$body) {
19+
throw new \Exception('Invalid JSON body');
20+
}
21+
22+
return new Response('Received JSON: '.$request->getContent(), 201);
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace PHPPM\Tests\Fixtures\Symfony\Controller;
4+
5+
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
6+
use Symfony\Component\HttpFoundation\StreamedResponse;
7+
use Symfony\Component\Routing\Annotation\Route;
8+
9+
class StreamedController extends Controller
10+
{
11+
/**
12+
* @Route("/streamed")
13+
*/
14+
public function __invoke()
15+
{
16+
return new StreamedResponse(function () {
17+
echo 'streamed data';
18+
}, 200);
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace PHPPM\Tests\Fixtures\Symfony\Controller;
4+
5+
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
6+
use Symfony\Component\HttpFoundation\Request;
7+
use Symfony\Component\HttpFoundation\Response;
8+
use Symfony\Component\Routing\Annotation\Route;
9+
10+
class UploadController extends Controller
11+
{
12+
/**
13+
* @Route("/upload", methods={"POST"})
14+
*/
15+
public function __invoke(Request $request)
16+
{
17+
$mappedFileNames = array_map(function ($f) {
18+
if (!isset($f)) {
19+
return 'NULL';
20+
}
21+
return $f->getClientOriginalName();
22+
}, $request->files->all());
23+
24+
return new Response('Uploaded files: '.implode(',', $mappedFileNames), 201);
25+
}
26+
}

tests/Fixtures/Symfony/Kernel.php

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
namespace PHPPM\Tests\Fixtures\Symfony;
4+
5+
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
6+
use Symfony\Component\Config\Loader\LoaderInterface;
7+
use Symfony\Component\Config\Resource\FileResource;
8+
use Symfony\Component\DependencyInjection\ContainerBuilder;
9+
use Symfony\Component\Routing\RouteCollectionBuilder;
10+
11+
class Kernel extends \Symfony\Component\HttpKernel\Kernel
12+
{
13+
use MicroKernelTrait;
14+
15+
const CONFIG_EXTS = '.{php,xml,yaml,yml}';
16+
17+
public function registerBundles()
18+
{
19+
$contents = require $this->getProjectDir() . '/config/bundles.php';
20+
foreach ($contents as $class => $envs) {
21+
if (isset($envs['all']) || isset($envs[$this->environment])) {
22+
yield new $class();
23+
}
24+
}
25+
}
26+
27+
protected function configureContainer(ContainerBuilder $container, LoaderInterface $loader)
28+
{
29+
$container->addResource(new FileResource($this->getProjectDir() . '/config/bundles.php'));
30+
// Feel free to remove the "container.autowiring.strict_mode" parameter
31+
// if you are using symfony/dependency-injection 4.0+ as it's the default behavior
32+
$container->setParameter('container.autowiring.strict_mode', true);
33+
$container->setParameter('container.dumper.inline_class_loader', true);
34+
$confDir = $this->getProjectDir() . '/config';
35+
36+
$loader->load($confDir . '/{services}' . self::CONFIG_EXTS, 'glob');
37+
}
38+
39+
protected function configureRoutes(RouteCollectionBuilder $routes)
40+
{
41+
$confDir = $this->getProjectDir() . '/config';
42+
43+
$routes->import($confDir . '/{routes}' . self::CONFIG_EXTS, '/', 'glob');
44+
}
45+
46+
public function getProjectDir()
47+
{
48+
return __DIR__;
49+
}
50+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
return [
4+
Symfony\Bundle\FrameworkBundle\FrameworkBundle::class => ['all' => true],
5+
];
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
controllers:
2+
resource: '../Controller/'
3+
type: annotation
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
services:
2+
framework:
3+
secret: foobar

tests/SymfonyBootstrapTest.php

+36-43
Original file line numberDiff line numberDiff line change
@@ -2,60 +2,61 @@
22

33
namespace PHPPM\Tests;
44

5+
use PHPPM\ProcessSlave;
6+
use PHPPM\Tests\Fixtures\ProcessSlaveDouble;
57
use PHPUnit\Framework\TestCase;
68
use PHPPM\Bridges\HttpKernel;
7-
use Psr\Http\Message\ServerRequestInterface;
8-
use Psr\Http\Message\UploadedFileInterface;
9+
use GuzzleHttp\Psr7\ServerRequest;
10+
use GuzzleHttp\Psr7\UploadedFile;
11+
use Symfony\Component\Filesystem\Filesystem;
912

1013
class SymfonyBootstrapTest extends TestCase
1114
{
15+
public function setUp()
16+
{
17+
ProcessSlave::$slave = new ProcessSlaveDouble();
18+
}
19+
20+
public static function tearDownAfterClass()
21+
{
22+
$fs = new Filesystem();
23+
$fs->remove(__DIR__.'/Fixtures/Symfony/var');
24+
}
1225

1326
/**
1427
* @runInSeparateProcess
1528
*/
1629
public function testGetRequest()
1730
{
18-
putenv('APP_KERNEL_NAMESPACE=PHPPM\\Tests\\SymfonyMocks\\');
31+
putenv('APP_KERNEL_NAMESPACE=PHPPM\\Tests\\Fixtures\\Symfony\\');
1932
$bridge = new HttpKernel();
2033
$bridge->bootstrap('Symfony', 'test', true);
2134

22-
$request = $this->getMockBuilder(ServerRequestInterface::class)->getMock();
23-
$request->method('getHeader')->with('Cookie')->willReturn([]);
24-
$request->method('getQueryParams')->willReturn([]);
25-
$request->method('getUploadedFiles')->willReturn([]);
26-
$request->method('getMethod')->willReturn('GET');
35+
$request = new ServerRequest('GET', '/get');
36+
$_SERVER['REQUEST_URI'] = (string) $request->getUri();
2737

2838
$response = $bridge->handle($request);
2939
$this->assertEquals(200, $response->getStatusCode());
30-
$this->assertEquals('Success', (string)$response->getBody());
40+
$this->assertEquals('Success', (string) $response->getBody());
3141
}
3242

3343
/**
3444
* @runInSeparateProcess
3545
*/
3646
public function testFileUpload()
3747
{
38-
putenv('APP_KERNEL_NAMESPACE=PHPPM\\Tests\\SymfonyMocks\\');
48+
putenv('APP_KERNEL_NAMESPACE=PHPPM\\Tests\\Fixtures\\Symfony\\');
3949
$bridge = new HttpKernel();
4050
$bridge->bootstrap('Symfony', 'test', true);
4151

42-
$fileOK = $this->getMockBuilder(UploadedFileInterface::class)->getMock();
43-
$fileOK->method('getClientFilename')->willReturn('testOK.pdf');
44-
$fileOK->method('getClientMediaType')->willReturn('pdf');
45-
$fileOK->method('getSize')->willReturn(1000);
46-
$fileOK->method('getError')->willReturn(UPLOAD_ERR_OK);
47-
48-
$fileErr = $this->getMockBuilder(UploadedFileInterface::class)->getMock();
49-
$fileErr->method('getClientFilename')->willReturn('testErr.pdf');
50-
$fileErr->method('getClientMediaType')->willReturn('pdf');
51-
$fileErr->method('getSize')->willReturn(0);
52-
$fileErr->method('getError')->willReturn(UPLOAD_ERR_NO_FILE);
53-
54-
$request = $this->getMockBuilder(ServerRequestInterface::class)->getMock();
55-
$request->method('getHeader')->with('Cookie')->willReturn([]);
56-
$request->method('getQueryParams')->willReturn([]);
57-
$request->method('getUploadedFiles')->willReturn([$fileOK, $fileErr]);
58-
$request->method('getMethod')->willReturn('POST');
52+
$request = new ServerRequest('POST', '/upload');
53+
$dummyStream = fopen('data:text/plain,dummy', 'r');
54+
$uploadedFiles = [
55+
new UploadedFile($dummyStream, 1000, UPLOAD_ERR_OK, 'testOK.pdf', 'pdf'),
56+
new UploadedFile($dummyStream, 0, UPLOAD_ERR_NO_FILE, 'testErr.pdf', 'pdf'),
57+
];
58+
$request = $request->withUploadedFiles($uploadedFiles);
59+
$_SERVER['REQUEST_URI'] = (string) $request->getUri();
5960

6061
$response = $bridge->handle($request);
6162
$this->assertEquals(201, $response->getStatusCode());
@@ -67,17 +68,14 @@ public function testFileUpload()
6768
*/
6869
public function testPostJSON()
6970
{
70-
putenv('APP_KERNEL_NAMESPACE=PHPPM\\Tests\\SymfonyMocks\\');
71+
putenv('APP_KERNEL_NAMESPACE=PHPPM\\Tests\\Fixtures\\Symfony\\');
7172
$bridge = new HttpKernel();
7273
$bridge->bootstrap('Symfony', 'test', true);
7374

74-
$_SERVER["CONTENT_TYPE"] = 'application/json';
75-
$request = $this->getMockBuilder(ServerRequestInterface::class)->getMock();
76-
$request->method('getHeader')->with('Cookie')->willReturn([]);
77-
$request->method('getQueryParams')->willReturn([]);
78-
$request->method('getUploadedFiles')->willReturn([]);
79-
$request->method('getBody')->willReturn('{"some_json_array":[{"map1":"value1"},{"map2":"value2"}]}');
80-
$request->method('getMethod')->willReturn('POST');
75+
$request = new ServerRequest('POST', '/json', [
76+
'CONTENT_TYPE' => 'application/json',
77+
], '{"some_json_array":[{"map1":"value1"},{"map2":"value2"}]}');
78+
$_SERVER['REQUEST_URI'] = (string) $request->getUri();
8179

8280
$response = $bridge->handle($request);
8381
$this->assertEquals(201, $response->getStatusCode());
@@ -89,17 +87,12 @@ public function testPostJSON()
8987
*/
9088
public function testStreamedResponse()
9189
{
92-
putenv('APP_KERNEL_NAMESPACE=PHPPM\\Tests\\SymfonyMocks\\');
90+
putenv('APP_KERNEL_NAMESPACE=PHPPM\\Tests\\Fixtures\\Symfony\\');
9391
$bridge = new HttpKernel();
9492
$bridge->bootstrap('Symfony', 'test', true);
9593

96-
$request = $this->getMockBuilder(ServerRequestInterface::class)->getMock();
97-
$request->method('getHeader')->with('Cookie')->willReturn([]);
98-
$request->method('getQueryParams')->willReturn([]);
99-
$request->method('getUploadedFiles')->willReturn([]);
100-
$request->method('getMethod')->willReturn('GET');
101-
102-
$_SERVER['REQUEST_URI'] = '/streamed';
94+
$request = new ServerRequest('GET', '/streamed');
95+
$_SERVER['REQUEST_URI'] = (string) $request->getUri();
10396

10497
$response = $bridge->handle($request);
10598
$this->assertEquals(200, $response->getStatusCode());

tests/SymfonyMocks/Container.php

-13
This file was deleted.

0 commit comments

Comments
 (0)