Skip to content
Browse files

* built a small autoloader for the test suite

 * removed all require statements
 * skip tests when extensions are missing (mcrypt, oauth)
 * fix all kinds of notices/warnings due to missing class variables in tests
 * add phpunit.xml to bootstrap the test suite (cd src/tests && phpunit :))
  • Loading branch information...
1 parent e37875f commit 307fe4f33d25c0ea0162f1c5e2ead1236eb44657 @till committed Apr 22, 2012
View
1 .gitignore
@@ -3,6 +3,7 @@
*~
*.swp
*.graffle
+.idea
# Useful files which should never be committed
src/configs/override.ini
View
29 src/tests/helpers/autoload.php
@@ -0,0 +1,29 @@
+<?php
+/**
+ * Avoid require_once in all test cases and attempt to load classes here.
+ *
+ * @param string $class
+ *
+ * @return bool|mixed
+ */
+function testsuite_autoload($class) {
+ static $base;
+ if (null === $base) {
+ $base = dirname(dirname(dirname(__FILE__))) . '/libraries';
+ }
+ /**
+ * @todo PSR-0
+ */
+ if (substr($class, -9) === 'Interface') {
+ $class = substr($class, 0, -9);
+ }
+ static $folders = array('adapters', 'models');
+ foreach ($folders as $folder) {
+ $path = sprintf("%s/%s/%s.php", $base, $folder, $class);
+ if (true === file_exists($path)) {
+ return include $path;
+ }
+ }
+ return false;
+}
+spl_autoload_register('testsuite_autoload');
View
6 src/tests/helpers/init.php
@@ -1,6 +1,10 @@
<?php
date_default_timezone_set('America/Los_Angeles');
-require_once 'PHPUnit/Framework.php';
+require_once 'PHPUnit/Autoload.php';
+require_once dirname(__FILE__) . '/mysql.php';
+require_once dirname(__FILE__) . '/aws.php';
+require_once dirname(__FILE__) . '/vfs.php';
+require_once dirname(__FILE__) . '/autoload.php';
// stub out exceptions
class OPException extends Exception
View
14 src/tests/helpers/vfs.php
@@ -0,0 +1,14 @@
+<?php
+/**
+ * Created by JetBrains PhpStorm.
+ * User: till
+ * Date: 4/22/12
+ * Time: 12:28 AM
+ * To change this template use File | Settings | File Templates.
+ */
+$paths = (array)explode(PATH_SEPARATOR, ini_get('include_path'));
+foreach($paths as $path)
+{
+ if(file_exists("{$path}/vfsStream/vfsStream.php"))
+ require_once 'vfsStream/vfsStream.php';
+}
View
4 src/tests/libraries/adapters/DatabaseMySqlTest.php
@@ -1,10 +1,6 @@
<?php
$baseDir = dirname(dirname(dirname(dirname(__FILE__))));
-require_once sprintf('%s/tests/helpers/init.php', $baseDir);
require_once sprintf('%s/tests/helpers/mysql.php', $baseDir);
-require_once sprintf('%s/libraries/adapters/Database.php', $baseDir);
-require_once sprintf('%s/libraries/adapters/DatabaseMySql.php', $baseDir);
-
class DatabaseMySqlOverride extends DatabaseMySql
{
View
7 src/tests/libraries/adapters/DatabaseSimpleDbTest.php
@@ -1,11 +1,4 @@
<?php
-$baseDir = dirname(dirname(dirname(dirname(__FILE__))));
-require_once sprintf('%s/tests/helpers/init.php', $baseDir);
-require_once sprintf('%s/tests/helpers/mysql.php', $baseDir);
-require_once sprintf('%s/libraries/adapters/Database.php', $baseDir);
-require_once sprintf('%s/libraries/adapters/DatabaseSimpleDb.php', $baseDir);
-require_once sprintf('%s/libraries/models/Utility.php', $baseDir);
-
class DatabaseSimpleDbOverride extends DatabaseSimpleDb
{
public function __construct($config = null, $params = null)
View
11 src/tests/libraries/adapters/FileSystemLocalTest.php
@@ -1,15 +1,4 @@
<?php
-$paths = (array)explode(PATH_SEPARATOR, ini_get('include_path'));
-foreach($paths as $path)
-{
- if(file_exists("{$path}/vfsStream/vfsStream.php"))
- require_once 'vfsStream/vfsStream.php';
-}
-$baseDir = dirname(dirname(dirname(dirname(__FILE__))));
-require_once sprintf('%s/tests/helpers/init.php', $baseDir);
-require_once sprintf('%s/libraries/adapters/FileSystem.php', $baseDir);
-require_once sprintf('%s/libraries/adapters/FileSystemLocal.php', $baseDir);
-
/*class FileSystemS3Override extends FileSystemS3
{
public function __construct($config = null, $params = null)
View
6 src/tests/libraries/adapters/FileSystemS3Test.php
@@ -1,10 +1,4 @@
<?php
-$baseDir = dirname(dirname(dirname(dirname(__FILE__))));
-require_once sprintf('%s/tests/helpers/init.php', $baseDir);
-require_once sprintf('%s/tests/helpers/aws.php', $baseDir);
-require_once sprintf('%s/libraries/adapters/FileSystem.php', $baseDir);
-require_once sprintf('%s/libraries/adapters/FileSystemS3.php', $baseDir);
-
class FileSystemS3Override extends FileSystemS3
{
public function __construct($config = null, $params = null)
View
5 src/tests/libraries/models/ActionTest.php
@@ -1,9 +1,4 @@
<?php
-$baseDir = dirname(dirname(dirname(dirname(__FILE__))));
-require_once sprintf('%s/tests/helpers/init.php', $baseDir);
-require_once sprintf('%s/libraries/models/BaseModel.php', $baseDir);
-require_once sprintf('%s/libraries/models/Action.php', $baseDir);
-
class ActionTest extends PHPUnit_Framework_TestCase
{
public function setUp()
View
11 src/tests/libraries/models/AssetPipelineTest.php
@@ -1,15 +1,4 @@
<?php
-$paths = (array)explode(PATH_SEPARATOR, ini_get('include_path'));
-foreach($paths as $path)
-{
- if(file_exists("{$path}/vfsStream/vfsStream.php"))
- require_once 'vfsStream/vfsStream.php';
-}
-$baseDir = dirname(dirname(dirname(dirname(__FILE__))));
-require_once sprintf('%s/tests/helpers/init.php', $baseDir);
-require_once sprintf('%s/libraries/models/BaseModel.php', $baseDir);
-require_once sprintf('%s/libraries/models/AssetPipeline.php', $baseDir);
-
class AssetPipelineOverride extends AssetPipeline
{
public $docroot;
View
5 src/tests/libraries/models/AuthenticationTest.php
@@ -1,9 +1,4 @@
<?php
-$baseDir = dirname(dirname(dirname(dirname(__FILE__))));
-require_once sprintf('%s/tests/helpers/init.php', $baseDir);
-require_once sprintf('%s/libraries/models/BaseModel.php', $baseDir);
-require_once sprintf('%s/libraries/models/Authentication.php', $baseDir);
-
$_REQUEST['oauth_consumer_key'] = 'foo';
class AuthenticationWrapper extends Authentication
{
View
9 src/tests/libraries/models/CredentialTest.php
@@ -1,9 +1,4 @@
<?php
-$baseDir = dirname(dirname(dirname(dirname(__FILE__))));
-require_once sprintf('%s/tests/helpers/init.php', $baseDir);
-require_once sprintf('%s/libraries/models/BaseModel.php', $baseDir);
-require_once sprintf('%s/libraries/models/Credential.php', $baseDir);
-
class CredentialTest extends PHPUnit_Framework_TestCase
{
public $headers = array('Authorization' => 'OAuth realm="http://sp.example.com/",
@@ -17,6 +12,10 @@ class CredentialTest extends PHPUnit_Framework_TestCase
public function setUp()
{
+ if (!extension_loaded('oauth')) {
+ $this->markTestSkipped("Test requires ext/oauth");
+ return;
+ }
$utility = $this->getMock('Utility', array('getAllHeaders'));
$utility->expects($this->any())
->method('getAllHeaders')
View
5 src/tests/libraries/models/GroupTest.php
@@ -1,9 +1,4 @@
<?php
-$baseDir = dirname(dirname(dirname(dirname(__FILE__))));
-require_once sprintf('%s/tests/helpers/init.php', $baseDir);
-require_once sprintf('%s/libraries/models/BaseModel.php', $baseDir);
-require_once sprintf('%s/libraries/models/Group.php', $baseDir);
-
class GroupTest extends PHPUnit_Framework_TestCase
{
public function setUp()
View
5 src/tests/libraries/models/HttpTest.php
@@ -1,9 +1,4 @@
<?php
-$baseDir = dirname(dirname(dirname(dirname(__FILE__))));
-require_once sprintf('%s/tests/helpers/init.php', $baseDir);
-require_once sprintf('%s/libraries/models/BaseModel.php', $baseDir);
-require_once sprintf('%s/libraries/models/Http.php', $baseDir);
-
class HttpWrapper extends Http
{
protected function generateCommand($method, $paramsAsString, $url)
View
5 src/tests/libraries/models/PhotoTest.php
@@ -1,9 +1,4 @@
<?php
-$baseDir = dirname(dirname(dirname(dirname(__FILE__))));
-require_once sprintf('%s/tests/helpers/init.php', $baseDir);
-require_once sprintf('%s/libraries/models/BaseModel.php', $baseDir);
-require_once sprintf('%s/libraries/models/Photo.php', $baseDir);
-
class PhotoTest extends PHPUnit_Framework_TestCase
{
public function setUp()
View
5 src/tests/libraries/models/PluginBaseTest.php
@@ -1,9 +1,4 @@
<?php
-$baseDir = dirname(dirname(dirname(dirname(__FILE__))));
-require_once sprintf('%s/tests/helpers/init.php', $baseDir);
-require_once sprintf('%s/libraries/models/BaseModel.php', $baseDir);
-require_once sprintf('%s/libraries/models/PluginBase.php', $baseDir);
-
class PluginBaseTest extends PHPUnit_Framework_TestCase
{
public function setUp()
View
12 src/tests/libraries/models/PluginTestSkip.php
@@ -1,16 +1,4 @@
<?php
-$paths = (array)explode(PATH_SEPARATOR, ini_get('include_path'));
-foreach($paths as $path)
-{
- if(file_exists("{$path}/vfsStream/vfsStream.php"))
- require_once 'vfsStream/vfsStream.php';
-}
-$baseDir = dirname(dirname(dirname(dirname(__FILE__))));
-require_once sprintf('%s/tests/helpers/init.php', $baseDir);
-require_once sprintf('%s/libraries/models/BaseModel.php', $baseDir);
-require_once sprintf('%s/libraries/models/PluginBase.php', $baseDir);
-require_once sprintf('%s/libraries/models/Plugin.php', $baseDir);
-
class Fake1Plugin extends PluginBase
{
public function defineConf()
View
5 src/tests/libraries/models/TagTest.php
@@ -1,9 +1,4 @@
<?php
-$baseDir = dirname(dirname(dirname(dirname(__FILE__))));
-require_once sprintf('%s/tests/helpers/init.php', $baseDir);
-require_once sprintf('%s/libraries/models/BaseModel.php', $baseDir);
-require_once sprintf('%s/libraries/models/Tag.php', $baseDir);
-
class TagTest extends PHPUnit_Framework_TestCase
{
public function setUp()
View
15 src/tests/libraries/models/UpgradeTest.php
@@ -1,17 +1,10 @@
<?php
-$paths = (array)explode(PATH_SEPARATOR, ini_get('include_path'));
-foreach($paths as $path)
-{
- if(file_exists("{$path}/vfsStream/vfsStream.php"))
- require_once 'vfsStream/vfsStream.php';
-}
-$baseDir = dirname(dirname(dirname(dirname(__FILE__))));
-require_once sprintf('%s/tests/helpers/init.php', $baseDir);
-require_once sprintf('%s/libraries/models/BaseModel.php', $baseDir);
-require_once sprintf('%s/libraries/models/Upgrade.php', $baseDir);
-
class UpgradeTest extends PHPUnit_Framework_TestCase
{
+ protected $config;
+ protected $scriptsDir;
+ protected $upgrade;
+
public function setUp()
{
if(class_exists('vfsStream'))
View
5 src/tests/libraries/models/UrlTest.php
@@ -1,9 +1,4 @@
<?php
-$baseDir = dirname(dirname(dirname(dirname(__FILE__))));
-require_once sprintf('%s/tests/helpers/init.php', $baseDir);
-require_once sprintf('%s/libraries/models/Utility.php', $baseDir);
-require_once sprintf('%s/libraries/models/Url.php', $baseDir);
-
class UrlTest extends PHPUnit_Framework_TestCase
{
public function setUp()
View
20 src/tests/libraries/models/UserConfigTest.php
@@ -1,14 +1,4 @@
<?php
-$paths = (array)explode(PATH_SEPARATOR, ini_get('include_path'));
-foreach($paths as $path)
-{
- if(file_exists("{$path}/vfsStream/vfsStream.php"))
- require_once 'vfsStream/vfsStream.php';
-}
-$baseDir = dirname(dirname(dirname(dirname(__FILE__))));
-require_once sprintf('%s/tests/helpers/init.php', $baseDir);
-require_once sprintf('%s/libraries/models/UserConfig.php', $baseDir);
-
class UserConfigWrapper extends UserConfig
{
public function __construct($params = null)
@@ -24,6 +14,16 @@ public function inject($key, $val)
class UserConfigTest extends PHPUnit_Framework_TestCase
{
+ /**
+ * @var UserConfigWrapper
+ */
+ protected $userConfig;
+
+ /**
+ * @var string
+ */
+ protected $userConfigDir;
+
public function setUp()
{
if(class_exists('vfsStream'))
View
7 src/tests/libraries/models/UserTest.php
@@ -1,11 +1,8 @@
<?php
-$baseDir = dirname(dirname(dirname(dirname(__FILE__))));
-require_once sprintf('%s/tests/helpers/init.php', $baseDir);
-require_once sprintf('%s/libraries/models/BaseModel.php', $baseDir);
-require_once sprintf('%s/libraries/models/User.php', $baseDir);
-
class UserTest extends PHPUnit_Framework_TestCase
{
+ protected $user;
+
public function setUp()
{
// to test the write methods
View
12 src/tests/libraries/models/UtilityTest.php
@@ -1,8 +1,4 @@
<?php
-$baseDir = dirname(dirname(dirname(dirname(__FILE__))));
-require_once sprintf('%s/tests/helpers/init.php', $baseDir);
-require_once sprintf('%s/libraries/models/Utility.php', $baseDir);
-
class EpiRoute
{
const httpGet = 'GET';
@@ -58,6 +54,10 @@ public function testCallApisNone()
public function testDecrypt()
{
+ if (!extension_loaded('mcrypt')) {
+ $this->markTestSkipped("Test requires ext/mcrypt");
+ return;
+ }
$res = $this->utility->decrypt('string', 'secret', 'salt');
$this->assertEquals('lOHFbOH4AD+cpcRh1FcDte9DAapMDzqIHrwFz5DvxD4=', base64_encode($res), 'decrypted string is not correct');
}
@@ -79,6 +79,10 @@ public function testDiagnosticLine()
public function testEncrypt()
{
+ if (!extension_loaded('mcrypt')) {
+ $this->markTestSkipped("Test requires ext/mcrypt");
+ return;
+ }
$res = $this->utility->encrypt('string', 'secret', 'salt');
$this->assertEquals('b2ljK3AzRVE3Tk56N0FNd3dlUFRLMWRHaHh3QnVReG8wdFRHdktYczRFWT0=', base64_encode($res), 'encrypted string is not correct');
}
View
5 src/tests/libraries/models/WebhookTest.php
@@ -1,9 +1,4 @@
<?php
-$baseDir = dirname(dirname(dirname(dirname(__FILE__))));
-require_once sprintf('%s/tests/helpers/init.php', $baseDir);
-require_once sprintf('%s/libraries/models/BaseModel.php', $baseDir);
-require_once sprintf('%s/libraries/models/Webhook.php', $baseDir);
-
class WebhookTest extends PHPUnit_Framework_TestCase
{
public function setUp()
View
8 src/tests/phpunit.xml
@@ -0,0 +1,8 @@
+<phpunit bootstrap="./helpers/init.php" colors="true" debug="true">
+ <testsuite name="adapters">
+ <directory>./libraries/adapters/</directory>
+ </testsuite>
+ <testsuite name="models">
+ <directory>./libraries/models</directory>
+ </testsuite>
+</phpunit>

1 comment on commit 307fe4f

@jmathai

Much saner. Thanks!

Please sign in to comment.
Something went wrong with that request. Please try again.