LegacyViewHelperWrapperTest::testDirect()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Tworzenieweb\Helper;
4
5
use Mockery;
6
use Mockery\MockInterface;
7
use PHPUnit_Framework_TestCase;
8
use Zend_View;
9
10
/**
11
 * @author Luke Adamczewski
12
 * @package Tworzenieweb\Helper
13
 */
14
class LegacyViewHelperWrapperTest extends PHPUnit_Framework_TestCase
15
{
16
    /**
17
     * @var BCViewHelperInterface|MockInterface
18
     */
19
    private $helper;
20
21
    /**
22
     * @var LegacyViewHelper
23
     */
24
    private $helperWrapper;
25
26
    public function setUp()
27
    {
28
        $this->helper = Mockery::mock('\Tworzenieweb\Helper\JobleadsHelper')->makePartial();
29
        $this->helperWrapper = new LegacyViewHelper();
30
        $this->helperWrapper->injectHelper($this->helper);
31
    }
32
33
    public function testInnerHelperInvokeMethodShouldBeInvoked()
34
    {
35
        $args = [1, 2, 3];
36
        $this->assertTrue(true, is_callable($this->helperWrapper));
37
        $this->helper->shouldReceive('__invoke')->once()->withArgs($args);
38
39
        $this->helperWrapper->__invoke($args);
40
    }
41
42
    public function testHelperWrapperShouldBeCompatibleWithZendView()
43
    {
44
        $view = new Zend_View();
45
        $name = $this->helperWrapper->getName();
46
        $view->registerHelper($this->helperWrapper, $name);
47
        $this->helper->shouldReceive('__invoke')->once()->with(1, 2, 3);
48
49
        $view->{$name}(1, 2, 3);
50
    }
51
52
    /**
53
     * @expectedException \RuntimeException
54
     */
55
    public function testItShouldFailWhenHelperIsNotCallable()
56
    {
57
        $notCallableMock = Mockery::mock('\Tworzenieweb\Helper\BCViewHelperInterface');
58
        $this->helperWrapper->injectHelper($notCallableMock);
59
        $this->helperWrapper->__invoke();
60
    }
61
62
    /**
63
     * @expectedException \InvalidArgumentException
64
     */
65
    public function testHelperShouldBeCallableByNameOnly()
66
    {
67
        $name = $this->helperWrapper->getName();
68
        $this->helper->shouldReceive('__invoke')->once();
69
        $this->helperWrapper->{$name}();
70
        
71
        $this->helperWrapper->someOtherMagicMethodThatWillFail();
0 ignored issues
show
Documentation Bug introduced by
The method someOtherMagicMethodThatWillFail does not exist on object<Tworzenieweb\Helper\LegacyViewHelper>? 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...
72
    }
73
74
    public function testDirect()
75
    {
76
        $this->assertNull($this->helperWrapper->direct());
77
    }
78
}
79