Re-namespace tests

This commit is contained in:
Timothy Warren 2016-10-19 13:24:08 -04:00
parent 258ebf30ef
commit ffaa4a4cab
26 changed files with 110 additions and 79 deletions

View File

@ -11,4 +11,5 @@
* @license http://www.opensource.org/licenses/mit-license.html MIT License * @license http://www.opensource.org/licenses/mit-license.html MIT License
* @version 1.0.0 * @version 1.0.0
* @link https://git.timshomepage.net/timw4mail/ion * @link https://git.timshomepage.net/timw4mail/ion
*/ */

View File

@ -25,28 +25,30 @@ if ( ! function_exists('glob_recursive'))
function get_text_to_replace($tokens) function get_text_to_replace($tokens)
{ {
if ($tokens[0][0] !== T_OPEN_TAG) $output = '';
// Tokens have the follow structure if arrays:
// [0] => token type constant
// [1] => raw sytax parsed to that token
// [2] => line number
foreach($tokens as $token)
{ {
return NULL; // Since we only care about opening docblocks,
// bail out when we get to the namespace token
if (is_array($token) && $token[0] === T_NAMESPACE)
{
break;
}
if (is_array($token))
{
$token = $token[1];
}
$output .= $token;
} }
// If there is already a docblock, as the second token after the return $output;
// open tag, get the contents of that token to replace
if ($tokens[1][0] === T_DOC_COMMENT)
{
return "<?php\n" . $tokens[1][1];
}
// If there is a declare strict types,
else if ($tokens[1][0] === T_DECLARE && $tokens[9][0] === T_DOC_COMMENT)
{
// '<?php' and 'declare(strict_types=1);' makes for 8 tokens
// replace it all
return "<?php\ndeclare(strict_types=1);\n" . $tokens[9][1];
}
else if ($tokens[1][0] !== T_DOC_COMMENT)
{
return "<?php";
}
} }
function get_tokens($source) function get_tokens($source)
@ -59,6 +61,12 @@ function replace_files(array $files, $template)
foreach ($files as $file) foreach ($files as $file)
{ {
$source = file_get_contents($file); $source = file_get_contents($file);
if (stripos($source, 'namespace') === FALSE)
{
continue;
}
$tokens = get_tokens($source); $tokens = get_tokens($source);
$text_to_replace = get_text_to_replace($tokens); $text_to_replace = get_text_to_replace($tokens);

View File

@ -34,14 +34,13 @@
"humbug/humbug": "~1.0@dev", "humbug/humbug": "~1.0@dev",
"consolidation/robo": "~1.0@RC", "consolidation/robo": "~1.0@RC",
"henrikbjorn/lurker": "^1.1.0", "henrikbjorn/lurker": "^1.1.0",
"nikic/php-parser": "3.0.*@alpha", "nikic/php-parser": "3.0.*@beta",
"monolog/monolog": "1.*", "monolog/monolog": "1.*",
"predis/predis": "1.1.*", "predis/predis": "1.1.*",
"squizlabs/php_codesniffer": "^3.0.0@alpha" "squizlabs/php_codesniffer": "^3.0.0@RC"
}, },
"suggest": { "suggest": {
"monolog/monolog": "Provides implementation of psr/log", "monolog/monolog": "Provides implementation of psr/log",
"predis/predis": "Required for redis cache driver", "predis/predis": "Required for redis cache driver"
"zendframework/zend-diactoros": "Provides implementation of psr/http-message"
} }
} }

View File

@ -29,7 +29,7 @@ trait StringWrapper {
* @param string $str * @param string $str
* @return StringType * @return StringType
*/ */
public function string($str) public function string($str): StringType
{ {
return StringType::create($str); return StringType::create($str);
} }

View File

@ -4,7 +4,7 @@ namespace Aviat\Ion\Tests;
use Aviat\Ion\Model as BaseModel; use Aviat\Ion\Model as BaseModel;
class BaseModelTest extends \Ion_TestCase { class BaseModelTest extends Ion_TestCase {
public function testBaseModelSanity() public function testBaseModelSanity()
{ {

View File

@ -18,8 +18,9 @@ namespace Aviat\Ion\Tests\Cache;
use Aviat\Ion\Friend; use Aviat\Ion\Friend;
use Aviat\Ion\Cache\CacheManager; use Aviat\Ion\Cache\CacheManager;
use Aviat\Ion\Tests\Ion_TestCase;
class CacheManagerTest extends \Ion_TestCase { class CacheManagerTest extends Ion_TestCase {
protected $cachedTime; protected $cachedTime;

View File

@ -17,8 +17,9 @@
namespace Aviat\Ion\Tests\Cache\Driver; namespace Aviat\Ion\Tests\Cache\Driver;
use Aviat\Ion\Cache\Driver\NullDriver; use Aviat\Ion\Cache\Driver\NullDriver;
use Aviat\Ion\Tests\Ion_TestCase;
class CacheNullDriverTest extends \Ion_TestCase { class CacheNullDriverTest extends Ion_TestCase {
use CacheDriverBase; use CacheDriverBase;
protected $driver; protected $driver;

View File

@ -18,8 +18,9 @@ namespace Aviat\Ion\Tests\Cache\Driver;
use Aviat\Ion\Config; use Aviat\Ion\Config;
use Aviat\Ion\Cache\Driver\RedisDriver; use Aviat\Ion\Cache\Driver\RedisDriver;
use Aviat\Ion\Tests\Ion_TestCase;
class CacheRedisDriverTestTwo extends \Ion_TestCase { class CacheRedisDriverTestTwo extends Ion_TestCase {
use CacheDriverBase; use CacheDriverBase;
protected $driver; protected $driver;

View File

@ -17,8 +17,9 @@
namespace Aviat\Ion\Tests\Cache\Driver; namespace Aviat\Ion\Tests\Cache\Driver;
use Aviat\Ion\Cache\Driver\RedisDriver; use Aviat\Ion\Cache\Driver\RedisDriver;
use Aviat\Ion\Tests\Ion_TestCase;
class CacheRedisDriverTest extends \Ion_TestCase { class CacheRedisDriverTest extends Ion_TestCase {
use CacheDriverBase; use CacheDriverBase;
protected $driver; protected $driver;

View File

@ -19,8 +19,9 @@ namespace Aviat\Ion\Tests\Cache\Driver;
use Aviat\Ion\Config; use Aviat\Ion\Config;
use Aviat\Ion\Friend; use Aviat\Ion\Friend;
use Aviat\Ion\Cache\Driver\SQLDriver; use Aviat\Ion\Cache\Driver\SQLDriver;
use Aviat\Ion\Tests\Ion_TestCase;
class CacheSQLDriverTest extends \Ion_TestCase { class CacheSQLDriverTest extends Ion_TestCase {
use CacheDriverBase; use CacheDriverBase;
protected $driver; protected $driver;

View File

@ -4,7 +4,7 @@ namespace Aviat\Ion\Tests;
use Aviat\Ion\Config; use Aviat\Ion\Config;
class ConfigTest extends \Ion_TestCase { class ConfigTest extends Ion_TestCase {
public function setUp() public function setUp()
{ {

View File

@ -16,10 +16,9 @@
namespace Aviat\Ion\Tests\Di; namespace Aviat\Ion\Tests\Di;
use Aviat\Ion\Di\Container; use Aviat\Ion\Di\{Container, ContainerAware, ContainerInterface};
use Aviat\Ion\Di\ContainerAware;
use Aviat\Ion\Di\ContainerInterface;
use Aviat\Ion\Di\Exception\ContainerException; use Aviat\Ion\Di\Exception\ContainerException;
use Aviat\Ion\Tests\Ion_TestCase;
class Aware { class Aware {
use ContainerAware; use ContainerAware;
@ -31,7 +30,7 @@ class Aware {
} }
class ContainerAwareTest extends \Ion_TestCase { class ContainerAwareTest extends Ion_TestCase {
public function setUp() public function setUp()
{ {

View File

@ -18,9 +18,9 @@ namespace Aviat\Ion\Tests\Di;
use Aviat\Ion\Di\Container; use Aviat\Ion\Di\Container;
use Aviat\Ion\Di\Exception\ContainerException; use Aviat\Ion\Di\Exception\ContainerException;
use Aviat\Ion\Tests\Ion_TestCase;
use Monolog\Logger; use Monolog\Logger;
use Monolog\Handler\TestHandler; use Monolog\Handler\{TestHandler, NullHandler};
use Monolog\Handler\NullHandler;
class FooTest { class FooTest {
@ -35,7 +35,7 @@ class FooTest2 {
use \Aviat\Ion\Di\ContainerAware; use \Aviat\Ion\Di\ContainerAware;
} }
class ContainerTest extends \Ion_TestCase { class ContainerTest extends Ion_TestCase {
public function setUp() public function setUp()

View File

@ -3,9 +3,8 @@
namespace Aviat\Ion\Tests; namespace Aviat\Ion\Tests;
use Aviat\Ion\Enum; use Aviat\Ion\Enum;
use TestEnum;
class EnumTest extends \Ion_TestCase { class EnumTest extends Ion_TestCase {
protected $expectedConstList = [ protected $expectedConstList = [
'FOO' => 'bar', 'FOO' => 'bar',

View File

@ -17,8 +17,9 @@
namespace Aviat\Ion\Tests\Exception; namespace Aviat\Ion\Tests\Exception;
use Aviat\Ion\Exception\DoubleRenderException; use Aviat\Ion\Exception\DoubleRenderException;
use Aviat\Ion\Tests\Ion_TestCase;
class DoubleRenderExceptionTest extends \Ion_TestCase { class DoubleRenderExceptionTest extends Ion_TestCase {
public function testDefaultMessage() public function testDefaultMessage()
{ {

View File

@ -3,13 +3,14 @@
namespace Aviat\Ion\Tests; namespace Aviat\Ion\Tests;
use Aviat\Ion\Friend; use Aviat\Ion\Friend;
use Aviat\Ion\Tests\FriendTestClass;
class FriendTest extends \Ion_TestCase { class FriendTest extends Ion_TestCase {
public function setUp() public function setUp()
{ {
parent::setUp(); parent::setUp();
$obj = new \FriendTestClass(); $obj = new FriendTestClass();
$this->friend = new Friend($obj); $this->friend = new Friend($obj);
} }

View File

@ -1,15 +1,17 @@
<?php <?php
namespace Aviat\Ion\Tests;
use Aura\Web\WebFactory; use Aura\Web\WebFactory;
use Aviat\Ion\Json;
use GuzzleHttp\Client; use GuzzleHttp\Client;
use GuzzleHttp\Handler\MockHandler; use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack; use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response; use GuzzleHttp\Psr7\Response;
use PHPUnit\Framework\TestCase;
use Zend\Diactoros\ServerRequestFactory; use Zend\Diactoros\ServerRequestFactory;
use Zend\Diactoros\Response as HttpResponse; use Zend\Diactoros\Response as HttpResponse;
use Aviat\Ion\Json;
define('ROOT_DIR', realpath(__DIR__ . '/../') . '/'); define('ROOT_DIR', realpath(__DIR__ . '/../') . '/');
define('SRC_DIR', ROOT_DIR . 'src/'); define('SRC_DIR', ROOT_DIR . 'src/');
define('TEST_DATA_DIR', __DIR__ . '/test_data'); define('TEST_DATA_DIR', __DIR__ . '/test_data');
@ -18,7 +20,7 @@ define('TEST_VIEW_DIR', __DIR__ . '/test_views');
/** /**
* Base class for TestCases * Base class for TestCases
*/ */
class Ion_TestCase extends PHPUnit_Framework_TestCase { class Ion_TestCase extends TestCase {
// Test directory constants // Test directory constants
const ROOT_DIR = ROOT_DIR; const ROOT_DIR = ROOT_DIR;
const SRC_DIR = SRC_DIR; const SRC_DIR = SRC_DIR;
@ -102,7 +104,7 @@ class Ion_TestCase extends PHPUnit_Framework_TestCase {
* @param array $supers * @param array $supers
* @return void * @return void
*/ */
public function setSuperGlobals($supers = []) public function setSuperGlobals(array $supers = [])
{ {
$default = [ $default = [
'_SERVER' => $_SERVER, '_SERVER' => $_SERVER,

View File

@ -2,10 +2,9 @@
namespace Aviat\Ion\Tests; namespace Aviat\Ion\Tests;
use Aviat\Ion\Json; use Aviat\Ion\{Json, JsonException};
use Aviat\Ion\JsonException;
class JsonTest extends \Ion_TestCase { class JsonTest extends Ion_TestCase {
public function testEncode() public function testEncode()
{ {

View File

@ -17,8 +17,9 @@
namespace Aviat\Ion\Tests\Model; namespace Aviat\Ion\Tests\Model;
use Aviat\Ion\Model\DB as BaseDBModel; use Aviat\Ion\Model\DB as BaseDBModel;
use Aviat\Ion\Tests\Ion_TestCase;
class BaseDBModelTest extends \Ion_TestCase { class BaseDBModelTest extends Ion_TestCase {
public function testBaseDBModelSanity() public function testBaseDBModelSanity()
{ {

View File

@ -1,16 +1,20 @@
<?php <?php
namespace Aviat\Ion\Tests;
use SessionHandlerInterface;
class TestSessionHandler implements SessionHandlerInterface { class TestSessionHandler implements SessionHandlerInterface {
public $data = []; public $data = [];
public $save_path = './test_data/sessions'; public $save_path = './test_data/sessions';
public function close() public function close()
{ {
return TRUE; return TRUE;
} }
public function destroy($id) public function destroy($id)
{ {
$file = "$this->save_path/$id"; $file = "$this->save_path/$id";
if (file_exists($file)) if (file_exists($file))
@ -20,13 +24,13 @@ class TestSessionHandler implements SessionHandlerInterface {
$this->data[$id] = []; $this->data[$id] = [];
return TRUE; return TRUE;
} }
public function gc($maxLifetime) public function gc($maxLifetime)
{ {
return TRUE; return TRUE;
} }
public function open($save_path, $name) public function open($save_path, $name)
{ {
/*if ( ! array_key_exists($save_path, $this->data)) /*if ( ! array_key_exists($save_path, $this->data))
{ {
@ -35,19 +39,19 @@ class TestSessionHandler implements SessionHandlerInterface {
}*/ }*/
return TRUE; return TRUE;
} }
public function read($id) public function read($id)
{ {
return json_decode(@file_get_contents("$this->save_path/$id"), TRUE); return json_decode(@file_get_contents("$this->save_path/$id"), TRUE);
} }
public function write($id, $data) public function write($id, $data)
{ {
$file = "$this->save_path/$id"; $file = "$this->save_path/$id";
file_put_contents($file, json_encode($data)); file_put_contents($file, json_encode($data));
return TRUE; return TRUE;
} }
} }
// End of TestSessionHandler.php // End of TestSessionHandler.php

View File

@ -16,14 +16,17 @@
namespace Aviat\Ion\Tests\Transformer; namespace Aviat\Ion\Tests\Transformer;
class AbstractTransformerTest extends \Ion_TestCase { use Aviat\Ion\Tests\Ion_TestCase;
use Aviat\Ion\Tests\TestTransformer;
class AbstractTransformerTest extends Ion_TestCase {
protected $transformer; protected $transformer;
public function setUp() public function setUp()
{ {
$this->transformer = new \TestTransformer(); $this->transformer = new TestTransformer();
} }
public function dataTransformCollection() public function dataTransformCollection()

View File

@ -16,9 +16,11 @@
namespace Aviat\Ion\Tests\Type; namespace Aviat\Ion\Tests\Type;
class ArrayTypeTest extends \Ion_TestCase { use Aviat\Ion\ArrayWrapper;
use \Aviat\Ion\ArrayWrapper; use Aviat\Ion\Tests\Ion_TestCase;
class ArrayTypeTest extends Ion_TestCase {
use ArrayWrapper;
public function setUp() public function setUp()
{ {

View File

@ -16,6 +16,10 @@
namespace Aviat\Ion\Tests\View; namespace Aviat\Ion\Tests\View;
use Aviat\Ion\Tests\TestHtmlView;
use function _dir;
class HtmlViewTest extends HttpViewTest { class HtmlViewTest extends HttpViewTest {
protected $template_path; protected $template_path;
@ -23,7 +27,7 @@ class HtmlViewTest extends HttpViewTest {
public function setUp() public function setUp()
{ {
parent::setUp(); parent::setUp();
$this->view = new \TestHtmlView($this->container); $this->view = new TestHtmlView($this->container);
} }
public function testRenderTemplate() public function testRenderTemplate()

View File

@ -18,8 +18,10 @@ namespace Aviat\Ion\Tests\View;
use Aviat\Ion\Friend; use Aviat\Ion\Friend;
use Aviat\Ion\Exception\DoubleRenderException; use Aviat\Ion\Exception\DoubleRenderException;
use Aviat\Ion\Tests\Ion_TestCase;
use Aviat\Ion\Tests\TestHttpView;
class HttpViewTest extends \Ion_TestCase { class HttpViewTest extends Ion_TestCase {
protected $view; protected $view;
protected $friend; protected $friend;
@ -27,7 +29,7 @@ class HttpViewTest extends \Ion_TestCase {
public function setUp() public function setUp()
{ {
parent::setUp(); parent::setUp();
$this->view = new \TestHttpView($this->container); $this->view = new TestHttpView($this->container);
$this->friend = new Friend($this->view); $this->friend = new Friend($this->view);
} }

View File

@ -17,6 +17,7 @@
namespace Aviat\Ion\Tests\View; namespace Aviat\Ion\Tests\View;
use Aviat\Ion\Friend; use Aviat\Ion\Friend;
use Aviat\Ion\Tests\TestJsonView;
class JsonViewTest extends HttpViewTest { class JsonViewTest extends HttpViewTest {
@ -24,14 +25,14 @@ class JsonViewTest extends HttpViewTest {
{ {
parent::setUp(); parent::setUp();
$this->view = new \TestJsonView($this->container); $this->view = new TestJsonView($this->container);
$this->friend = new Friend($this->view); $this->friend = new Friend($this->view);
} }
public function testSetOutputJSON() public function testSetOutputJSON()
{ {
// Extend view class to remove destructor which does output // Extend view class to remove destructor which does output
$view = new \TestJsonView($this->container); $view = new TestJsonView($this->container);
// Json encode non-string // Json encode non-string
$content = ['foo' => 'bar']; $content = ['foo' => 'bar'];
@ -43,7 +44,7 @@ class JsonViewTest extends HttpViewTest {
public function testSetOutput() public function testSetOutput()
{ {
// Directly set string // Directly set string
$view = new \TestJsonView($this->container); $view = new TestJsonView($this->container);
$content = '{}'; $content = '{}';
$expected = '{}'; $expected = '{}';
$view->setOutput($content); $view->setOutput($content);

View File

@ -3,14 +3,14 @@
* All the mock classes that extend the classes they are used to test * All the mock classes that extend the classes they are used to test
*/ */
namespace Aviat\Ion\Tests;
use Aviat\Ion\Enum; use Aviat\Ion\Enum;
use Aviat\Ion\Exception\DoubleRenderException; use Aviat\Ion\Exception\DoubleRenderException;
use Aviat\Ion\Friend; use Aviat\Ion\Friend;
use Aviat\Ion\Transformer\AbstractTransformer; use Aviat\Ion\Transformer\AbstractTransformer;
use Aviat\Ion\View; use Aviat\Ion\View;
use Aviat\Ion\View\HtmlView; use Aviat\Ion\View\{HtmlView, HttpView, JsonView};
use Aviat\Ion\View\HttpView;
use Aviat\Ion\View\JsonView;
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
// Mock the default error handler // Mock the default error handler