phpcs and docs

This commit is contained in:
Ade Attwood 2017-09-17 20:50:44 +01:00
parent 074ca1ee4b
commit 5ee2a60fc1
19 changed files with 230 additions and 192 deletions

View file

@ -3,7 +3,13 @@
<description>Yii addon coding standards</description>
<file>./src</file>
<file>./tests</file>
<exclude-pattern>*/_output/*</exclude-pattern>
<exclude-pattern>*/_data/*</exclude-pattern>
<exclude-pattern>**.*.css</exclude-pattern>
<exclude-pattern>**.*.js</exclude-pattern>
<arg name="colors"/>
<rule ref="PSR1"/>
@ -14,12 +20,26 @@
<rule ref="Squiz.Classes.ValidClassName.NotCamelCaps">
<exclude-pattern>*/tests/*</exclude-pattern>
<exclude-pattern>*/migrations/*</exclude-pattern>
</rule>
<rule ref="Generic.CodeAnalysis.UnusedFunctionParameter.Found">
<exclude-pattern>*/tests/*</exclude-pattern>
</rule>
<rule ref="PSR1.Classes.ClassDeclaration.MissingNamespace">
<exclude-pattern>*/tests/*</exclude-pattern>
<exclude-pattern>*/migrations/*</exclude-pattern>
</rule>
<rule ref="PEAR.Commenting.FunctionComment.MissingParamTag">
<exclude-pattern>*/tests/*</exclude-pattern>
</rule>
<rule ref="PSR2.Methods.MethodDeclaration.Underscore">
<exclude-pattern>*/tests/*</exclude-pattern>
</rule>
<rule ref="PEAR.Commenting.FunctionComment.Missing">
<exclude-pattern>*/tests/*</exclude-pattern>
</rule>
<rule ref="Generic.Commenting.Todo.CommentFound">
@ -67,6 +87,7 @@
<rule ref="PEAR.Commenting">
<exclude name="PEAR.Commenting.FileComment"/>
<exclude name="PEAR.Commenting.ClassComment.InvalidPackage"/>
</rule>
<rule ref="PEAR.Commenting.FileComment.MissingCategoryTag">

View file

@ -34,7 +34,7 @@ class ActiveRecordCollection extends \yiiaddon\helpers\ArrayObject
*/
public function map($key, $value)
{
return new ArrayObject(ArrayHelper::map((array)$this, $key, $value));
return new ArrayObject(ArrayHelper::map((array) $this, $key, $value));
}
}

View file

@ -19,7 +19,7 @@ class ActiveQuery extends \yii\db\ActiveQuery
/**
* Overrides the Yii method to return an ActiveRecordCollection instead of an array
*
* @param mixed $db
* @param mixed $db The DB connection used to create the DB command.
*
* @return ActiveRecordCollection
*/

View file

@ -39,7 +39,7 @@ class ActiveRecord extends \yii\db\ActiveRecord
*/
public function parseAttribute($attribute, $defalts = [])
{
return ArrayHelper::parseAttribute($this, $attribute, $defalts);
return ArrayHelper::parseAttribute($this, $attribute, $defalts);
}
/**

View file

@ -35,7 +35,7 @@ trait Rankable
{
$query = self::find();
if ( $this->rankGoup ) {
if ($this->rankGoup) {
$query->where([$this->rankGroup => $this->{$this->rankGroup}]);
}
@ -48,31 +48,31 @@ trait Rankable
$group = $this->rankGoup;
$old_rank = $this->{ $rank_filed };
if ( $new_rank == $old_rank ) {
if ($new_rank == $old_rank) {
return true;
}
$value = '';
if ( $new_rank > $old_rank ) {
$value = $rank_filed . ' - 1';
$where = $rank_filed . '>= :rank1 AND ' . $rank_filed . ' <= :rank2';
if ($new_rank > $old_rank) {
$value = $rank_filed.' - 1';
$where = $rank_filed.'>= :rank1 AND '.$rank_filed.' <= :rank2';
} else {
$value = $rank_filed . ' + 1';
$where = $rank_filed . ' <= :rank1 AND ' . $rank_filed . ' >= :rank2';
$value = $rank_filed.' + 1';
$where = $rank_filed.' <= :rank1 AND '.$rank_filed.' >= :rank2';
}
if ( $group ) {
$where .= ' AND ' . $group . ' = ' . $this->{ $group };
if ($group) {
$where .= ' AND '.$group.' = '.$this->{ $group };
}
$query = Yii::$app->db->createCommand()
->update( self::tableName(), [ $rank_filed => new Expression( $value ) ], $where )
->bindValue( ':rank1', $old_rank )
->bindValue( ':rank2', $new_rank )
->update(self::tableName(), [ $rank_filed => new Expression($value) ], $where)
->bindValue(':rank1', $old_rank)
->bindValue(':rank2', $new_rank)
->execute();
$this->updateAttributes( [ 'rank' => $new_rank ] );
$this->updateAttributes([ 'rank' => $new_rank ]);
return true;
}

View file

@ -8,7 +8,7 @@ use yii\web\Response;
/**
* Minifies the html output of the page
*
*
* @category PHP
* @package adeattwood\yii-addon
* @author Ade Attwood <hello@adeattwood.co.uk>
@ -61,23 +61,24 @@ class PageMin extends \yii\base\ActionFilter
*/
public function minifyHtml( $input )
{
if(trim($input) === "") return $input;
if(trim($input) === '') { return $input;
}
$input = preg_replace_callback('#<([^\/\s<>!]+)(?:\s+([^<>]*?)\s*|\s*)(\/?)>#s', function($matches) {
return '<' . $matches[1] . preg_replace('#([^\s=]+)(\=([\'"]?)(.*?)\3)?(\s+|$)#s', ' $1$2', $matches[2]) . $matches[3] . '>';
}, str_replace("\r", "", $input));
return '<'.$matches[1].preg_replace('#([^\s=]+)(\=([\'"]?)(.*?)\3)?(\s+|$)#s', ' $1$2', $matches[2]).$matches[3].'>';
}, str_replace("\r", '', $input));
if(strpos($input, ' style=') !== false) {
$input = preg_replace_callback('#<([^<]+?)\s+style=([\'"])(.*?)\2(?=[\/\s>])#s', function($matches) {
return '<' . $matches[1] . ' style=' . $matches[2] . $this->minifyCss($matches[3]) . $matches[2];
return '<'.$matches[1].' style='.$matches[2].$this->minifyCss($matches[3]).$matches[2];
}, $input);
}
if(strpos($input, '</style>') !== false) {
$input = preg_replace_callback('#<style(.*?)>(.*?)</style>#is', function($matches) {
return '<style' . $matches[1] .'>'. $this->minifyCss($matches[2]) . '</style>';
return '<style'.$matches[1].'>'.$this->minifyCss($matches[2]).'</style>';
}, $input);
}
if(strpos($input, '</script>') !== false) {
$input = preg_replace_callback('#<script(.*?)>(.*?)</script>#is', function($matches) {
return '<script' . $matches[1] .'>'. $this->minifyJs($matches[2]) . '</script>';
return '<script'.$matches[1].'>'.$this->minifyJs($matches[2]).'</script>';
}, $input);
}
return preg_replace(
@ -113,7 +114,7 @@ class PageMin extends \yii\base\ActionFilter
* Minifies css strings inside the html
*
* @param string $input The input html
*
*
* @return string The output html
*/
public function minifyCss($input)
@ -157,7 +158,7 @@ class PageMin extends \yii\base\ActionFilter
* Minifies js strings inside the html
*
* @param string $input The input html
*
*
* @return string The output html
*/
public function minifyJs($input)

View file

@ -19,6 +19,7 @@ use yiiaddon\helpers\Enum;
*/
class ArrayHelper extends \yii\helpers\ArrayHelper
{
/**
* Creates a array of numbers
*
@ -43,8 +44,8 @@ class ArrayHelper extends \yii\helpers\ArrayHelper
public static function numberArray( $max, $min = 0, $step = 1 )
{
$array = [];
for( $i = $min; $i <= $max; $i += $step ) {
$array[ $i ] = $i;
for($i = $min; $i <= $max; $i += $step) {
$array[$i] = $i;
}
return $array;
}
@ -66,22 +67,22 @@ class ArrayHelper extends \yii\helpers\ArrayHelper
*/
public static function parseAttribute( $model, $attribute, $defalts = [] )
{
if ( !preg_match( '/^([^:]+)(:(\w*))?(:(.*))?$/', $attribute, $matches ) ) {
throw new InvalidConfigException( 'The column must be specified in the format of "attribute", "attribute:format" or "attribute:format:label"' );
if (!preg_match('/^([^:]+)(:(\w*))?(:(.*))?$/', $attribute, $matches)) {
throw new InvalidConfigException('The column must be specified in the format of "attribute", "attribute:format" or "attribute:format:label"');
}
$rawValue = self::getValue( $model, $matches[ 1 ] );
$rawValue = self::getValue($model, $matches[1]);
$format = isset( $matches[ 3 ] ) ? $matches[ 3 ] : self::remove($defalts, 'format', 'text');
$value = Enum::isEmpty( $rawValue ) ? self::remove($defalts, 'value', '( NOT SET )') : Yii::$app->formatter->format( $rawValue, $format );
$lable = isset( $matches[ 5 ] ) ? $matches[ 5 ] : $model->getAttributeLabel( $matches[ 1 ] );
$format = isset($matches[3]) ? $matches[3] : self::remove($defalts, 'format', 'text');
$value = Enum::isEmpty($rawValue) ? self::remove($defalts, 'value', '( NOT SET )') : Yii::$app->formatter->format($rawValue, $format);
$lable = isset($matches[5]) ? $matches[5] : $model->getAttributeLabel($matches[1]);
return new ArrayObject( [
return new ArrayObject([
'format' => $format,
'value' => $value,
'lable' => $lable,
'raw' => $rawValue
] );
]);
}
}

View file

@ -30,24 +30,24 @@ class ArrayObject extends \ArrayObject
*/
public function __construct( $array = [], $flags = null, $iterator_class = 'ArrayIterator' )
{
if( $flags === null ) {
if($flags === null) {
$flags = self::ARRAY_AS_PROPS;
}
parent::__construct( $array, $flags, $iterator_class );
parent::__construct($array, $flags, $iterator_class);
}
/**
* Test that the ArrayObject contains an item
*
* @param $item The item to test
* @param mixed $item The item to test
*
* @return bool
*/
public function contains( $item )
{
$flipped = array_flip( ( array )$this );
return isset( $flipped[ $item ] );
$flipped = array_flip(( array ) $this);
return isset($flipped[$item]);
}
/**
@ -59,12 +59,12 @@ class ArrayObject extends \ArrayObject
*/
public function randomValue( $count = 1 )
{
$arr = ( array )$this;
$arr = ( array ) $this;
shuffle( $arr );
shuffle($arr);
$r = array();
for ( $i = 0; $i < $count; $i++ ) {
for ($i = 0; $i < $count; $i++) {
$r[] = $arr[$i];
}
return $count == 1 ? $r[0] : new self($r);
@ -74,7 +74,7 @@ class ArrayObject extends \ArrayObject
* Adds an item to the start of the object
*
* @param mixed $value The item to prepend
*
*
* @return void
*/
public function prepend($value)
@ -82,7 +82,7 @@ class ArrayObject extends \ArrayObject
$array = $this->getArrayCopy();
if (is_string($value)) {
$value = (array)$value;
$value = (array) $value;
}
if (is_object($value)) {
@ -100,7 +100,7 @@ class ArrayObject extends \ArrayObject
*/
public function combine($array)
{
$this->exchangeArray(array_merge((array)$this->getArrayCopy(), $array));
$this->exchangeArray(array_merge((array) $this->getArrayCopy(), $array));
}
/**
@ -142,8 +142,8 @@ class ArrayObject extends \ArrayObject
*/
public function chunks($count)
{
$out = [];
$array = array_chunk((array)$this, $count);
$out = [];
$array = array_chunk((array) $this, $count);
foreach ($array as $chunk) {
$out[] = new self($chunk);
@ -159,7 +159,7 @@ class ArrayObject extends \ArrayObject
*/
public function toJson()
{
return \yii\helpers\Json::encode((array)$this);
return \yii\helpers\Json::encode((array) $this);
}
/**
@ -177,65 +177,64 @@ class ArrayObject extends \ArrayObject
*/
public function toTable( $transpose = false, $recursive = false, $typeHint = true, $tableOptions = [ 'class' => 'table table-bordered table-striped' ], $keyOptions = [], $valueOptions = [ 'style' => 'cursor: default; border-bottom: 1px #aaa dashed;' ], $null = '<span class="not-set">(not set)</span>' )
{
if ( empty( ( array )$this ) ) {
if (empty(( array ) $this)) {
return false;
}
$array = ( array )$this;
$array = ( array ) $this;
// Start the table
$table = Html::beginTag( 'table', $tableOptions ) . "\n";
$table = Html::beginTag('table', $tableOptions)."\n";
// The header
$table .= "\t<tr>";
if ( $transpose ) {
foreach ( $this as $key => $value ) {
if ( $typeHint ) {
$valueOptions['title'] = Enum::getType( strtoupper( $value ) );
if ($transpose) {
foreach ($this as $key => $value) {
if ($typeHint) {
$valueOptions['title'] = Enum::getType(strtoupper($value));
}
if ( is_array( $value ) ) {
$value = '<pre>' . print_r( $value, true ) . '</pre>';
if (is_array($value)) {
$value = '<pre>'.print_r($value, true).'</pre>';
} else {
$value = Html::tag( 'span', $value, $valueOptions );
$value = Html::tag('span', $value, $valueOptions);
}
$table .= "\t\t<th>" . Html::tag( 'span', $key, $keyOptions ) . "</th>" .
"<td>" . $value . "</td>\n\t</tr>\n";
$table .= "\t\t<th>".Html::tag('span', $key, $keyOptions).'</th>'.'<td>'.$value."</td>\n\t</tr>\n";
}
$table .= "</table>";
$table .= '</table>';
return $table;
}
if ( !isset( $array[0] ) || !is_array( $array[0] ) ) {
if (!isset($array[0]) || !is_array($array[0])) {
$array = array( $array );
}
// Take the keys from the first row as the headings
foreach ( array_keys( $array[0] ) as $heading ) {
$table .= '<th>' . Html::tag( 'span', $heading, $keyOptions ) . '</th>';
foreach (array_keys($array[0]) as $heading) {
$table .= '<th>'.Html::tag('span', $heading, $keyOptions).'</th>';
}
$table .= "</tr>\n";
// The body
foreach ( $array as $row ) {
foreach ($array as $row) {
$table .= "\t<tr>";
foreach ( $row as $cell ) {
foreach ($row as $cell) {
$table .= '<td>';
// Cast objects
if ( is_object( $cell ) ) {
$cell = (array)$cell;
if (is_object($cell)) {
$cell = (array) $cell;
}
if ( $recursive === true && is_array( $cell ) && !empty( $cell ) ) {
if ($recursive === true && is_array($cell) && !empty($cell)) {
// Recursive mode
$table .= "\n" . static::array2table( $cell, true, true ) . "\n";
$table .= "\n".static::array2table($cell, true, true)."\n";
} else {
if ( !is_null( $cell ) && is_bool( $cell ) ) {
$val = $cell ? 'true' : 'false';
if ($cell !== null && is_bool($cell)) {
$val = $cell ? 'true' : 'false';
$type = 'boolean';
} else {
$chk = ( strlen( $cell ) > 0 );
$type = $chk ? Enum::getType( $cell ) : 'null';
$val = $chk ? htmlspecialchars( (string)$cell ) : $null;
$chk = ( strlen($cell) > 0 );
$type = $chk ? Enum::getType($cell) : 'null';
$val = $chk ? htmlspecialchars((string) $cell) : $null;
}
if ( $typeHint ) {
if ($typeHint) {
$valueOptions['title'] = $type;
}
$table .= Html::tag( 'span', $val, $valueOptions );
$table .= Html::tag('span', $val, $valueOptions);
}
$table .= '</td>';
}
@ -244,4 +243,5 @@ class ArrayObject extends \ArrayObject
$table .= '</table>';
return $table;
}
}

View file

@ -38,22 +38,22 @@ class Dumper
);
public static function dump( $var ) {
VarDumper::setHandler( function ( $var ) {
VarDumper::setHandler(function ( $var ) {
$cloner = new VarCloner();
$dumper = 'cli' === PHP_SAPI ? new CliDumper() : new HtmlDumper();
$dumper->setStyles( self::$styles );
$dumper->setDisplayOptions( self::$displayOptions );
$dumper->setStyles(self::$styles);
$dumper->setDisplayOptions(self::$displayOptions);
$dumper->dump( $cloner->cloneVar( $var ) );
} );
$dumper->dump($cloner->cloneVar($var));
});
dump( $var );
dump($var);
}
public static function dDump( $var )
{
self::dump( $var );
self::dump($var);
die;
}
@ -61,89 +61,90 @@ class Dumper
{
$dataArray = [];
$class = new \ReflectionClass( $var );
$class = new \ReflectionClass($var);
$dataArray[ 'name' ] = $class->name;
$dataArray['name'] = $class->name;
$methods = $class->getMethods();
$dataArray[ 'constants'] = ( array )$class->getConstants();
$methods = $class->getMethods();
$dataArray['constants'] = ( array ) $class->getConstants();
foreach ( $class->getProperties() as $propertie ) {
foreach ($class->getProperties() as $propertie) {
$prop = '';
if ( $propertie->isPublic() ) {
$prop .= "public";
} elseif ( $propertie->isPrivate() ) {
$prop .= "private";
} elseif ( $propertie->isProtected() ) {
$prop .= "protected";
if ($propertie->isPublic()) {
$prop .= 'public';
} elseif ($propertie->isPrivate()) {
$prop .= 'private';
} elseif ($propertie->isProtected()) {
$prop .= 'protected';
}
if ( $propertie->isStatic() ) {
$prop .= " static";
if ($propertie->isStatic()) {
$prop .= ' static';
}
$prop .= " \${$propertie->name}";
$dataArray[ 'properties' ][] = $prop;
$dataArray['properties'][] = $prop;
}
foreach ( $methods as $method ) {
foreach ($methods as $method) {
$prop = '';
if ( $method->isPublic() ) {
$prop .= "public";
} elseif ( $method->isPrivate() ) {
$prop .= "private";
} elseif ( $method->isProtected() ) {
$prop .= "protected";
if ($method->isPublic()) {
$prop .= 'public';
} elseif ($method->isPrivate()) {
$prop .= 'private';
} elseif ($method->isProtected()) {
$prop .= 'protected';
}
$prop .= " {$method->name}( ";
$params = "";
foreach ( $method->getParameters() as $parameter ) {
$params = '';
foreach ($method->getParameters() as $parameter) {
$params .= "\${$parameter->name}";
if ( $parameter->isDefaultValueAvailable() ) {
if ($parameter->isDefaultValueAvailable()) {
$value = $parameter->getDefaultValue();
if ( is_array( $value ) ) {
$value = "[]";
} elseif ( is_bool( $value ) ) {
$value = $value ? "true" : "false";
if (is_array($value)) {
$value = '[]';
} elseif (is_bool($value)) {
$value = $value ? 'true' : 'false';
}
$params .= "= {$value}";
// self::dDump( $parameter->getDefaultValue() );
}
$params .= ", ";
$params .= ', ';
}
$params = rtrim( $params, ", " );
$prop .= "{$params} )";
$params = rtrim($params, ', ');
$prop .= "{$params} )";
// $prop .= " : {$method->getReturnType()}";
$dataArray[ 'methords' ][ $prop ] = [
"file" => $method->getFileName(),
"line" => $method->getStartLine(),
$dataArray['methords'][$prop] = [
'file' => $method->getFileName(),
'line' => $method->getStartLine(),
'fileLine' => "{$method->getFileName()}:{$method->getStartLine()}",
"doc" => $method->getDocComment()
'doc' => $method->getDocComment()
];
}
sort( $dataArray[ 'properties' ] );
ksort( $dataArray[ 'constants' ] );
ksort( $dataArray[ 'methords' ] );
self::dDump( $dataArray );
sort($dataArray['properties']);
ksort($dataArray['constants']);
ksort($dataArray['methords']);
self::dDump($dataArray);
}
public static function dDumpInfo( $var )
{
self::dumpInfo( $var );
self::dumpInfo($var);
die;
}
}

View file

@ -48,34 +48,34 @@ class Enum extends \yii\base\Object
*/
public static function getType( $var )
{
if (is_null($var)) {
if ($var === null) {
return self::TYPE_NULL;
}
if (is_array( $var )) {
if (is_array($var)) {
return self::TYPE_ARRAY;
}
if (is_bool( $var )) {
if (is_bool($var)) {
return self::TYPE_BOOL;
}
if (is_float( $var )
if (is_float($var)
|| (!is_object($var) &&
is_numeric( str_replace( ',', '', $var ) ) && strpos( $var, '.' ) > 0 && is_float( ( float )str_replace( ',', '', $var ) ))
is_numeric(str_replace(',', '', $var)) && strpos($var, '.') > 0 && is_float(( float ) str_replace(',', '', $var)))
) {
return self::TYPE_FLOAT;
}
if (is_int( $var ) || ( is_numeric( $var ) && is_int( ( int )$var ) )) {
if (is_int($var) || ( is_numeric($var) && is_int(( int ) $var) )) {
return self::TYPE_INT;
}
if (is_scalar( $var ) && strtotime( $var ) !== false) {
if (is_scalar($var) && strtotime($var) !== false) {
return self::TYPE_DATETIME;
}
if (is_scalar( $var )) {
if (is_scalar($var)) {
return self::TYPE_STRING;
}
@ -99,7 +99,7 @@ class Enum extends \yii\base\Object
*/
public static function isEmpty( $var )
{
return !isset( $var ) ? true : ( is_array( $var ) ? empty( $var ) : ( $var === null || $var === '' ) );
return !isset($var) ? true : ( is_array($var) ? empty($var) : ( $var === null || $var === '' ) );
}
/**
@ -120,11 +120,12 @@ class Enum extends \yii\base\Object
*/
public static function formatBytes( $bytes, $precision = 2 )
{
$units = [ 'B', 'KB', 'MB', 'GB', 'TB' ];
$bytes = max( $bytes, 0 );
$pow = floor( ( $bytes ? log( $bytes ) : 0 ) / log( 1024 ) );
$pow = min( $pow, count( $units ) - 1 );
$bytes /= pow( 1024, $pow );
return round( $bytes, $precision ) . ' ' . $units[$pow];
$units = [ 'B', 'KB', 'MB', 'GB', 'TB' ];
$bytes = max($bytes, 0);
$pow = floor(( $bytes ? log($bytes) : 0 ) / log(1024));
$pow = min($pow, count($units) - 1);
$bytes /= pow(1024, $pow);
return round($bytes, $precision).' '.$units[$pow];
}
}

View file

@ -31,7 +31,7 @@ use yii\helpers\Url;
*/
class RedirectRule extends \yii\web\UrlRule
{
public $permanents = [];
public $permanents = [];
public $temporaries = [];
@ -55,14 +55,13 @@ class RedirectRule extends \yii\web\UrlRule
if(in_array($pathInfo, $this->permanents)) {
$request->setPathInfo($this->name);
Yii::$app->response->redirect($this->name, 301);
} else if(in_array($pathInfo, $this->temporaries)) {
} elseif(in_array($pathInfo, $this->temporaries)) {
$request->setPathInfo($this->name);
Yii::$app->response->redirect($this->name, 302);
}
parent::parseRequest($manager, $request);
Yii::$app->end();
}
}

View file

@ -202,7 +202,7 @@ class View extends \yii\web\View
$this->registerGoogleAnalytics();
}
return '<title>' . $this->title . '</title>' . parent::renderHeadHtml();
return '<title>'.$this->title.'</title>'.parent::renderHeadHtml();
}
/**
@ -212,7 +212,7 @@ class View extends \yii\web\View
*/
public function registerGoogleAnalytics()
{
$js = "(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)})(window,document,'script','$this->googleAnalyticsFile','$this->googleAnalyticsVar');";
$js = "(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)})(window,document,'script','$this->googleAnalyticsFile','$this->googleAnalyticsVar');";
$js .= "$this->googleAnalyticsVar('create', '$this->googleAnalyticsCode', 'auto');";
$js .= "$this->googleAnalyticsVar('send', 'pageview');";

View file

@ -12,7 +12,8 @@ namespace yiiaddon\tests\_data;
* @since v0.1
*/
class ModelOne extends \yii\base\Model
{
{
/**
* The first test proprety
*
@ -31,4 +32,5 @@ class ModelOne extends \yii\base\Model
[ [ 'propOne' ], 'string' ]
];
}
}

View file

@ -15,6 +15,11 @@ use yiiaddon\tests\_data\ActiveModel;
class ActiveRecordCest
{
/**
* The instace of ActiveModel that we will be using in the tests
*
* @var ActiveModel
*/
public $model;
public function _before( UnitTester $I )

View file

@ -16,6 +16,11 @@ use yiiaddon\tests\_data\ActiveModel;
class ActiveRecordCollectionCest
{
/**
* The model collection built before every test
*
* @var ActiveRecordCollection
*/
public $model;
public function _before( UnitTester $I )

View file

@ -15,8 +15,6 @@ use yiiaddon\tests\_data\ModelOne;
class ArrayHelperCest
{
public function _before( UnitTester $I )
{
}
@ -27,49 +25,50 @@ class ArrayHelperCest
public function testNumberArray( UnitTester $I )
{
$array = ArrayHelper::numberArray( 10, 2, 2 );
$array = ArrayHelper::numberArray(10, 2, 2);
$I->assertEquals( $array[ 2 ], 2 );
$I->assertFalse( isset( $array[ 3 ] ) );
$I->assertEquals( $array[ 4 ], 4 );
$I->assertEquals($array[2], 2);
$I->assertFalse(isset($array[3]));
$I->assertEquals($array[4], 4);
}
public function testNumberArrayWithStep( UnitTester $I )
{
$array = ArrayHelper::numberArray( 10 );
$array = ArrayHelper::numberArray(10);
$I->assertEquals( $array[ 0 ], 0 );
$I->assertEquals( $array[ 10 ], 10 );
$I->assertFalse( isset( $array[ 11 ] ) );
$I->assertEquals($array[0], 0);
$I->assertEquals($array[10], 10);
$I->assertFalse(isset($array[11]));
}
public function testParseAttribute( UnitTester $I )
{
$model = new ModelOne;
$model->propOne = "test";
$model->propOne = 'test';
$data = ArrayHelper::parseAttribute( $model, 'propOne' );
$data = ArrayHelper::parseAttribute($model, 'propOne');
$I->assertEquals( $data->format, 'text' );
$I->assertEquals($data->format, 'text');
$I->assertEquals( $data->lable, 'Prop One' );
$I->assertEquals($data->lable, 'Prop One');
$I->assertEquals( $data->value, 'test' );
$I->assertEquals( $data->raw, 'test' );
$I->assertEquals($data->value, 'test');
$I->assertEquals($data->raw, 'test');
}
public function testErrorWithParseAttribute( UnitTester $I )
{
$model = new ModelOne;
$model->propOne = "test";
$model->propOne = 'test';
try {
$data = ArrayHelper::parseAttribute( $model, 'not_a_prop' );
$I->fail( 'Not exseption thrown' );
} catch ( \yii\base\UnknownPropertyException $e ) {
} catch ( \Exception $e ) {
$I->fail( '\yii\base\UnknownPropertyException should have been thorwn' );
$data = ArrayHelper::parseAttribute($model, 'not_a_prop');
$I->fail('Not exseption thrown');
} catch (\yii\base\UnknownPropertyException $e) {
$I->assertEquals($e->getMessage(), 'Getting unknown property: yiiaddon\tests\_data\ModelOne::not_a_prop');
} catch (\Exception $e) {
$I->fail('\yii\base\UnknownPropertyException should have been thorwn');
}
}
@ -77,13 +76,15 @@ class ArrayHelperCest
{
$model = new ModelOne;
$model->propOne = "test";
$model->propOne = 'test';
try {
$data = ArrayHelper::parseAttribute( $model, '' );
$I->fail( 'Not exseption thrown' );
} catch ( \yii\base\InvalidConfigException $e ) {
} catch ( \Exception $e ) {
$I->fail( '\yii\base\InvalidConfigException should have been thorwn' );
$data = ArrayHelper::parseAttribute($model, '');
$I->fail('Not exseption thrown');
} catch (\yii\base\InvalidConfigException $e) {
$I->assertEquals($e->getMessage(), 'The column must be specified in the format of "attribute", "attribute:format" or "attribute:format:label"');
} catch (\Exception $e) {
$I->fail('\yii\base\InvalidConfigException should have been thorwn');
}
}
}

View file

@ -22,7 +22,7 @@ class ArrayObjectCest
public function _before( UnitTester $I )
{
$this->arrayObject = new ArrayObject( $this->array );
$this->arrayObject = new ArrayObject($this->array);
}
public function _after( UnitTester $I )
@ -31,7 +31,7 @@ class ArrayObjectCest
public function testArrayAsProps( UnitTester $I )
{
$I->assertEquals( $this->arrayObject->key_one, "Value One" );
$I->assertEquals($this->arrayObject->key_one, 'Value One');
}
/**
@ -41,8 +41,8 @@ class ArrayObjectCest
*/
public function testArrayContains( UnitTester $I )
{
$I->assertTrue( $this->arrayObject->contains( "Value One" ) );
$I->assertFalse( $this->arrayObject->contains( "Not in array" ) );
$I->assertTrue($this->arrayObject->contains('Value One'));
$I->assertFalse($this->arrayObject->contains('Not in array'));
}
/**
@ -54,14 +54,14 @@ class ArrayObjectCest
{
$table = $this->arrayObject->toTable();
$I->assertContains( '<table class="table table-bordered table-striped">', $table );
$I->assertContains( '<span title="string" style="cursor: default; border-bottom: 1px #aaa dashed;">Value One</span>', $table );
$I->assertContains('<table class="table table-bordered table-striped">', $table);
$I->assertContains('<span title="string" style="cursor: default; border-bottom: 1px #aaa dashed;">Value One</span>', $table);
}
public function testRandomValue( UnitTester $I )
{
for( $i = 0; $i < 5; $i++ ) {
$I->assertContains( $this->arrayObject->randomValue(), $this->array );
for($i = 0; $i < 5; $i++) {
$I->assertContains($this->arrayObject->randomValue(), $this->array);
}
}

View file

@ -24,8 +24,8 @@ class EnumCest
public function testNumberArray( UnitTester $I )
{
$I->assertEquals( Enum::formatBytes( 28434322.25 ), '27.12 MB' );
$I->assertEquals( Enum::formatBytes( 17328347842.25, 3 ), '16.138 GB' );
$I->assertEquals(Enum::formatBytes(28434322.25), '27.12 MB');
$I->assertEquals(Enum::formatBytes(17328347842.25, 3), '16.138 GB');
}
/**
@ -44,8 +44,9 @@ class EnumCest
'unknown' => new StdClass()
];
foreach ( $array as $type => $var ) {
$I->assertEquals( $type, Enum::getType( $var ) );
foreach ($array as $type => $var) {
$I->assertEquals($type, Enum::getType($var));
}
}
}

View file

@ -10,12 +10,12 @@
* @since v0.1
*/
require __DIR__ . "/../../vendor/autoload.php";
require __DIR__ . "/../../vendor/yiisoft/yii2/Yii.php";
require __DIR__.'/../../vendor/autoload.php';
require __DIR__.'/../../vendor/yiisoft/yii2/Yii.php';
$config = [
'id' => 'Yii Helpers Tests',
'basePath' => dirname( __DIR__ )
'basePath' => dirname(__DIR__)
];
( new yii\web\Application( $config ) );
( new yii\web\Application($config) );