Skip to content

Commit 211801a

Browse files
committed
PSR-4 comliant tests
1 parent cca56e7 commit 211801a

11 files changed

+58
-21
lines changed

phpunit.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<phpunit>
1+
<phpunit bootstrap="tests/autoload.php">
22
<php>
33
<ini name="display_errors" value="true"/>
44
</php>

tests/Api.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
namespace PhpCrudApi\Tests;
23

34
require_once(__DIR__ . '/../api.php');
45

@@ -12,12 +13,12 @@ class Api
1213
protected $config;
1314

1415
/**
15-
* @var PHP_CRUD_API_Test
16+
* @var PhpCrudApi\Tests\BaseTest
1617
*/
1718
protected $test;
1819

1920
/**
20-
* @var PHP_CRUD_API
21+
* @var \PHP_CRUD_API
2122
*/
2223
protected $api;
2324

@@ -34,7 +35,7 @@ private function action($method,$url,$data='')
3435
$query = isset($url['query'])?$url['query']:'';
3536
parse_str($query,$get);
3637

37-
$this->api = new PHP_CRUD_API(array(
38+
$this->api = new \PHP_CRUD_API(array(
3839
'dbengine'=>$this->config['dbengine'],
3940
'hostname'=>$this->config['hostname'],
4041
'username'=>$this->config['username'],

tests/Config.php.dist

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
namespace PhpCrudApi\Tests;
23

34
class Config
45
{

tests/Config.php.travis

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
namespace PhpCrudApi\Tests;
23

34
class Config
45
{

tests/MysqlTest.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<?php
2-
3-
require_once(__DIR__ . '/Tests.php');
2+
namespace PhpCrudApi\Tests;
43

54
class MysqlTest extends Tests
65
{

tests/PostgresqlTest.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<?php
2-
3-
require_once(__DIR__ . '/Tests.php');
2+
namespace PhpCrudApi\Tests;
43

54
class PostgresqlTest extends Tests
65
{

tests/SqlServerTest.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<?php
2-
3-
require_once(__DIR__ . '/Tests.php');
2+
namespace PhpCrudApi\Tests;
43

54
class SqlServerTest extends Tests
65
{

tests/SqliteTest.php

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<?php
2-
3-
require_once(__DIR__ . '/Tests.php');
2+
namespace PhpCrudApi\Tests;
43

54
class SqliteTest extends Tests
65
{
@@ -21,10 +20,10 @@ public function getEngineName()
2120
*/
2221
public function connect($config)
2322
{
24-
$db = new SQLite3($config['database']);
23+
$db = new \SQLite3($config['database']);
2524

2625
if (!$db) {
27-
die("Could not open '$database' SQLite database: ".SQLite3::lastErrorMsg().' ('.SQLite3::lastErrorCode().")\n");
26+
die("Could not open '$database' SQLite database: ".\SQLite3::lastErrorMsg().' ('.\SQLite3::lastErrorCode().")\n");
2827
}
2928

3029
return $db;
@@ -49,7 +48,7 @@ public function checkVersion($db)
4948
{
5049
$major = 3;
5150
$minor = 0;
52-
$version = SQLite3::version();
51+
$version = \SQLite3::version();
5352
$v = explode('.',$version['versionString']);
5453
if ($v[0]<$major || ($v[0]==$major && $v[1]<$minor)) {
5554
die("Detected SQLite $v[0].$v[1], but only $major.$minor and up are supported\n");

tests/TestBase.php

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
<?php
2+
namespace PhpCrudApi\Tests;
23

3-
require_once(__DIR__ . '/Config.php');
4-
5-
abstract class TestBase extends PHPUnit_Framework_TestCase
4+
abstract class TestBase extends \PHPUnit_Framework_TestCase
65
{
76
public static function setUpBeforeClass()
87
{

tests/Tests.php

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
<?php
2-
3-
require_once(__DIR__ . '/TestBase.php');
4-
require_once(__DIR__ . '/Api.php');
2+
namespace PhpCrudApi\Tests;
53

64
abstract class Tests extends TestBase
75
{

tests/autoload.php

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
/**
3+
* An example of a project-specific implementation.
4+
*
5+
* After registering this autoload function with SPL, the following line
6+
* would cause the function to attempt to load the \Foo\Bar\Baz\Qux class
7+
* from /path/to/project/src/Baz/Qux.php:
8+
*
9+
* new \Foo\Bar\Baz\Qux;
10+
*
11+
* @param string $class The fully-qualified class name.
12+
* @return void
13+
*/
14+
spl_autoload_register(function ($class) {
15+
16+
// project-specific namespace prefix
17+
$prefix = 'PhpCrudApi\\Tests\\';
18+
19+
// base directory for the namespace prefix
20+
$base_dir = __DIR__ . '/';
21+
22+
// does the class use the namespace prefix?
23+
$len = strlen($prefix);
24+
if (strncmp($prefix, $class, $len) !== 0) {
25+
// no, move to the next registered autoloader
26+
return;
27+
}
28+
29+
// get the relative class name
30+
$relative_class = substr($class, $len);
31+
32+
// replace the namespace prefix with the base directory, replace namespace
33+
// separators with directory separators in the relative class name, append
34+
// with .php
35+
$file = $base_dir . str_replace('\\', '/', $relative_class) . '.php';
36+
37+
// if the file exists, require it
38+
if (file_exists($file)) {
39+
require $file;
40+
}
41+
});

0 commit comments

Comments
 (0)