PrototypeTest::testToString()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 13
rs 9.4285
cc 1
eloc 7
nc 1
nop 0
1
<?php
2
3
class PrototypeTest extends PHPUnit_Framework_TestCase
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
4
{
5
    public function testExtend()
6
    {
7
        $tester = $this;
8
9
        $o = new Rde\Prototype;
10
        $o['sayHello'] = function($o){
0 ignored issues
show
Unused Code introduced by
The parameter $o is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
11
            return "Hello";
12
        };
13
        $o['sum'] = function($o, $a, $b) use($tester) {
14
            $tester->assertTrue(isset($o['sum']), '確認注入程序');
15
            return $a + $b;
16
        };
17
18
        $this->assertFalse(method_exists($o, 'sayHello'), '測試method_exists');
19
        $this->assertTrue(is_callable(array($o, 'sayHelloxx')), '測試is_callable sayHelloxx(不存在)');
20
        $this->assertFalse($o->hasDriver('sayHelloxx'), '測試hasDriver(sayHelloxx)');
21
        $this->assertTrue(is_callable(array($o, 'sayHello')), '測試is_callable');
22
        $this->assertTrue($o->hasDriver('sayHello'), '測試hasDriver(sayHello)');
23
24
        $this->assertEquals(
25
            'Hello',
26
            $o->sayHello(),
0 ignored issues
show
Documentation Bug introduced by
The method sayHello does not exist on object<Rde\Prototype>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
27
            '檢查建構注入');
28
29
        $ret = $o->extend('sayHello', function($o, $name){
30
                return "hello {$name}";
31
            });
32
33
        $this->assertEquals($o, $ret, '檢查物件方法鍊');
34
35
        $this->assertEquals(
36
            'hello abc',
37
            $o->sayHello('abc'),
0 ignored issues
show
Documentation Bug introduced by
The method sayHello does not exist on object<Rde\Prototype>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
38
            '檢查動態注入覆寫');
39
40
        $this->assertEquals(
41
            9,
42
            $o->sum(1, 8),
0 ignored issues
show
Documentation Bug introduced by
The method sum does not exist on object<Rde\Prototype>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
43
            '檢查注入驅動處理');
44
    }
45
46
    public function testArrayAccess()
47
    {
48
        $tester = $this;
49
        $o = new Rde\Prototype();
50
51
        $o['verify'] = function($o, array $balls) use($tester) {
52
53
            $tester->assertInstanceOf('\\Rde\\Prototype', $o, '檢查容器');
54
55
            return 5 == count(array_unique($balls));
56
        };
57
58
        $this->assertTrue(isset($o['verify']), '檢查驅動');
59
60
        $this->assertTrue(
61
            $o->verify(array(1,2,3,4,5)),
0 ignored issues
show
Documentation Bug introduced by
The method verify does not exist on object<Rde\Prototype>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
62
            '檢查注入驗證回傳');
63
    }
64
65
    /**
66
     * @expectedException \BadMethodCallException
67
     * @expectedExceptionMessageRegExp /沒有安裝\[\w+\]處理驅動/
68
     */
69
    public function testException()
70
    {
71
        $o = new Rde\Prototype();
72
73
        $o->getColde();
0 ignored issues
show
Documentation Bug introduced by
The method getColde does not exist on object<Rde\Prototype>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
74
    }
75
76
    public function testToString()
77
    {
78
        $o = new Rde\Prototype();
79
80
        $this->assertEquals('['.get_class($o).']', (string) $o, '檢查預設轉型(string)');
81
82
        $str = 'hello world';
83
        $o->extend('__toString', function() use($str){
84
            return $str;
85
        });
86
87
        $this->assertEquals($str, (string) $o, '檢查自訂轉型(string)');
88
    }
89
}
90