Passed
Pull Request — master (#25)
by Alexander
14:50
created

ExistsTest::testValidateValueExpectedException()   A

Complexity

Conditions 3
Paths 16

Size

Total Lines 19
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 19
rs 9.7998
cc 3
nc 16
nop 0
1
<?php
2
namespace Yiisoft\Validator\Tests\Rule;
3
4
use PHPUnit\Framework\TestCase;
5
use Yiisoft\Validator\Rule\Exist;
6
use Yiisoft\Validator\Tests\data\ValidatorTestMainModel;
7
use Yiisoft\Validator\Tests\data\ValidatorTestRefModel;
8
9
/**
10
 * @group validators
11
 */
12
class ExistsTest extends TestCase
13
{
14
    protected function setUp()
15
    {
16
        parent::setUp();
17
18
//        // destroy application, Validator must work without Yii::$app
19
//        $this->destroyApplication();
20
//        ActiveRecord::$db = $this->getConnection();
21
    }
22
23
    public function testValidateValueExpectedException()
24
    {
25
        try {
26
            $val = new Exist();
27
            $val->validate('ref');
28
            $this->fail('Exception should have been thrown at this time');
29
        } catch (\Exception $e) {
30
            $this->assertInstanceOf('\Exception', $e);
31
//            $this->assertInstanceOf('yii\base\InvalidConfigException', $e);
32
            $this->assertEquals('The "targetClass" property must be set.', $e->getMessage());
33
        }
34
        // combine to save the time creating a new db-fixture set (likely ~5 sec)
35
        try {
36
            $val = (new Exist())->targetClass(ValidatorTestMainModel::class);
0 ignored issues
show
Bug introduced by
The method targetClass() does not exist on Yiisoft\Validator\Rule\Exist. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

36
            $val = (new Exist())->/** @scrutinizer ignore-call */ targetClass(ValidatorTestMainModel::class);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
37
            $val->validate('ref');
38
            $this->fail('Exception should have been thrown at this time');
39
        } catch (\Exception $e) {
40
            $this->assertInstanceOf('\Exception', $e);
41
            $this->assertEquals('The "targetAttribute" property must be configured as a string.', $e->getMessage());
42
        }
43
    }
44
45
    public function testValidateValue()
46
    {
47
        $val = (new Exist())->targetClass(ValidatorTestRefModel::class)->targetAttribute('id');
48
        $this->assertTrue($val->validate(2));
49
        $this->assertTrue($val->validate(5));
50
        $this->assertFalse($val->validate(99));
51
        $this->assertFalse($val->validate(['1']));
52
    }
53
54
//    public function testValidateAttribute()
55
//    {
56
//        // existing value on different table
57
//        $val = new ExistValidator(['targetClass' => ValidatorTestMainModel::className(), 'targetAttribute' => 'id']);
58
//        $m = ValidatorTestRefModel::findOne(['id' => 1]);
59
//        $val->validateAttribute($m, 'ref');
60
//        $this->assertFalse($m->hasErrors());
61
//        // non-existing value on different table
62
//        $val = new ExistValidator(['targetClass' => ValidatorTestMainModel::className(), 'targetAttribute' => 'id']);
63
//        $m = ValidatorTestRefModel::findOne(['id' => 6]);
64
//        $val->validateAttribute($m, 'ref');
65
//        $this->assertTrue($m->hasErrors('ref'));
66
//        // existing value on same table
67
//        $val = new ExistValidator(['targetAttribute' => 'ref']);
68
//        $m = ValidatorTestRefModel::findOne(['id' => 2]);
69
//        $val->validateAttribute($m, 'test_val');
70
//        $this->assertFalse($m->hasErrors());
71
//        // non-existing value on same table
72
//        $val = new ExistValidator(['targetAttribute' => 'ref']);
73
//        $m = ValidatorTestRefModel::findOne(['id' => 5]);
74
//        $val->validateAttribute($m, 'test_val_fail');
75
//        $this->assertTrue($m->hasErrors('test_val_fail'));
76
//        // check for given value (true)
77
//        $val = new ExistValidator();
78
//        $m = ValidatorTestRefModel::findOne(['id' => 3]);
79
//        $val->validateAttribute($m, 'ref');
80
//        $this->assertFalse($m->hasErrors());
81
//        // check for given defaults (false)
82
//        $val = new ExistValidator();
83
//        $m = ValidatorTestRefModel::findOne(['id' => 4]);
84
//        $m->a_field = 'some new value';
85
//        $val->validateAttribute($m, 'a_field');
86
//        $this->assertTrue($m->hasErrors('a_field'));
87
//        // existing array
88
//        $val = new ExistValidator(['targetAttribute' => 'ref']);
89
//        $val->allowArray = true;
90
//        $m = new ValidatorTestRefModel();
91
//        $m->test_val = [2, 3, 4, 5];
92
//        $val->validateAttribute($m, 'test_val');
93
//        $this->assertFalse($m->hasErrors('test_val'));
94
//        // non-existing array
95
//        $val = new ExistValidator(['targetAttribute' => 'ref']);
96
//        $val->allowArray = true;
97
//        $m = new ValidatorTestRefModel();
98
//        $m->test_val = [95, 96, 97, 98];
99
//        $val->validateAttribute($m, 'test_val');
100
//        $this->assertTrue($m->hasErrors('test_val'));
101
//        // partial-existing array
102
//        $val = new ExistValidator(['targetAttribute' => 'ref']);
103
//        $val->allowArray = true;
104
//        $m = new ValidatorTestRefModel();
105
//        $m->test_val = [2, 97, 3, 98];
106
//        $val->validateAttribute($m, 'test_val');
107
//        $this->assertTrue($m->hasErrors('test_val'));
108
//        // existing array (allowArray = false)
109
//        $val = new ExistValidator(['targetAttribute' => 'ref']);
110
//        $val->allowArray = false;
111
//        $m = new ValidatorTestRefModel();
112
//        $m->test_val = [2, 3, 4, 5];
113
//        $val->validateAttribute($m, 'test_val');
114
//        $this->assertTrue($m->hasErrors('test_val'));
115
//        // non-existing array (allowArray = false)
116
//        $val = new ExistValidator(['targetAttribute' => 'ref']);
117
//        $val->allowArray = false;
118
//        $m = new ValidatorTestRefModel();
119
//        $m->test_val = [95, 96, 97, 98];
120
//        $val->validateAttribute($m, 'test_val');
121
//        $this->assertTrue($m->hasErrors('test_val'));
122
//    }
123
//
124
//    public function testValidateCompositeKeys()
125
//    {
126
//        $val = new ExistValidator([
127
//            'targetClass' => OrderItem::className(),
128
//            'targetAttribute' => ['order_id', 'item_id'],
129
//        ]);
130
//        // validate old record
131
//        $m = OrderItem::findOne(['order_id' => 1, 'item_id' => 2]);
132
//        $val->validateAttribute($m, 'order_id');
133
//        $this->assertFalse($m->hasErrors('order_id'));
134
//
135
//        // validate new record
136
//        $m = new OrderItem(['order_id' => 1, 'item_id' => 2]);
137
//        $val->validateAttribute($m, 'order_id');
138
//        $this->assertFalse($m->hasErrors('order_id'));
139
//        $m = new OrderItem(['order_id' => 2, 'item_id' => 5]);
140
//        $val->validateAttribute($m, 'order_id');
141
//        $this->assertFalse($m->hasErrors('order_id'));
142
//        $m = new OrderItem(['order_id' => 10, 'item_id' => 2]);
143
//        $val->validateAttribute($m, 'order_id');
144
//        $this->assertTrue($m->hasErrors('order_id'));
145
//
146
//        $val = new ExistValidator([
147
//            'targetClass' => OrderItem::className(),
148
//            'targetAttribute' => ['id' => 'order_id'],
149
//        ]);
150
//        // validate old record
151
//        $m = Order::findOne(1);
152
//        $val->validateAttribute($m, 'id');
153
//        $this->assertFalse($m->hasErrors('id'));
154
//        $m = Order::findOne(1);
155
//        $m->id = 10;
156
//        $val->validateAttribute($m, 'id');
157
//        $this->assertTrue($m->hasErrors('id'));
158
//
159
//        $m = new Order(['id' => 1]);
160
//        $val->validateAttribute($m, 'id');
161
//        $this->assertFalse($m->hasErrors('id'));
162
//        $m = new Order(['id' => 10]);
163
//        $val->validateAttribute($m, 'id');
164
//        $this->assertTrue($m->hasErrors('id'));
165
//    }
166
//
167
//    /**
168
//     * @see https://github.com/yiisoft/yii2/issues/14150
169
//     */
170
//    public function testTargetTableWithAlias()
171
//    {
172
//        $oldTableName = OrderItem::$tableName;
173
//        OrderItem::$tableName = '{{%order_item}}';
174
//
175
//        $val = new ExistValidator([
176
//            'targetClass' => OrderItem::className(),
177
//            'targetAttribute' => ['id' => 'order_id'],
178
//        ]);
179
//
180
//        $m = new Order(['id' => 1]);
181
//        $val->validateAttribute($m, 'id');
182
//        $this->assertFalse($m->hasErrors('id'));
183
//
184
//        OrderItem::$tableName = $oldTableName;
185
//    }
186
//
187
//    /**
188
//     * Test expresssion in targetAttribute.
189
//     * @see https://github.com/yiisoft/yii2/issues/14304
190
//     */
191
//    public function testExpresionInAttributeColumnName()
192
//    {
193
//        $val = new ExistValidator([
194
//            'targetClass' => OrderItem::className(),
195
//            'targetAttribute' => ['id' => 'COALESCE(order_id, 0)'],
196
//        ]);
197
//
198
//        $m = new Order(['id' => 1]);
199
//        $val->validateAttribute($m, 'id');
200
//        $this->assertFalse($m->hasErrors('id'));
201
//    }
202
//
203
//    public function testTargetRelation()
204
//    {
205
//        $val = new ExistValidator(['targetRelation' => 'references']);
206
//
207
//        $m = ValidatorTestMainModel::findOne(2);
208
//        $val->validateAttribute($m, 'id');
209
//        $this->assertFalse($m->hasErrors('id'));
210
//
211
//        $m = ValidatorTestMainModel::findOne(1);
212
//        $val->validateAttribute($m, 'id');
213
//        $this->assertTrue($m->hasErrors('id'));
214
//    }
215
//
216
//    public function testTargetRelationWithFilter()
217
//    {
218
//        $val = new ExistValidator(['targetRelation' => 'references', 'filter' => function ($query) {
219
//            $query->andWhere(['a_field' => 'ref_to_2']);
220
//        }]);
221
//        $m = ValidatorTestMainModel::findOne(2);
222
//        $val->validateAttribute($m, 'id');
223
//        $this->assertFalse($m->hasErrors('id'));
224
//
225
//        $val = new ExistValidator(['targetRelation' => 'references', 'filter' => function ($query) {
226
//            $query->andWhere(['a_field' => 'ref_to_3']);
227
//        }]);
228
//        $m = ValidatorTestMainModel::findOne(2);
229
//        $val->validateAttribute($m, 'id');
230
//        $this->assertTrue($m->hasErrors('id'));
231
//    }
232
//
233
//    public function testForceMaster()
234
//    {
235
//        $connection = $this->getConnectionWithInvalidSlave();
236
//        ActiveRecord::$db = $connection;
237
//
238
//        $model = null;
239
//        $connection->useMaster(function() use (&$model) {
240
//            $model = ValidatorTestMainModel::findOne(2);
241
//        });
242
//
243
//        $validator = new ExistValidator([
244
//            'forceMasterDb' => true,
245
//            'targetRelation' => 'references',
246
//        ]);
247
//        $validator->validateAttribute($model, 'id');
248
//
249
//        $this->expectException('\yii\base\InvalidConfigException');
250
//        $validator = new ExistValidator([
251
//            'forceMasterDb' => false,
252
//            'targetRelation' => 'references',
253
//        ]);
254
//        $validator->validateAttribute($model, 'id');
255
//
256
//        ActiveRecord::$db = $this->getConnection();
257
//    }
258
}
259