Skip to content

Commit

Permalink
Bug #2/#3, Add SubClasses loader, add/fix unit tests
Browse files Browse the repository at this point in the history
* De-couple [iet:3634834], base-classes [iet:3634867]
* PSR-2 [iet:3633574], unit-tests [iet:3699309]
  • Loading branch information
nfreear committed Jun 25, 2015
1 parent 8e21b52 commit f76a07e
Show file tree
Hide file tree
Showing 9 changed files with 179 additions and 12 deletions.
3 changes: 0 additions & 3 deletions phpcs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@
<exclude-pattern>src/class.pdf2text.php</exclude-pattern>
<exclude-pattern>src/POParser.php</exclude-pattern>

<!-- Why does `phpcbf` fail on http.php ?? -->
<exclude-pattern>libraries/ht--tp.php</exclude-pattern>

<!-- Temporarily exclude all views and provider-libraries. -->
<exclude-pattern>*/views/*</exclude-pattern>
<exclude-pattern>src/providers/</exclude-pattern>
Expand Down
13 changes: 13 additions & 0 deletions src/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,19 @@ protected function _error($message, $code = 500, $from = null, $obj = null)
}
}

/** Get class name, without namespace, optionally trim content by RegExp.
http://stackoverflow.com/questions/19901850/how-do-i-get-an-objects-unqualified-short-class-
*/
protected function shortClass($regex = null, $replace = '', $is_parent = false)
{
$with_namespace = $is_parent ? get_parent_class($this) : get_class($this);
$short_name = substr($with_namespace, strrpos($with_namespace, '\\') + 1);
if ($regex) {
return preg_replace($regex, $replace, $short_name);
}
return $short_name;
}

protected function throw_no_framework_found_warning($function, $args = null)
{
if (static::$throw_no_framework) {
Expand Down
7 changes: 2 additions & 5 deletions src/Media_Player_Theme.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,8 @@ public function __construct()
parent::__construct();

// We use $this - an instance, not a class.
$this->name = strtolower(preg_replace('#_Theme$#i', '', get_class($this)));
$this->parent = strtolower(preg_replace('#_Theme$#i', '', get_parent_class($this)));
#$this->name = dirname(__FILE__);
#echo __FILE__;
#echo $this->parent;
$this->name = strtolower($this->shortClass('#_Theme$#i'));
$this->parent = strtolower($this->shortClass('#_Theme$#i', '', true));
}

/** Get the machine-readable name for the Scripts controller.
Expand Down
12 changes: 11 additions & 1 deletion src/Oembed_Provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public function call($url, $regex_matches);
*/
abstract class Oembed_Provider extends Base implements iService
{
protected static $hosts = array();

public $regex = ''; # array();
public $about = ''; # Human
Expand Down Expand Up @@ -67,14 +68,23 @@ public function __construct()
parent::__construct();

// We use $this - an instance, not a class.
$this->name = strtolower(preg_replace('#_serv$#i', '', get_class($this)));
$this->name = strtolower($this->shortClass('#_(Provider|serv)$#i'));

// Get the Google Analytics ID, if available.
if (function_exists('google_analytics_id')) {
$this->_google_analytics = \google_analytics_id($this->name);
}
}

/** Used by SubClasses.
*/
public function onAddClass(& $class_array)
{
foreach (static::$hosts as $host) {
$class_array[ $host ] = get_class($this);
}
return $class_array;
}

/** Get the machine-readable name for the Scripts controller.
* @return string
Expand Down
50 changes: 50 additions & 0 deletions src/OffsetIterator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php namespace IET_OU\Open_Media_Player;

/**
* OffsetIterator utility class.
*
* @copyright 2015 The Open University.
* @author N.D.Freear, 23 May 2015.
* @link https://gist.github.com/nfreear/72a3a62b8ac810ea4c49
* @link http://php.net/manual/en/class.iterator.php
*/

class OffsetIterator implements \Iterator
{

private $offset;
private $position;
private $array = array();

public function __construct($array, $offset = 0)
{
$this->position = $this->offset = $offset;
$this->array = $array;
}

public function rewind()
{
$this->position = $this->offset;
}

public function current()
{
#var_dump(__METHOD__, $this->position);
return $this->array[ $this->position ];
}

public function key()
{
return $this->position;
}

public function next()
{
++$this->position;
}

public function valid()
{
return isset($this->array[ $this->position ]);
}
}
61 changes: 61 additions & 0 deletions src/SubClasses.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php namespace IET_OU\Open_Media_Player;

/**
* SubClasses class.
*
* @copyright 2015 The Open University.
* @author N.D.Freear, 23 May 2015.
* @link https://gist.github.com/nfreear/72a3a62b8ac810ea4c49
*/

use \IET_OU\Open_Media_Player\OffsetIterator;

class SubClasses extends OffsetIterator
{
/**
* How many PHP "core" classes should we skip? (performance)
*
* $ php -r 'echo count(get_declared_classes());'
* Result: 139, Mac/PHP 5.4.38; 125, Ar**s/PHP 5.3.3; 120, Pan**s/RHE 6/PHP 5.5.26;
*/
const PHP_CORE_OFFSET = 120;

const ON_ADD_FN = 'onAddClass';

public function __construct($offset = self::PHP_CORE_OFFSET)
{
parent::__construct(get_declared_classes(), $offset);
}

/**
* @param string $base_class A parent class or interface.
* @param string $callback Optional static function to call on the class, providing the key in the results.
* @return array Array of result classes, optionally keyed.
*/
public function match($base_class, $callback = null, $test_instantiable = true)
{
$results = array();
foreach ($this as $class) {
if (is_subclass_of($class, $base_class)) {
//or $base_class == $class) {
$reflect = new \ReflectionClass($class);
if ($reflect->isInstantiable()) {
if ($callback) {
$obj = new $class;
$obj->{ $callback }($results);
//Was: $results[ $class::{ $callback }() ] = $class;
} else {
$results[] = $class;
}
}
}
}
return $results;
}


public function get_oembed_providers()
{
return $this->match('IET_OU\Open_Media_Player\Oembed_Provider', self::ON_ADD_FN);
}
}
1 change: 1 addition & 0 deletions src/providers/Oupodcast_Provider.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php namespace IET_OU\Open_Media_Player;

/**
* OU Media Player/ OU Podcast oEmbed service provider.
*
Expand Down
6 changes: 3 additions & 3 deletions tests/Oembed_Provider_Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use \IET_OU\Open_Media_Player\Oembed_Provider;
use \IET_OU\Open_Media_Player\Oupodcast_Provider;

class Mock_Provider extends Oembed_Provider {
class Mock_Service_Provider extends Oembed_Provider {

public function call($url, $regex_matches) {
}
Expand All @@ -22,13 +22,13 @@ public function setup()
public function testName()
{
// Arrange
$mock_provider = new Mock_Provider();
$mock_provider = new Mock_Service_Provider();

// Act
$mock_name = $mock_provider->getName();

// Assert
$this->assertEquals('mock_provider', $mock_name);
$this->assertEquals('mock_service', $mock_name);
}

public function testPodcast()
Expand Down
38 changes: 38 additions & 0 deletions tests/SubClasses_Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php namespace IET_OU\Open_Media_Player\Test;

use \IET_OU\Open_Media_Player\SubClasses;
use \IET_OU\Open_Media_Player\Oembed_Provider;

class Mock_Ex_Provider extends Oembed_Provider {

protected static $hosts = array( 'example.org', 'example.com' );

public function call($url, $regex_matches) {
}
}

class SubClasses_Test extends \PHPUnit_Framework_TestCase
{
// ...

public function setup()
{
\IET_OU\Open_Media_Player\Base::$throw_no_framework = false;
}

public function testOembedProviders()
{
// Arrange
$sub = new SubClasses();

// Act
$providers = $sub->get_oembed_providers();

var_dump($providers);

// Assert
$this->assertEquals(2, count($providers));
}

}

0 comments on commit f76a07e

Please sign in to comment.