Passed
Push — master ( 68c09b...a4bad7 )
by DeGracia
41s queued 11s
created

ManagerTests::make_with_singleton_drivers()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace DeGraciaMathieu\Manager\Tests;
4
5
use PHPUnit\Framework\TestCase;
6
use DeGraciaMathieu\Manager\Manager;
7
use DeGraciaMathieu\Manager\Exceptions\DriverResolutionException;
8
9
class ManagerTests extends TestCase
10
{
11
    /**
12
     * @test
13
     */
14
    public function make()
15
    {
16
        $manager = $this->getManager($singleton = false);
17
18
        $this->assertEquals($manager->doAnything(), 'do_anything_from_foo_driver');
0 ignored issues
show
Documentation Bug introduced by
The method doAnything does not exist on object<DeGraciaMathieu\M...sts/ManagerTests.php$0>? 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...
19
        $this->assertEquals($manager->driver()->doAnything(), 'do_anything_from_foo_driver');
20
        $this->assertEquals($manager->driver('foo')->doAnything(), 'do_anything_from_foo_driver');
21
        $this->assertEquals($manager->driver('bar')->doAnything(), 'do_anything_from_bar_driver');
22
    }
23
24
    /**
25
     * @test
26
     */
27
    public function make_with_singleton_drivers()
28
    {
29
        $manager = $this->getManager($singleton = true);
30
31
        $manager->driver('foo')->doAnything();
32
        $manager->driver('foo')->doAnything();
33
        $manager->driver('foo')->doAnything();
34
35
        $this->assertEquals($manager->driver('foo')->doAnything(), 'do_anything_from_foo_driver');
36
    }
37
38
    /**
39
     * @test
40
     */
41
    public function make_with_unexpected_driver()
42
    {
43
        $manager = $this->getManager($singleton = false);
44
45
        $this->expectException(DriverResolutionException::class);
46
47
        $manager->driver('unexpected');
48
    }
49
50
    /**
51
     * @return Anonymous
52
     */
53
    protected function getManager(bool $needSingleton)
54
    {
55
        return new class($needSingleton) extends Manager {
56
57
            public function __construct(bool $needSingleton)
58
            {
59
                parent::__construct();
60
61
                $this->singleton = $needSingleton;
62
            }
63
64
            public function createFooDriver()
65
            {
66
                return new class {
67
68
                    public function doAnything()
69
                    {
70
                        return 'do_anything_from_foo_driver';
71
                    }
72
                };
73
            }
74
75
            public function createBarDriver()
76
            {
77
                return new class {
78
79
                    public function doAnything()
80
                    {
81
                        return 'do_anything_from_bar_driver';
82
                    }
83
                };
84
            }
85
86
            public function getDefaultDriver()
87
            {
88
                return 'foo';
89
            }
90
        };
91
    }
92
}
93