Skip to content

Commit b8f83a3

Browse files
committed
improve tests, replace psr7 implementation
1 parent 53ea496 commit b8f83a3

14 files changed

+191
-149
lines changed

Bootstraps/Symfony.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace PHPPM\Bootstraps;
44

5+
use PHPPM\ProcessSlave;
56
use PHPPM\Symfony\StrongerNativeSessionStorage;
67
use PHPPM\Utils;
78
use Symfony\Component\HttpFoundation\Request;
@@ -88,7 +89,7 @@ public function getApplication()
8889
$app->booted = true;
8990
}, $app);
9091

91-
if ($this->debug) {
92+
if ($this->debug && ProcessSlave::$slave) {
9293
Utils::bindAndCall(function () use ($app) {
9394
$container = $app->container;
9495

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": {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace PHPPM\Tests\Fixtures\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,23 @@
1+
<?php
2+
3+
namespace PHPPM\Tests\Fixtures\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+
return new Response('Received JSON: '.$request->getContent(), 201);
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace PHPPM\Tests\Fixtures\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\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/Kernel.php

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
namespace PHPPM\Tests\Fixtures;
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 . '/{packages}/*' . self::CONFIG_EXTS, 'glob');
37+
$loader->load($confDir . '/{packages}/' . $this->environment . '/**/*' . self::CONFIG_EXTS, 'glob');
38+
$loader->load($confDir . '/{services}' . self::CONFIG_EXTS, 'glob');
39+
$loader->load($confDir . '/{services}_' . $this->environment . self::CONFIG_EXTS, 'glob');
40+
}
41+
42+
protected function configureRoutes(RouteCollectionBuilder $routes)
43+
{
44+
$confDir = $this->getProjectDir() . '/config';
45+
46+
$routes->import($confDir . '/{routes}/*' . self::CONFIG_EXTS, '/', 'glob');
47+
$routes->import($confDir . '/{routes}/' . $this->environment . '/**/*' . self::CONFIG_EXTS, '/', 'glob');
48+
$routes->import($confDir . '/{routes}' . self::CONFIG_EXTS, '/', 'glob');
49+
}
50+
51+
public function getProjectDir()
52+
{
53+
return __DIR__;
54+
}
55+
}

tests/Fixtures/config/bundles.php

+5
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+
];

tests/Fixtures/config/routes.yaml

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
controllers:
2+
resource: '../Controller/'
3+
type: annotation

tests/Fixtures/config/services.yaml

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
services:
2+
framework:
3+
secret: foobar

tests/SymfonyBootstrapTest.php

+29-43
Original file line numberDiff line numberDiff line change
@@ -4,58 +4,52 @@
44

55
use PHPUnit\Framework\TestCase;
66
use PHPPM\Bridges\HttpKernel;
7-
use Psr\Http\Message\ServerRequestInterface;
8-
use Psr\Http\Message\UploadedFileInterface;
7+
use GuzzleHttp\Psr7\ServerRequest;
8+
use GuzzleHttp\Psr7\UploadedFile;
9+
use Symfony\Component\Filesystem\Filesystem;
910

1011
class SymfonyBootstrapTest extends TestCase
1112
{
13+
public static function tearDownAfterClass()
14+
{
15+
$fs = new Filesystem();
16+
$fs->remove(__DIR__.'/Fixtures/var');
17+
}
1218

1319
/**
1420
* @runInSeparateProcess
1521
*/
1622
public function testGetRequest()
1723
{
18-
putenv('APP_KERNEL_NAMESPACE=PHPPM\\Tests\\SymfonyMocks\\');
24+
putenv('APP_KERNEL_NAMESPACE=PHPPM\\Tests\\Fixtures\\');
1925
$bridge = new HttpKernel();
2026
$bridge->bootstrap('Symfony', 'test', true);
2127

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');
28+
$request = new ServerRequest('GET', '/get');
29+
$_SERVER['REQUEST_URI'] = (string) $request->getUri();
2730

2831
$response = $bridge->handle($request);
2932
$this->assertEquals(200, $response->getStatusCode());
30-
$this->assertEquals('Success', (string)$response->getBody());
33+
$this->assertEquals('Success', (string) $response->getBody());
3134
}
3235

3336
/**
3437
* @runInSeparateProcess
3538
*/
3639
public function testFileUpload()
3740
{
38-
putenv('APP_KERNEL_NAMESPACE=PHPPM\\Tests\\SymfonyMocks\\');
41+
putenv('APP_KERNEL_NAMESPACE=PHPPM\\Tests\\Fixtures\\');
3942
$bridge = new HttpKernel();
4043
$bridge->bootstrap('Symfony', 'test', true);
4144

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');
45+
$request = new ServerRequest('POST', '/upload');
46+
$dummyStream = fopen('data:text/plain,dummy', 'r');
47+
$uploadedFiles = [
48+
new UploadedFile($dummyStream, 1000, UPLOAD_ERR_OK, 'testOK.pdf', 'pdf'),
49+
new UploadedFile($dummyStream, 0, UPLOAD_ERR_NO_FILE, 'testErr.pdf', 'pdf'),
50+
];
51+
$request = $request->withUploadedFiles($uploadedFiles);
52+
$_SERVER['REQUEST_URI'] = (string) $request->getUri();
5953

6054
$response = $bridge->handle($request);
6155
$this->assertEquals(201, $response->getStatusCode());
@@ -67,17 +61,14 @@ public function testFileUpload()
6761
*/
6862
public function testPostJSON()
6963
{
70-
putenv('APP_KERNEL_NAMESPACE=PHPPM\\Tests\\SymfonyMocks\\');
64+
putenv('APP_KERNEL_NAMESPACE=PHPPM\\Tests\\Fixtures\\');
7165
$bridge = new HttpKernel();
7266
$bridge->bootstrap('Symfony', 'test', true);
7367

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');
68+
$request = new ServerRequest('POST', '/json', [
69+
'CONTENT_TYPE' => 'application/json',
70+
], '{"some_json_array":[{"map1":"value1"},{"map2":"value2"}]}');
71+
$_SERVER['REQUEST_URI'] = (string) $request->getUri();
8172

8273
$response = $bridge->handle($request);
8374
$this->assertEquals(201, $response->getStatusCode());
@@ -89,17 +80,12 @@ public function testPostJSON()
8980
*/
9081
public function testStreamedResponse()
9182
{
92-
putenv('APP_KERNEL_NAMESPACE=PHPPM\\Tests\\SymfonyMocks\\');
83+
putenv('APP_KERNEL_NAMESPACE=PHPPM\\Tests\\Fixtures\\');
9384
$bridge = new HttpKernel();
9485
$bridge->bootstrap('Symfony', 'test', true);
9586

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';
87+
$request = new ServerRequest('GET', '/streamed');
88+
$_SERVER['REQUEST_URI'] = (string) $request->getUri();
10389

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

tests/SymfonyMocks/Container.php

-13
This file was deleted.

0 commit comments

Comments
 (0)