-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathModelMatcher.php
118 lines (94 loc) · 3.54 KB
/
ModelMatcher.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<?php
namespace developeruz\yii_matcher;
use yii\base\Model;
use yii\base\Security;
use yii\codeception\TestCase;
use yii\console\Exception;
class ModelMatcher extends TestCase
{
public $class;
public function __construct($class)
{
parent::__construct();
$this->class = $class;
}
public function shouldBeSafe($attribute, $onScenario = Model::SCENARIO_DEFAULT)
{
$model = $this->getModel($onScenario);
$this->assertTrue($model->isAttributeSafe($attribute));
}
public function shouldBeNotSafe($attribute, $onScenario = Model::SCENARIO_DEFAULT)
{
$model = $this->getModel($onScenario);
$this->assertFalse($model->isAttributeSafe($attribute));
}
public function shouldBeRequired($attribute, $onScenario = Model::SCENARIO_DEFAULT)
{
$model = $this->getModel($onScenario);
$this->assertTrue($model->isAttributeRequired($attribute));
}
public function shouldBeNotRequired($attribute, $onScenario = Model::SCENARIO_DEFAULT)
{
$model = $this->getModel($onScenario);
$this->assertFalse($model->isAttributeRequired($attribute));
}
public function matchLength($attribute, $min = null, $max = null, $onScenario = Model::SCENARIO_DEFAULT)
{
$stringValidator = $this->getValidator('yii\validators\StringValidator', $attribute, $onScenario);
$stringGenerator = new Security();
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)) {
$string = $stringGenerator->generateRandomString($max + 1);
$this->assertFalse($stringValidator->validate($string));
$string = $stringGenerator->generateRandomString($max);
$this->assertTrue($stringValidator->validate($string));
}
}
public function hasOne($relationName, $relatedClass, $links = null)
{
$model = $this->getModel();
$relation = $model->getRelation($relationName);
$this->assertEquals($relatedClass, $relation->modelClass);
$this->assertFalse($relation->multiple);
if (!empty($links)) {
$actualLinks = $relation->link;
$this->assertEmpty(array_diff($actualLinks, $links));
}
}
public function hasMany($relationName, $relatedClass, $links = null)
{
$model = $this->getModel();
$relation = $model->getRelation($relationName);
$this->assertEquals($relatedClass, $relation->modelClass);
$this->assertTrue($relation->multiple);
if (!empty($links)) {
$actualLinks = $relation->link;
$this->assertEmpty(array_diff($actualLinks, $links));
}
}
private function getModel($scenario = Model::SCENARIO_DEFAULT)
{
$model = new $this->class();
$model->scenario = $scenario;
return $model;
}
private function getValidator($type, $attribute, $onScenario)
{
$model = $this->getModel($onScenario);
$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;
}
}