Completed
Push — master ( 984fe7...d1eeb4 )
by Tõnis
02:37
created

ModelTestTrait::invokeMethod()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 3
dl 0
loc 7
rs 10
c 0
b 0
f 0
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);
0 ignored issues
show
Bug introduced by
It seems like assertArrayHasKey() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

17
            $this->/** @scrutinizer ignore-call */ 
18
                   assertArrayHasKey($key, $this->model->attributes);
Loading history...
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
}