Skip to content

Add test infrastructure and symfony basic test #129

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Aug 26, 2018
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ jobs:
# dependencies for phpstan
- composer require --dev "symfony/symfony" "laravel/framework" "drupal/drupal"
script:
# Unit test
- ./vendor/bin/phpunit
# Static analyzer check
- ./vendor/bin/phpstan analyze -c .phpstan.neon --level=4 --no-progress Bootstraps Bridges Laravel || true
# Check the code style
Expand Down
6 changes: 5 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@
"symfony/http-kernel": "^2.6|^3.0|^4",
"ringcentral/psr7": "^1.2"
},
"require-dev": {
"phpunit/phpunit": "^5.7"
},
"autoload": {
"psr-4": {
"PHPPM\\": ""
"PHPPM\\": "",
"PHPPM\\Tests\\": "tests"
}
}
}
19 changes: 19 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
bootstrap="tests/bootstrap.php"
>

<testsuites>
<testsuite name="PHP-PM-HttpKernel Test Suite">
<directory>tests/</directory>
</testsuite>
</testsuites>

</phpunit>
24 changes: 24 additions & 0 deletions tests/HttpKernelTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace PHPPM\Tests;

use PHPUnit\Framework\TestCase;
use PHPPM\Bridges\HttpKernel;
use Psr\Http\Message\ServerRequestInterface;

class HttpKernelTest extends TestCase
{
/**
* @runInSeparateProcess
*/
public function testNoBootstrap()
{
$bridge = new HttpKernel();
$request = $this
->getMockBuilder(ServerRequestInterface::class)
->getMock();
$response = $bridge->handle($request);
$this->assertEquals(500, $response->getStatusCode());
$this->assertEquals('Application not configured during bootstrap', (string)$response->getBody());
}
}
86 changes: 86 additions & 0 deletions tests/SymfonyBootstrapTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

namespace PHPPM\Tests;

use PHPUnit\Framework\TestCase;
use PHPPM\Bridges\HttpKernel;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\UploadedFileInterface;

class SymfonyBootstrapTest extends TestCase
{

/**
* @runInSeparateProcess
*/
public function testGetRequest()
{
putenv('APP_KERNEL_NAMESPACE=PHPPM\\Tests\\SymfonyMocks\\');
$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');

$response = $bridge->handle($request);
$this->assertEquals(200, $response->getStatusCode());
$this->assertEquals('Success', (string)$response->getBody());
}

/**
* @runInSeparateProcess
*/
public function testFileUpload()
{
putenv('APP_KERNEL_NAMESPACE=PHPPM\\Tests\\SymfonyMocks\\');
$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');

$response = $bridge->handle($request);
$this->assertEquals(201, $response->getStatusCode());
$this->assertEquals('Uploaded files: testOK.pdf,NULL', (string)$response->getBody());
}

/**
* @runInSeparateProcess
*/
public function testPostJSON()
{
putenv('APP_KERNEL_NAMESPACE=PHPPM\\Tests\\SymfonyMocks\\');
$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');

$response = $bridge->handle($request);
$this->assertEquals(201, $response->getStatusCode());
$this->assertEquals('Received JSON: {"some_json_array":[{"map1":"value1"},{"map2":"value2"}]}', (string)$response->getBody());
}
}
11 changes: 11 additions & 0 deletions tests/SymfonyMocks/Container.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace PHPPM\Tests\SymfonyMocks;

class Container
{
public function has($service)
{
return false;
}
}
76 changes: 76 additions & 0 deletions tests/SymfonyMocks/Kernel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

namespace PHPPM\Tests\SymfonyMocks;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class Kernel
{
private $bundlesInitialized = false;
private $containerInitialized = false;

public function __construct($env, $debug)
{
}

public function initializeBundles()
{
$this->bundlesInitialized = true;
}

public function initializeContainer()
{
$this->containerInitialized = true;
}

public function getBundles()
{
if (!$this->bundlesInitialized) {
throw new \Exception('Bundles not initialized');
}

return [];
}

public function getContainer()
{
if (!$this->containerInitialized) {
throw new \Exception('Container not initialized');
}

return new 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') {
// Simple get request
return new Response('Success', 200);
}
}
}
3 changes: 3 additions & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

require __DIR__ . ' /../vendor/autoload.php';