Skip to content

Commit a607582

Browse files
dnnaandig
authored andcommitted
Add unit tests for Symfony (#129)
1 parent 6d1da1a commit a607582

8 files changed

+226
-1
lines changed

.travis.yml

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ jobs:
1919
# dependencies for phpstan
2020
- composer require --dev "symfony/symfony" "laravel/framework" "drupal/drupal"
2121
script:
22+
# Unit test
23+
- ./vendor/bin/phpunit
2224
# Static analyzer check
2325
- ./vendor/bin/phpstan analyze -c .phpstan.neon --level=4 --no-progress Bootstraps Bridges Laravel || true
2426
# Check the code style

composer.json

+5-1
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,13 @@
77
"symfony/http-kernel": "^2.6|^3.0|^4",
88
"ringcentral/psr7": "^1.2"
99
},
10+
"require-dev": {
11+
"phpunit/phpunit": "^5.7"
12+
},
1013
"autoload": {
1114
"psr-4": {
12-
"PHPPM\\": ""
15+
"PHPPM\\": "",
16+
"PHPPM\\Tests\\": "tests"
1317
}
1418
}
1519
}

phpunit.xml.dist

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit backupGlobals="false"
3+
backupStaticAttributes="false"
4+
colors="true"
5+
convertErrorsToExceptions="true"
6+
convertNoticesToExceptions="true"
7+
convertWarningsToExceptions="true"
8+
processIsolation="false"
9+
stopOnFailure="false"
10+
bootstrap="tests/bootstrap.php"
11+
>
12+
13+
<testsuites>
14+
<testsuite name="PHP-PM-HttpKernel Test Suite">
15+
<directory>tests/</directory>
16+
</testsuite>
17+
</testsuites>
18+
19+
</phpunit>

tests/HttpKernelTest.php

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace PHPPM\Tests;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use PHPPM\Bridges\HttpKernel;
7+
use Psr\Http\Message\ServerRequestInterface;
8+
9+
class HttpKernelTest extends TestCase
10+
{
11+
/**
12+
* @runInSeparateProcess
13+
*/
14+
public function testNoBootstrap()
15+
{
16+
$bridge = new HttpKernel();
17+
$request = $this
18+
->getMockBuilder(ServerRequestInterface::class)
19+
->getMock();
20+
$response = $bridge->handle($request);
21+
$this->assertEquals(500, $response->getStatusCode());
22+
$this->assertEquals('Application not configured during bootstrap', (string)$response->getBody());
23+
}
24+
}

tests/SymfonyBootstrapTest.php

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
3+
namespace PHPPM\Tests;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use PHPPM\Bridges\HttpKernel;
7+
use Psr\Http\Message\ServerRequestInterface;
8+
use Psr\Http\Message\UploadedFileInterface;
9+
10+
class SymfonyBootstrapTest extends TestCase
11+
{
12+
13+
/**
14+
* @runInSeparateProcess
15+
*/
16+
public function testGetRequest()
17+
{
18+
putenv('APP_KERNEL_NAMESPACE=PHPPM\\Tests\\SymfonyMocks\\');
19+
$bridge = new HttpKernel();
20+
$bridge->bootstrap('Symfony', 'test', true);
21+
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');
27+
28+
$response = $bridge->handle($request);
29+
$this->assertEquals(200, $response->getStatusCode());
30+
$this->assertEquals('Success', (string)$response->getBody());
31+
}
32+
33+
/**
34+
* @runInSeparateProcess
35+
*/
36+
public function testFileUpload()
37+
{
38+
putenv('APP_KERNEL_NAMESPACE=PHPPM\\Tests\\SymfonyMocks\\');
39+
$bridge = new HttpKernel();
40+
$bridge->bootstrap('Symfony', 'test', true);
41+
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');
59+
60+
$response = $bridge->handle($request);
61+
$this->assertEquals(201, $response->getStatusCode());
62+
$this->assertEquals('Uploaded files: testOK.pdf,NULL', (string)$response->getBody());
63+
}
64+
65+
/**
66+
* @runInSeparateProcess
67+
*/
68+
public function testPostJSON()
69+
{
70+
putenv('APP_KERNEL_NAMESPACE=PHPPM\\Tests\\SymfonyMocks\\');
71+
$bridge = new HttpKernel();
72+
$bridge->bootstrap('Symfony', 'test', true);
73+
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');
81+
82+
$response = $bridge->handle($request);
83+
$this->assertEquals(201, $response->getStatusCode());
84+
$this->assertEquals('Received JSON: {"some_json_array":[{"map1":"value1"},{"map2":"value2"}]}', (string)$response->getBody());
85+
}
86+
}

tests/SymfonyMocks/Container.php

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace PHPPM\Tests\SymfonyMocks;
4+
5+
class Container
6+
{
7+
public function has($service)
8+
{
9+
return false;
10+
}
11+
}

tests/SymfonyMocks/Kernel.php

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
namespace PHPPM\Tests\SymfonyMocks;
4+
5+
use Symfony\Component\HttpFoundation\Request;
6+
use Symfony\Component\HttpFoundation\Response;
7+
8+
class Kernel
9+
{
10+
private $bundlesInitialized = false;
11+
private $containerInitialized = false;
12+
13+
public function __construct($env, $debug)
14+
{
15+
}
16+
17+
public function initializeBundles()
18+
{
19+
$this->bundlesInitialized = true;
20+
}
21+
22+
public function initializeContainer()
23+
{
24+
$this->containerInitialized = true;
25+
}
26+
27+
public function getBundles()
28+
{
29+
if (!$this->bundlesInitialized) {
30+
throw new \Exception('Bundles not initialized');
31+
}
32+
33+
return [];
34+
}
35+
36+
public function getContainer()
37+
{
38+
if (!$this->containerInitialized) {
39+
throw new \Exception('Container not initialized');
40+
}
41+
42+
return new Container();
43+
}
44+
45+
public function handle(Request $request)
46+
{
47+
if (!$this->bundlesInitialized) {
48+
throw new \Exception('Bundles not initialized');
49+
}
50+
if (!$this->containerInitialized) {
51+
throw new \Exception('Container not initialized');
52+
}
53+
54+
if ($request->getMethod() == 'POST') {
55+
if (count($request->files->all()) > 0) {
56+
$mappedFileNames = array_map(function ($f) {
57+
if (!isset($f)) {
58+
return 'NULL';
59+
}
60+
return $f->getClientOriginalName();
61+
}, $request->files->all());
62+
return new Response('Uploaded files: '.implode(',', $mappedFileNames), 201);
63+
}
64+
if ($request->getContentType() == 'json') {
65+
$body = json_decode($request->getContent(), true);
66+
if ($request->getContent() == null || !$body) {
67+
throw new \Exception('Invalid JSON body');
68+
}
69+
return new Response('Received JSON: '.$request->getContent(), 201);
70+
}
71+
} elseif ($request->getMethod() == 'GET') {
72+
// Simple get request
73+
return new Response('Success', 200);
74+
}
75+
}
76+
}

tests/bootstrap.php

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
3+
require __DIR__ . ' /../vendor/autoload.php';

0 commit comments

Comments
 (0)