@@ -16,48 +16,89 @@ class MockableServiceProxyFactoryTest extends TestCase
16
16
/** @var FakeService */
17
17
private $ alternative ;
18
18
19
- /** @var MockableService|FakeService $proxy */
20
- private $ proxy ;
21
-
22
19
protected function setUp (): void
23
20
{
24
21
parent ::setUp ();
25
22
26
23
$ this ->original = new FakeService ('orig ' );
27
24
$ this ->alternative = new FakeService ('mock ' );
28
-
29
- $ factory = new MockableServiceProxyFactory ();
30
-
31
- $ this ->proxy = $ factory ->createServiceProxy ($ this ->original );
32
25
}
33
26
34
27
/** @test */
35
28
public function it_should_generate_a_mockable_service_proxy (): void
36
29
{
37
- $ this ->assertInstanceOf (MockableService::class, $ this ->proxy );
38
- $ this ->assertInstanceOf (FakeService::class, $ this ->proxy );
30
+ $ factory = new MockableServiceProxyFactory ();
31
+ /** @var FakeService&MockableService $proxy */
32
+ $ proxy = $ factory ->createServiceProxy ($ this ->original );
39
33
40
- $ this ->assertEquals ($ this ->original ->getValue (), $ this ->proxy ->getValue ());
34
+ $ this ->assertInstanceOf (MockableService::class, $ proxy );
35
+ $ this ->assertInstanceOf (FakeService::class, $ proxy );
36
+
37
+ $ this ->assertEquals ($ this ->original ->getValue (), $ proxy ->getValue ());
41
38
}
42
39
43
40
/** @test */
44
41
public function it_should_generate_a_working_set_alternative (): void
45
42
{
46
- $ this ->proxy ->setAlternativeService ($ this ->alternative );
47
- $ this ->assertEquals ($ this ->alternative ->getValue (), $ this ->proxy ->getValue ());
43
+ $ factory = new MockableServiceProxyFactory ();
44
+ /** @var FakeService&MockableService $proxy */
45
+ $ proxy = $ factory ->createServiceProxy ($ this ->original );
46
+
47
+ $ proxy ->setAlternativeService ($ this ->alternative );
48
+ $ this ->assertEquals ($ this ->alternative ->getValue (), $ proxy ->getValue ());
48
49
}
49
50
50
51
/** @test */
51
52
public function it_should_generate_a_working_reset (): void
52
53
{
53
- $ this ->proxy ->setAlternativeService ($ this ->alternative );
54
- $ this ->proxy ->restoreOriginalService ();
54
+ $ factory = new MockableServiceProxyFactory ();
55
+ /** @var FakeService&MockableService $proxy */
56
+ $ proxy = $ factory ->createServiceProxy ($ this ->original );
57
+
58
+ $ proxy ->setAlternativeService ($ this ->alternative );
59
+ $ proxy ->restoreOriginalService ();
60
+
61
+ $ this ->assertEquals ($ this ->original ->getValue (), $ proxy ->getValue ());
62
+ }
63
+
64
+ /** @test */
65
+ public function it_should_generate_a_mockable_service_proxy_via_an_interface (): void
66
+ {
67
+ $ original = new FinalFakeService ('value ' );
68
+
69
+ $ factory = new MockableServiceProxyFactory ();
70
+ /** @var FinalFakeService&MockableService $proxy */
71
+ $ proxy = $ factory ->createInterfaceServiceProxy ($ original , TestInterface::class);
72
+
73
+ $ this ->assertInstanceOf (MockableService::class, $ proxy );
74
+ $ this ->assertInstanceOf (TestInterface::class, $ proxy );
75
+
76
+ $ this ->assertEquals ($ original ->getValue (), $ proxy ->getValue ());
77
+ }
78
+ }
79
+
80
+ interface TestInterface
81
+ {
82
+ public function getValue (): string ;
83
+ }
84
+
85
+ class FakeService implements TestInterface
86
+ {
87
+ /** @var string */
88
+ private $ value ;
89
+
90
+ public function __construct (string $ value )
91
+ {
92
+ $ this ->value = $ value ;
93
+ }
55
94
56
- $ this ->assertEquals ($ this ->original ->getValue (), $ this ->proxy ->getValue ());
95
+ public function getValue (): string
96
+ {
97
+ return $ this ->value ;
57
98
}
58
99
}
59
100
60
- class FakeService
101
+ final class FinalFakeService implements TestInterface
61
102
{
62
103
/** @var string */
63
104
private $ value ;
0 commit comments