Skip to content
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

Php82 phpunit9 slim4 #45

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 2 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ class VersionTest extends LocalWebTestCase
}
```

Here is an example on how to pass data to a POST endpoint in a test case and
Here is an example on how to pass data to a POST endpoint in a test case and
retrieve it later in the endpoint. We are passing encoded JSON data in the body
of the request. The data is retrieved in the endpoint using
of the request. The data is retrieved in the endpoint using
`$app->request->getBody()`.

```php
Expand All @@ -50,28 +50,6 @@ $app->post('/user', function() use ($app) {
});
```

### Example with DbUnit

If you wish to use Database fixture, use class `WebDbTestCase`. *Caution*: Make
sure the names you use for you fixture models won't conflict with your actual
DB tables.

```php
class LocalDbWebTestCase extends \There4\Slim\Test\WebDbTestCase
{
/**
* You must implement this method
* @return PHPUnit_Extensions_Database_DataSet_IDataSet
*/
public function getDataSet()
{
return $this->createFlatXMLDataSet(
dirname(__FILE__) . DIRECTORY_SEPARATOR . 'fixture.xml'
);
}
}
```

## Setup

You'll need a bootstrap file for phpunit that can instantiate your Slim application. You can see [an example boostrap] in [the sample app][example].
Expand Down
14 changes: 8 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,16 @@
"source": "https://github.com/there4/slim-test-helpers"
},
"require": {
"slim/slim": "~3.1",
"phpunit/phpunit": "^4.8|5.*|6.*",
"phpunit/dbunit": "2.*|3.*",
"illuminate/database": ">=4.0"
"php": "^8.2.3",
"slim/slim": "^4.12.0",
"phpunit/phpunit": "9.*",
"illuminate/database": "^10",
"illuminate/container": "^10.0"
},
"require-dev": {
"squizlabs/php_codesniffer": "2.*",
"codeclimate/php-test-reporter": "^0.3"
"squizlabs/php_codesniffer": "3.*",
"slim/psr7": "^1.6",
"slim/http": "^1.3"
},
"autoload": {
"psr-0": {
Expand Down
44 changes: 21 additions & 23 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit strict="false"
backupGlobals="false"
backupStaticAttributes="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
bootstrap="tests/bootstrap.php">

<testsuites>
<testsuite name="Slim Test Helpers Test Suite">
<directory suffix="Test.php" phpVersion="5.3.2" phpVersionOperator=">=">tests</directory>
</testsuite>
</testsuites>

<filter>
<whitelist processUncoveredFilesFromWhitelist="false">
<directory suffix=".php">src</directory>
</whitelist>
</filter>

<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
backupGlobals="false"
backupStaticAttributes="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
bootstrap="tests/bootstrap.php"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage processUncoveredFiles="false">
<include>
<directory suffix=".php">src</directory>
</include>
</coverage>
<testsuites>
<testsuite name="Slim Test Helpers Test Suite">
<directory suffix="Test.php" phpVersion="7.4.0" phpVersionOperator="&gt;=">tests</directory>
</testsuite>
</testsuites>
</phpunit>
33 changes: 0 additions & 33 deletions src/There4/Slim/Test/NoCacheRouter.php

This file was deleted.

16 changes: 11 additions & 5 deletions src/There4/Slim/Test/SlimInstance.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
<?php
namespace There4\Slim\Test;

use Illuminate\Container\Container;
use Illuminate\Support\Collection;
use \Slim\App;
use Slim\Factory\AppFactory;
use Slim\Psr7\Environment;

/**
* Static class to get configured Slim App Instance for use for TestCases
Expand All @@ -24,12 +28,14 @@ public static function getInstance(array $settings = [])
'routerCacheFile' => false
);

// Merge user settings
$settings = array(
'settings' => array_merge($defaultSettings, $settings)
);
$container = new Container();
$container['settings'] = new Collection(array_merge($defaultSettings, $settings));
$container['environment'] = new Collection(Environment::mock());


// Create App
return new App($settings);
AppFactory::setContainer($container);

return AppFactory::create();
}
}
34 changes: 16 additions & 18 deletions src/There4/Slim/Test/WebDbTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
namespace There4\Slim\Test;

use PDO;
use PHPUnit\DbUnit\TestCase;
use PHPUnit\DbUnit\DataSet\QueryDataSet;
use \PHPUnit\Framework\TestCase;

class WebDbTestCase extends TestCase
{
Expand All @@ -15,10 +14,20 @@ class WebDbTestCase extends TestCase
public $client;

/** Database Connection **/
protected $conn;
private static $conn;

public static function setUpBeforeClass() : void
{
self::$conn = new PDO('sqlite::memory:');
}

public static function tearDownAfterClass(): void
{
self::$conn = null;
}

// Run for each unit test to setup our slim app environment
public function setup()
protected function setUp() : void
{
parent::setUp();

Expand All @@ -29,24 +38,13 @@ public function setup()

// Instantiate a Slim application for use in our testing environment. You
// will most likely override this for your own application.
public function getSlimInstance()
public function getSlimInstance() : object
{
return SlimInstance::getInstance();
}

public function getConnection()
{
if ($this->conn === null) {
$pdo = new PDO('sqlite::memory:');
$this->conn = $this->createDefaultDBConnection($pdo, ':memory:');
}
return $this->conn;
}

public function getDataSet()
public function getConnection() : object
{
return new QueryDataSet(
$this->getConnection()
);
return static::$conn;
}
}
2 changes: 1 addition & 1 deletion src/There4/Slim/Test/WebTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class WebTestCase extends \PHPUnit\Framework\TestCase
protected $client;

// Run for each unit test to setup our slim app environment
public function setup()
protected function setUp() : void
{
// Establish a local reference to the Slim app object
// Ensure no cache Router
Expand Down
66 changes: 45 additions & 21 deletions src/There4/Slim/Test/WebTestClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,25 @@
namespace There4\Slim\Test;

use Slim\App;
use Slim\Http\Environment;
use Slim\Http\Headers;
use Slim\Http\Request;
use Slim\Http\RequestBody;
use Slim\Http\Response;
use Slim\Http\Uri;

use Slim\Psr7\Environment;
use Slim\Psr7\Factory\StreamFactory;
use Slim\Psr7\Headers;
use Slim\Psr7\Request as SlimRequest;
use Slim\Psr7\Uri;

class WebTestClient
{
/** @var \Slim\App */
public $app;

/** @var \Slim\Http\Request */
/** @var \Slim\Psr7\Request */
public $request;

/** @var \Slim\Http\Response */
/** @var \Slim\Psr7\Response */
public $response;

private $cookies = array();
protected $cookies = array();

public function __construct(App $slim)
{
Expand Down Expand Up @@ -70,7 +70,7 @@ public function options($path, $data = array(), $optionalHeaders = array())

// Abstract way to make a request to SlimPHP, this allows us to mock the
// slim environment
private function request($method, $path, $data = array(), $optionalHeaders = array())
protected function request($method, $path, $data = array(), $optionalHeaders = array())
{
//Make method uppercase
$method = strtoupper($method);
Expand All @@ -79,32 +79,56 @@ private function request($method, $path, $data = array(), $optionalHeaders = arr
'REQUEST_URI' => $path
);

$query = '';
if ($method === 'GET') {
$options['QUERY_STRING'] = http_build_query($data);
$query = http_build_query($data);
} else {
$params = json_encode($data);
}

// Prepare a mock environment
$env = Environment::mock(array_merge($options, $optionalHeaders));
$uri = Uri::createFromEnvironment($env);
$headers = Headers::createFromEnvironment($env);

// $uri = Uri::createFromEnvironment($env);
$uri = new Uri($env['REQUEST_SCHEME'], $env['SERVER_NAME'], $env['SERVER_PORT'], $path, $query);
$handle = fopen('php://temp', 'w+');
$stream = (new StreamFactory())->createStreamFromResource($handle);

$headerdata = [];
$special = [
'CONTENT_TYPE' => 1,
'CONTENT_LENGTH' => 1,
'PHP_AUTH_USER' => 1,
'PHP_AUTH_PW' => 1,
'PHP_AUTH_DIGEST' => 1,
'AUTH_TYPE' => 1,
];
foreach ($env as $key => $value) {
$key = strtoupper($key);
if (isset($special[$key]) || strpos($key, 'HTTP_') === 0) {
if ($key !== 'HTTP_CONTENT_LENGTH') {
$headerdata[$key] = $value;
}
}
}
$headers = new Headers($headerdata);

// $headers = Headers::createFromEnvironment($env);
$cookies = $this->cookies;
$serverParams = $env->all();
$body = new RequestBody();

// $serverParams = $env->all();
// $body = new RequestBody();

// Attach JSON request
if (isset($params)) {
$headers->set('Content-Type', 'application/json;charset=utf8');
$body->write($params);
$headers->addHeader('Content-Type', 'application/json;charset=utf8');
$stream->write($params);
}

$this->request = new Request($method, $uri, $headers, $cookies, $serverParams, $body);
$response = new Response();
$this->request = new SlimRequest(strtoupper($method), $uri, $headers, $cookies, $env /* $serverParams */, $stream);

// Process request
$app = $this->app;
$this->response = $app->process($this->request, $response);
$this->response = $this->app->handle($this->request);

// Return the application output.
return (string)$this->response->getBody();
Expand Down
11 changes: 1 addition & 10 deletions tests/There4Test/Slim/Test/WebDbTestCaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public function testExtendsDbUnit()
{
$testCase = new WebDbTestCase();
self::assertInstanceOf(
'\PHPUnit\DbUnit\TestCase',
'\PHPUnit\Framework\TestCase',
$testCase
);
}
Expand Down Expand Up @@ -44,13 +44,4 @@ public function testGetSlimInstance()
);
}
}

public function testGetDataset()
{
$testCase = new WebDbTestCase();
self::assertInstanceOf(
'\PHPUnit\DbUnit\DataSet\QueryDataSet',
$testCase->getDataSet()
);
}
}
Loading