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'); |
|
|
|
|
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
|
|
|
|
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: