diff --git a/ruleset.xml b/ruleset.xml index 72a893d..d6621ea 100644 --- a/ruleset.xml +++ b/ruleset.xml @@ -3,7 +3,13 @@ Yii addon coding standards ./src + ./tests + */_output/* + */_data/* + **.*.css + **.*.js + @@ -14,12 +20,26 @@ */tests/* - */migrations/* + + + + */tests/* */tests/* - */migrations/* + + + + */tests/* + + + + */tests/* + + + + */tests/* @@ -67,6 +87,7 @@ + diff --git a/src/components/ActiveRecordCollection.php b/src/components/ActiveRecordCollection.php index e4a84e2..97c0e35 100644 --- a/src/components/ActiveRecordCollection.php +++ b/src/components/ActiveRecordCollection.php @@ -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)); } } diff --git a/src/db/ActiveQuery.php b/src/db/ActiveQuery.php index 6fb466d..5c93f38 100644 --- a/src/db/ActiveQuery.php +++ b/src/db/ActiveQuery.php @@ -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 */ diff --git a/src/db/ActiveRecord.php b/src/db/ActiveRecord.php index c279998..aa648d3 100644 --- a/src/db/ActiveRecord.php +++ b/src/db/ActiveRecord.php @@ -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); } /** diff --git a/src/db/Rankable.php b/src/db/Rankable.php index 628a50b..b012dd3 100644 --- a/src/db/Rankable.php +++ b/src/db/Rankable.php @@ -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; } diff --git a/src/filters/PageMin.php b/src/filters/PageMin.php index 5521fd3..e69e519 100644 --- a/src/filters/PageMin.php +++ b/src/filters/PageMin.php @@ -8,7 +8,7 @@ use yii\web\Response; /** * Minifies the html output of the page - * + * * @category PHP * @package adeattwood\yii-addon * @author Ade Attwood @@ -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, '') !== false) { $input = preg_replace_callback('#(.*?)#is', function($matches) { - return ''. $this->minifyCss($matches[2]) . ''; + return ''.$this->minifyCss($matches[2]).''; }, $input); } if(strpos($input, '') !== false) { $input = preg_replace_callback('#(.*?)#is', function($matches) { - return ''. $this->minifyJs($matches[2]) . ''; + return ''.$this->minifyJs($matches[2]).''; }, $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) diff --git a/src/helpers/ArrayHelper.php b/src/helpers/ArrayHelper.php index d7c2a6d..e6637eb 100644 --- a/src/helpers/ArrayHelper.php +++ b/src/helpers/ArrayHelper.php @@ -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 - ] ); + ]); } } diff --git a/src/helpers/ArrayObject.php b/src/helpers/ArrayObject.php index 458d290..974af4b 100644 --- a/src/helpers/ArrayObject.php +++ b/src/helpers/ArrayObject.php @@ -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 = '(not set)' ) { - 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"; - 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 = '
' . print_r( $value, true ) . '
'; + if (is_array($value)) { + $value = '
'.print_r($value, true).'
'; } else { - $value = Html::tag( 'span', $value, $valueOptions ); + $value = Html::tag('span', $value, $valueOptions); } - $table .= "\t\t" . Html::tag( 'span', $key, $keyOptions ) . "" . - "" . $value . "\n\t\n"; + $table .= "\t\t".Html::tag('span', $key, $keyOptions).''.''.$value."\n\t\n"; } - $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 .= '' . Html::tag( 'span', $heading, $keyOptions ) . ''; + foreach (array_keys($array[0]) as $heading) { + $table .= ''.Html::tag('span', $heading, $keyOptions).''; } $table .= "\n"; // The body - foreach ( $array as $row ) { + foreach ($array as $row) { $table .= "\t"; - foreach ( $row as $cell ) { + foreach ($row as $cell) { $table .= ''; // 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 .= ''; } @@ -244,4 +243,5 @@ class ArrayObject extends \ArrayObject $table .= ''; return $table; } + } diff --git a/src/helpers/Dumper.php b/src/helpers/Dumper.php index 39f4f87..7c2d2dd 100644 --- a/src/helpers/Dumper.php +++ b/src/helpers/Dumper.php @@ -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; } + } diff --git a/src/helpers/Enum.php b/src/helpers/Enum.php index 20646ec..1a1b40b 100644 --- a/src/helpers/Enum.php +++ b/src/helpers/Enum.php @@ -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]; } + } diff --git a/src/web/RedirectRule.php b/src/web/RedirectRule.php index d626332..5c94a3a 100644 --- a/src/web/RedirectRule.php +++ b/src/web/RedirectRule.php @@ -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(); - } } diff --git a/src/web/View.php b/src/web/View.php index 807d3d8..c86b793 100644 --- a/src/web/View.php +++ b/src/web/View.php @@ -202,7 +202,7 @@ class View extends \yii\web\View $this->registerGoogleAnalytics(); } - return '' . $this->title . '' . parent::renderHeadHtml(); + return ''.$this->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');"; diff --git a/tests/_data/ModelOne.php b/tests/_data/ModelOne.php index 31f0ec8..e71c066 100644 --- a/tests/_data/ModelOne.php +++ b/tests/_data/ModelOne.php @@ -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' ] ]; } + } diff --git a/tests/unit/ActiveRecordCest.php b/tests/unit/ActiveRecordCest.php index c88a7fc..8befb3b 100644 --- a/tests/unit/ActiveRecordCest.php +++ b/tests/unit/ActiveRecordCest.php @@ -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 ) diff --git a/tests/unit/ActiveRecordCollectionCest.php b/tests/unit/ActiveRecordCollectionCest.php index 108bc55..4358924 100644 --- a/tests/unit/ActiveRecordCollectionCest.php +++ b/tests/unit/ActiveRecordCollectionCest.php @@ -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 ) diff --git a/tests/unit/ArrayHelperCest.php b/tests/unit/ArrayHelperCest.php index d150a57..a9d50ef 100644 --- a/tests/unit/ArrayHelperCest.php +++ b/tests/unit/ArrayHelperCest.php @@ -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'); } } + } diff --git a/tests/unit/ArrayObjectCest.php b/tests/unit/ArrayObjectCest.php index 7298e1b..cb7963c 100644 --- a/tests/unit/ArrayObjectCest.php +++ b/tests/unit/ArrayObjectCest.php @@ -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 ); - $I->assertContains( 'Value One', $table ); + $I->assertContains('
', $table); + $I->assertContains('Value One', $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); } } diff --git a/tests/unit/EnumCest.php b/tests/unit/EnumCest.php index 7297bdf..c3a6555 100644 --- a/tests/unit/EnumCest.php +++ b/tests/unit/EnumCest.php @@ -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)); } } + } diff --git a/tests/unit/_bootstrap.php b/tests/unit/_bootstrap.php index ab1cda6..672480c 100644 --- a/tests/unit/_bootstrap.php +++ b/tests/unit/_bootstrap.php @@ -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) );