1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace andmemasin\myabstract\traits; |
4
|
|
|
use yii\base\Model; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Trait ActiveRecordTestTrait |
8
|
|
|
* @package andmemasin\myabstract\traits |
9
|
|
|
* @author Tõnis Ormisson <[email protected]> |
10
|
|
|
* @property Model $model |
11
|
|
|
*/ |
12
|
|
|
trait ModelTestTrait |
13
|
|
|
{ |
14
|
|
|
public function testAttributeLabelsForExistingAttributesOnly() { |
15
|
|
|
// labels only for actually existing attributes |
16
|
|
|
foreach ($this->model->attributeLabels() as $key => $label) { |
17
|
|
|
$this->assertArrayHasKey($key, $this->model->attributes); |
|
|
|
|
18
|
|
|
} |
19
|
|
|
} |
20
|
|
|
public function testAttributeHintsForExistingAttributesOnly() { |
21
|
|
|
// labels only for actually existing attributes |
22
|
|
|
foreach ($this->model->attributeHints() as $key => $label) { |
23
|
|
|
$this->assertArrayHasKey($key, $this->model->attributes); |
24
|
|
|
} |
25
|
|
|
} |
26
|
|
|
public function testRulesForExistingAttributesOnly() { |
27
|
|
|
// labels only for actually existing attributes |
28
|
|
|
foreach ($this->model->rules() as $rule) { |
29
|
|
|
foreach ($rule[0] as $attribute) { |
30
|
|
|
$this->assertArrayHasKey($attribute, $this->model->attributes); |
31
|
|
|
} |
32
|
|
|
} |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Call protected/private method of a class. |
37
|
|
|
* |
38
|
|
|
* @param object &$object Instantiated object that we will run method on. |
39
|
|
|
* @param string $methodName Method name to call |
40
|
|
|
* @param array $parameters Array of parameters to pass into method. |
41
|
|
|
* |
42
|
|
|
* @return mixed Method return. |
43
|
|
|
*/ |
44
|
|
|
public function invokeMethod(&$object, $methodName, array $parameters = array()) |
45
|
|
|
{ |
46
|
|
|
$reflection = new \ReflectionClass(get_class($object)); |
47
|
|
|
$method = $reflection->getMethod($methodName); |
48
|
|
|
$method->setAccessible(true); |
49
|
|
|
|
50
|
|
|
return $method->invokeArgs($object, $parameters); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
} |