Skip to content

Commit

Permalink
Bug #1/#2/#3, Add CodeSniffer, phpunit, namespaces [iet:3634834][iet:…
Browse files Browse the repository at this point in the history
…3634867]

* phpcs.xml; phpunit.xml.dist; Initial unit tests.
  • Loading branch information
nfreear committed Jun 24, 2015
1 parent 6c2b9c5 commit 8e21b52
Show file tree
Hide file tree
Showing 13 changed files with 330 additions and 41 deletions.
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
},
"autoload": {
"psr-4": {
"IET_OU\\Open_Media_Player\\": "src"
"IET_OU\\Open_Media_Player\\": [ "src", "src/providers" ]
}
},
"autoload-dev": {
Expand All @@ -42,11 +42,11 @@
"#phpcs --standard=./phpcs.xml -n --encoding=utf-8 --extensions=php src/*",
"phpunit"
],
"test-x": [
"phpcs --standard=PSR2 -n --encoding=utf-8 --extensions=php src/*"
"test-cs": [
"phpcs --standard=./phpcs.xml -n --encoding=utf-8 --extensions=php src/*"
],
"fix": [
"phpcbf --standard=./phpcs.xml -n -l --encoding=utf-8 --extensions=php src/X*"
"phpcbf --standard=./phpcs.xml -n -l --encoding=utf-8 --extensions=php src/pro*"
]
},
"config": {
Expand Down
101 changes: 101 additions & 0 deletions phpcs.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?xml version="1.0"?>
<ruleset name="mediaplayer-open-ac-uk">
<description>OU Media Player coding standard - PSR2, almost!</description>

<!--
Stay as close to PSR-2 as possible, but account for CodeIgniter conventions.
http://edorian.github.io/php-coding-standard-generator/#phpcs
-->

<exclude-pattern>\.git</exclude-pattern>
<exclude-pattern>/application/assets/</exclude-pattern>
<exclude-pattern>/application/cli/</exclude-pattern>
<exclude-pattern>/application/config/</exclude-pattern>
<exclude-pattern>/application/engines/</exclude-pattern>
<exclude-pattern>/application/errors/</exclude-pattern>
<exclude-pattern>/application/language/</exclude-pattern>
<exclude-pattern>/system/</exclude-pattern>
<exclude-pattern>/vendor/</exclude-pattern>
<exclude-pattern>*/*.html</exclude-pattern>
<exclude-pattern>*/*BAK*</exclude-pattern>

<!-- Exclude third-party libraries. -->
<exclude-pattern>src/FirePHPCore</exclude-pattern>
<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>


<rule ref="PSR2">
<exclude name="PSR2.Methods.MethodDeclaration"/>
<!--<exclude name="PSR2.Classes.PropertyDeclaration"/>-->
</rule>

<rule ref="PSR1">
<exclude name="PSR1.Methods.CamelCapsMethodName"/>

<!--
For the moment, don't require namespaces.
-->
<!--<exclude name="PSR1.Classes.ClassDeclaration"/>-->

<!--<exclude name="PSR1.Files.SideEffects"/>-->
</rule>

<rule ref="Squiz.Classes">
<exclude name="Squiz.Classes.ValidClassName"/>
</rule>

<!-- "%s name doesn't match filename; expected"-->
<rule ref="Squiz.Classes">
<exclude-pattern>/application/controllers/</exclude-pattern>

<exclude name="Squiz.Classes.ValidClassName"/>
<!--<exclude name="Squiz.Classes.ClassFileName"/>-->
</rule>

<!--
Accept class names like "MY_Controller".
-->
<rule ref="PEAR.NamingConventions.ValidClassName"/>


<!--<rule ref="Squiz.Arrays.ArrayBracketSpacing"/>-->

<!--<rule ref="Squiz.Classes.LowercaseClassKeywords"/>-->

<rule ref="Generic.CodeAnalysis.ForLoopShouldBeWhileLoop"/>
<rule ref="Generic.CodeAnalysis.ForLoopWithTestFunctionCall"/>
<rule ref="Generic.CodeAnalysis.JumbledIncrementer"/>
<rule ref="Generic.CodeAnalysis.UnconditionalIfStatement"/>
<rule ref="Generic.CodeAnalysis.UnnecessaryFinalModifier"/>

<rule ref="Generic.Commenting.Todo"/>

<rule ref="Generic.Files.LineEndings"/>

<rule ref="Generic.PHP.DeprecatedFunctions"/>
<rule ref="Squiz.PHP.DisallowSizeFunctionsInLoops"/>
<rule ref="Squiz.PHP.Eval"/>
<rule ref="Generic.PHP.ForbiddenFunctions"/>
<rule ref="Squiz.PHP.NonExecutableCode"/>
<!--<rule ref="Generic.PHP.NoSilencedErrors"/>-->

<rule ref="Squiz.Scope.MemberVarScope"/>
<rule ref="Squiz.Scope.StaticThisUsage"/>

<rule ref="Squiz.WhiteSpace.CastSpacing"/>
<rule ref="Squiz.WhiteSpace.LanguageConstructSpacing"/>
<rule ref="Squiz.WhiteSpace.LogicalOperatorSpacing"/>
<rule ref="Squiz.WhiteSpace.SemicolonSpacing"/>
<rule ref="Squiz.WhiteSpace.SuperfluousWhitespace"/>

<!-- All rules in ./Sniffs are included automatically -->
</ruleset>
25 changes: 25 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0"?>
<phpunit
bootstrap="./vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
verbose="true"
stopOnFailure="false"
processIsolation="false"
backupGlobals="false"
syntaxCheck="true"
>
<testsuite name="IET_OU\Open_Media_Player core tests">
<directory>./tests</directory>
</testsuite>
<filter>
<whitelist addUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./src</directory>
</whitelist>
</filter>
<logging>
<log type="coverage-clover" target="./coverage.clover"/>
</logging>
</phpunit>
79 changes: 79 additions & 0 deletions src/Base.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php namespace IET_OU\Open_Media_Player;

/**
* THE single base class, for extensible components of Open Media Player.
*
* @copyright Copyright 2011-2015 The Open University.
* @author N.D.Freear, 18 June 2015.
* @link http://www.codeigniter.com/userguide2/
*/

abstract class Base
{
public static $throw_no_framework = true;

protected $CI;

public function __construct()
{
// Is CodeIgniter present?
if (defined('CI_VERSION') && function_exists('get_instance')) {
$ci =& \get_instance();

if ($ci instanceof IET_OU\Open_Media_Player\MY_Controller) {
$this->CI = $ci;
}
}
if (!$this->CI) {
$this->throw_no_framework_found_warning(__METHOD__, func_get_args());
}
}

protected function get_param($key, $default = null, $filter = FILTER_SANITIZE_STRING, $options = null)
{
$value = filter_input(INPUT_GET, $key, $filter, $options);
return $value ? $value : $default;
// Alt: $this->CI->input->get();
}

/** Get a configuration item, set a default if it doesn't exist.
*/
protected function config_item($item, $default = null, $index = '')
{
if ($this->CI) {
$value = $this->CI->config->item($item, $index);
return $value ? $value : $default;
} else {
$this->throw_no_framework_found_warning(__METHOD__, func_get_args());
}
}

protected function _log($level = 'error', $message = 'unknown', $php_error = false)
{
if ($this->CI) {
return $this->CI->_log($level, $message, $php_error);
} else {
$this->throw_no_framework_found_warning(__METHOD__, func_get_args());
}
}

/** Display an error message and exit?
*/
protected function _error($message, $code = 500, $from = null, $obj = null)
{
if ($this->CI) {
return $this->CI->_error($mesage, $code, $from, $obj);
} else {
$this->throw_no_framework_found_warning(__METHOD__, func_get_args());
}
}

protected function throw_no_framework_found_warning($function, $args = null)
{
if (static::$throw_no_framework) {
throw new \Exception('No CodeIgniter framework found: '. $function);
} else {
echo "Warning: no CodeIgniter framework found: $function\n";
}
}
}
9 changes: 6 additions & 3 deletions src/Gitlib.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php defined('BASEPATH') or exit('No direct script access allowed');
<?php namespace IET_OU\Open_Media_Player;

/**
* Gitlib: a simple Git library, to get changeset hashes and information.
*
Expand All @@ -8,7 +9,9 @@
* @author N.D.Freear, 2012-04-25.
*/

class Gitlib
use \IET_OU\Open_Media_Player\Base;

class Gitlib extends Base
{

const GIT_DESCRIBE_REGEX = '/(?P<major>\d+)\.(?P<minor>\d+)(?P<id>-[\w\.]+)?-(?P<patch>\d+)-(?P<hash>g.+)/';
Expand Down Expand Up @@ -113,7 +116,7 @@ protected function _exec($cmd)
}

//Security?
$git_path = $this->CI->config->item('git_path');
$git_path = $this->config_item('git_path');


if (! $git_path) {
Expand Down
27 changes: 11 additions & 16 deletions src/http.php → src/Http.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php defined('BASEPATH') or exit('No direct script access allowed');
<?php namespace IET_OU\Open_Media_Player;

/**
* HTTP request library.
* Code from base_service.php, using cURL.
Expand All @@ -13,17 +14,11 @@
*/
defined('CURLOPT_NOPROXY') ? null : define('CURLOPT_NOPROXY', 10177);

use \IET_OU\Open_Media_Player\Base;

class Http
class Http extends Base
{

protected $CI;

public function __construct()
{
$this->CI =& get_instance();
}


public function request($url, $spoof = true, $options = array())
{
Expand Down Expand Up @@ -61,9 +56,9 @@ protected function _prepare_request($url, $spoof, &$options)
// Bug #1334, Proxy mode to fix VLE caption redirects (Timedtext controller).
$options[ 'cookie' ] = null;
if (isset($options['proxy_cookies'])) {
$cookie_names = $this->CI->config->item('httplib_proxy_cookies');
$cookie_names = $this->config_item('httplib_proxy_cookies');
if (! is_array($cookie_names)) {
$this->CI->_error('Array expected for $config[httplib_proxy_cookies]', 400);
$this->_error('Array expected for $config[httplib_proxy_cookies]', 400);
}

$cookies = '';
Expand All @@ -74,7 +69,7 @@ protected function _prepare_request($url, $spoof, &$options)
}

// Bug #4, Optionally add cookies for every request to a host/ domain.
$cookie_r = $this->CI->config->item('http_cookie');
$cookie_r = $this->config_item('http_cookie');
if (is_array($cookie_r)) {
foreach ($cookie_r as $domain => $cookie) {
if (false !== strpos($url, $domain)) {
Expand All @@ -92,8 +87,8 @@ protected function _prepare_request($url, $spoof, &$options)

// Merge the default options.
$options += array(
'proxy' => $this->CI->config->item('http_proxy'),
'no_proxy' => $this->CI->config->item('http_no_proxy'),
'proxy' => $this->config_item('http_proxy'),
'no_proxy' => $this->config_item('http_no_proxy'),
'headers' => array(),
'method' => 'GET',
'data' => null,
Expand All @@ -113,7 +108,7 @@ protected function _prepare_request($url, $spoof, &$options)


/** Perform the HTTP request using cURL.
*/
*/
protected function _http_request_curl($url, $spoof, $options, $result)
{
if (!function_exists('curl_init')) {
Expand Down Expand Up @@ -177,7 +172,7 @@ protected function _http_request_curl($url, $spoof, $options, $result)
}
if ($errno = curl_errno($h_curl)) {
//Error. Quietly log?
$this->CI->_log('error', "cURL $errno, ".curl_error($h_curl)." GET $url");
$this->_log('error', "cURL $errno, ".curl_error($h_curl)." GET $url");
#$this->CI->firephp->fb("cURL $errno", "cURL error", "ERROR");
$result->http_code = "500." . $errno;
$result->curl_errno = $errno;
Expand Down
11 changes: 6 additions & 5 deletions src/player_theme.php → src/Media_Player_Theme.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
<?php defined('BASEPATH') or exit('No direct script access allowed');
<?php namespace IET_OU\Open_Media_Player;

/**
* An abstract theme from which to extend OU Media Player themes or skins.
*
* @copyright Copyright 2012 The Open University.
* @author N.D.Freear, 20 March 2012.
*/

use \IET_OU\Open_Media_Player\Base;

// Based on (private): https://gist.github.com/08e20a98136289bbd7ec
abstract class Player_Theme
abstract class Media_Player_Theme extends Base
{

public $name; // The short theme name, used internally (auto-generated from class name).
Expand All @@ -28,8 +31,6 @@ abstract class Player_Theme
protected $controls_height; // Pixels.
protected $controls_overlap; // Boolean.

protected $CI;

const ENGINE_PATH = 'engines/';
const THEME_PATH = 'themes/';

Expand All @@ -38,7 +39,7 @@ abstract class Player_Theme
*/
public function __construct()
{
$this->CI =& get_instance();
parent::__construct();

// We use $this - an instance, not a class.
$this->name = strtolower(preg_replace('#_Theme$#i', '', get_class($this)));
Expand Down
Loading

0 comments on commit 8e21b52

Please sign in to comment.