Tests on array helper

This commit is contained in:
Ade Attwood 2017-06-23 22:35:56 +01:00
parent ff1500369c
commit 6350c747b6
2 changed files with 36 additions and 8 deletions

View file

@ -2,6 +2,10 @@
namespace adeattwood\helpers; namespace adeattwood\helpers;
use Yii;
use adeattwood\helpers\ArrayObject;
use adeattwood\helpers\Enum;
/** /**
* @category PHP * @category PHP
* @package adeattwood\yii-helpers * @package adeattwood\yii-helpers
@ -64,10 +68,10 @@ class ArrayHelper extends \yii\helpers\ArrayHelper
throw new InvalidConfigException( 'The column must be specified in the format of "attribute", "attribute:format" or "attribute:format:label"' ); throw new InvalidConfigException( 'The column must be specified in the format of "attribute", "attribute:format" or "attribute:format:label"' );
} }
$rawValue = ArrayHelper::getValue( $model, $matches[ 1 ] ); $rawValue = self::getValue( $model, $matches[ 1 ] );
$format = isset( $matches[ 3 ] ) ? $matches[ 3 ] : $defalts[ 'format' ]; $format = isset( $matches[ 3 ] ) ? $matches[ 3 ] : $defalts[ 'format' ];
$value = empty( $rawValue ) ? $defalts[ 'value' ] : Yii::$app->formatter->format( $rawValue, $format ); $value = Enum::isEmpty( $rawValue ) ? $defalts[ 'value' ] : Yii::$app->formatter->format( $rawValue, $format );
$lable = isset( $matches[ 5 ] ) ? $matches[ 5 ] : $model->getAttributeLabel( $matches[ 1 ] ); $lable = isset( $matches[ 5 ] ) ? $matches[ 5 ] : $model->getAttributeLabel( $matches[ 1 ] );
return new ArrayObject([ return new ArrayObject([

View file

@ -1,25 +1,49 @@
<?php <?php
use adeattwood\helpers\ArrayHelper; use adeattwood\helpers\ArrayHelper;
use adeattwood\helpers\tests\_data\ModelOne;
class ArrayHelperCest class ArrayHelperCest
{ {
public function _before(UnitTester $I) public function _before( UnitTester $I )
{ {
} }
public function _after(UnitTester $I) public function _after( UnitTester $I )
{ {
} }
public function testNumberArray(UnitTester $I) public function testNumberArray( UnitTester $I )
{ {
$array = ArrayHelper::numberArray( 11 ); $array = ArrayHelper::numberArray( 10, 2, 2 );
$I->assertEquals( $array[ 2 ], 2 );
$I->assertFalse( isset( $array[ 3 ] ) );
$I->assertEquals( $array[ 4 ], 4 );
}
public function testNumberArrayWithStep( UnitTester $I )
{
$array = ArrayHelper::numberArray( 10 );
$I->assertEquals( $array[ 0 ], 0 ); $I->assertEquals( $array[ 0 ], 0 );
$I->assertEquals( $array[ 10 ], 10 ); $I->assertEquals( $array[ 10 ], 10 );
$array = ArrayHelper::numberArray( 10, 2, 2 ); $I->assertFalse( isset( $array[ 11 ] ) );
var_dump( $array ); }
public function testParseAttribute( UnitTester $I )
{
$model = new ModelOne;
$model->propOne = "test";
$data = ArrayHelper::parseAttribute( $model, 'propOne' );
$I->assertEquals( $data->format, 'text' );
$I->assertEquals( $data->lable, 'Prop One' );
$I->assertEquals( $data->value, 'test' );
$I->assertEquals( $data->raw, 'test' );
} }
} }