Skip to content

Commit

Permalink
string matcher
Browse files Browse the repository at this point in the history
  • Loading branch information
developeruz committed Jul 20, 2015
1 parent ef6004e commit c2be099
Showing 1 changed file with 16 additions and 16 deletions.
32 changes: 16 additions & 16 deletions ModelMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
use yii\base\Security;
use yii\codeception\TestCase;
use yii\console\Exception;
use yii\validators\Validator;


class ModelMatcher extends TestCase {
class ModelMatcher extends TestCase
{

public $class;

Expand Down Expand Up @@ -45,20 +45,18 @@ public function shouldBeNotRequired($attribute, $onScenario = Model::SCENARIO_DE

public function matchLength($attribute, $min = null, $max = null, $onScenario = Model::SCENARIO_DEFAULT)
{
$stringValidator = $this->getValidator('yii\validators\StringValidator', ['max' => $max, 'min' => $min], $attribute, $onScenario);
$stringValidator = $this->getValidator('yii\validators\StringValidator', $attribute, $onScenario);
$stringGenerator = new Security();

if(!empty($min))
{
if (!empty($min)) {
$string = $stringGenerator->generateRandomString($min - 1);
$this->assertFalse($stringValidator->validate($string));

$string = $stringGenerator->generateRandomString($min);
$this->assertTrue($stringValidator->validate($string));
}

if(!empty($max))
{
if (!empty($max)) {
$string = $stringGenerator->generateRandomString($max + 1);
$this->assertFalse($stringValidator->validate($string));

Expand All @@ -75,8 +73,7 @@ public function hasOne($relationName, $relatedClass, $links = null)
$this->assertEquals($relatedClass, $relation->modelClass);
$this->assertFalse($relation->multiple);

if(!empty($links))
{
if (!empty($links)) {
$actualLinks = $relation->link;
$this->assertEmpty(array_diff($actualLinks, $links));
}
Expand All @@ -90,8 +87,7 @@ public function hasMany($relationName, $relatedClass, $links = null)
$this->assertEquals($relatedClass, $relation->modelClass);
$this->assertTrue($relation->multiple);

if(!empty($links))
{
if (!empty($links)) {
$actualLinks = $relation->link;
$this->assertEmpty(array_diff($actualLinks, $links));
}
Expand All @@ -105,13 +101,17 @@ private function getModel($scenario = Model::SCENARIO_DEFAULT)
return $model;
}

private function getValidator($type, $params, $attribute , $onScenario)
private function getValidator($type, $attribute, $onScenario)
{
$model = $this->getModel($onScenario);
$validator = Validator::createValidator($type, $model, $attribute, $params);
if(empty($validator))
{
throw new Exception('Can not generate validator');
$validators = $model->getActiveValidators($attribute);
foreach ($validators as $v) {
if ($v instanceof $type) {
return $v;
}
}
if (empty($validator)) {
throw new Exception('Not found ' . $type . ' validator for this class');
}
return $validator;
}
Expand Down

0 comments on commit c2be099

Please sign in to comment.