summaryrefslogtreecommitdiffstats
path: root/tests/Router/ControllerFactoryTest.php
blob: 855edb293a096e77b2450f43d2169fad558d1584 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php

namespace Jasny\Router;

use Jasny\Router\ControllerFactory;
use Jasny\TestHelper;

/**
 * @covers Jasny\Router\ControllerFactory
 */
class ControllerFactoryTest extends \PHPUnit_Framework_TestCase
{
    use TestHelper;
    
    public function testInvoke()
    {
        $mockController = new \stdClass();
        
        $factory = $this->createPartialMock(ControllerFactory::class, ['instantiate', 'assertClass']);
        $factory->expects($this->once())->method('instantiate')->with('FooController')->willReturn($mockController);
        
        $result = $factory('foo');
        
        $this->assertSame($mockController, $result);
    }
    
    public function invalidClassProvider()
    {
        return [
            [
                null,
                'foo-bar-zoo',
                "Can't route to controller 'FooBarZooController': class not exists"
            ],
            [
                null,
                ['foo', 'BAR', 'zoo'],
                "Can't route to controller 'Foo\Bar\ZooController': class not exists"
            ],
            [
                'StDclass',
                'foo',
                "Can't route to controller 'StDclass': case mismatch with 'stdClass'"
            ],
            [
                null,
                'fooBarZoo',
                "Can't route to controller 'FoobarzooController': class not exists"
            ],
            [
                null,
                '-foo-bar-zoo',
                "Can't route to controller '-fooBarZooController': invalid classname"
            ],
            [
                null,
                'foo--bar-zoo',
                "Can't route to controller 'Foo--barZooController': invalid classname"
            ],
            [
                null,
                'Foo\Bar\zoo',
                "Can't route to controller 'Foo\\\\bar\\\\zooController': invalid classname"
            ]
        ];
    }
    
    /**
     * @dataProvider invalidClassProvider
     * 
     * @param string       $class
     * @param string|array $controller
     * @param string       $message
     */
    public function testAssertClass($class, $controller, $message)
    {
        $this->expectException(\UnexpectedValueException::class);
        $this->expectExceptionMessage($message);
        
        if (empty($class)) {
            $runner = $this->createPartialMock(ControllerFactory::class, ['instantiate']);
        } else {
            $runner = $this->createPartialMock(ControllerFactory::class, ['instantiate', 'getClass']);
            $runner->expects($this->once())->method('getClass')->with('foo')->willReturn($class);
        }
        $runner->expects($this->never())->method('instantiate');
        
        $runner($controller);
    }
    
    public function testInstantiate()
    {
        $runner = $this->createPartialMock(ControllerFactory::class, ['getClass']);
        $runner->expects($this->once())->method('getClass')->with('foo')->willReturn('stdClass');
        
        $result = $runner('foo');
        
        $this->assertInstanceOf(\stdClass::class, $result);
    }
    
    
    public function testChain()
    {
        $mockController = new \stdClass();
        $chain = $this->createCallbackMock($this->once(), ['FooController'], $mockController);
        
        $factory = $this->getMockBuilder(ControllerFactory::class)
            ->setConstructorArgs([$chain])
            ->setMethods(['assertClass'])
            ->getMock();
        
        $result = $factory('foo');
        
        $this->assertSame($mockController, $result);
    }
    
    /**
     * @expectedException \InvalidArgumentException
     */
    public function testInvalidChain()
    {
        new ControllerFactory('not callable');
    }
}