2
2
3
3
declare (strict_types=1 );
4
4
5
- /**
6
- * This file is part of php-fast-forward/container.
7
- *
8
- * This source file is subject to the license bundled
9
- * with this source code in the file LICENSE.
10
- *
11
- * @link https://github.com/php-fast-forward/container
12
- * @copyright Copyright (c) 2025 Felipe Sayão Lobato Abreu <github@mentordosnerds.com>
13
- * @license https://opensource.org/licenses/MIT MIT License
14
- */
15
-
16
5
namespace FastForward \Container \Tests \Factory ;
17
6
18
7
use FastForward \Container \Exception \RuntimeException ;
23
12
use Prophecy \PhpUnit \ProphecyTrait ;
24
13
use Psr \Container \ContainerInterface ;
25
14
26
- /**
27
- * @internal
28
- */
29
15
#[CoversClass(MethodFactory::class)]
30
16
#[UsesClass(RuntimeException::class)]
31
17
final class MethodFactoryTest extends TestCase
@@ -34,14 +20,14 @@ final class MethodFactoryTest extends TestCase
34
20
35
21
public function testInvokeInstanceMethod (): void
36
22
{
37
- $ service = new MethodTarget ();
23
+ $ service = new MethodFactoryTestTargetStub ();
38
24
39
25
$ container = $ this ->prophesize (ContainerInterface::class);
40
26
$ container ->has ('prefix ' )->willReturn (false );
41
- $ container ->has (MethodTarget ::class)->willReturn (true );
42
- $ container ->get (MethodTarget ::class)->willReturn ($ service );
27
+ $ container ->has (MethodFactoryTestTargetStub ::class)->willReturn (true );
28
+ $ container ->get (MethodFactoryTestTargetStub ::class)->willReturn ($ service );
43
29
44
- $ factory = new MethodFactory (MethodTarget ::class, 'instanceMethod ' , 'prefix ' );
30
+ $ factory = new MethodFactory (MethodFactoryTestTargetStub ::class, 'instanceMethod ' , 'prefix ' );
45
31
46
32
$ result = $ factory ($ container ->reveal ());
47
33
@@ -51,10 +37,9 @@ public function testInvokeInstanceMethod(): void
51
37
public function testInvokeStaticMethod (): void
52
38
{
53
39
$ container = $ this ->prophesize (ContainerInterface::class);
54
- $ container ->has (MethodTarget::class)->willReturn (false );
55
40
$ container ->has ('value ' )->willReturn (false );
56
41
57
- $ factory = new MethodFactory (MethodTarget ::class, 'staticMethod ' , 'value ' );
42
+ $ factory = new MethodFactory (MethodFactoryTestTargetStub ::class, 'staticMethod ' , 'value ' );
58
43
59
44
$ result = $ factory ($ container ->reveal ());
60
45
@@ -63,16 +48,16 @@ public function testInvokeStaticMethod(): void
63
48
64
49
public function testInvokeResolvesArgumentsFromContainer (): void
65
50
{
66
- $ service = new MethodTarget ();
51
+ $ service = new MethodFactoryTestTargetStub ();
67
52
$ argObj = new \stdClass ();
68
53
69
54
$ container = $ this ->prophesize (ContainerInterface::class);
70
- $ container ->has (MethodTarget ::class)->willReturn (true );
71
- $ container ->get (MethodTarget ::class)->willReturn ($ service );
55
+ $ container ->has (MethodFactoryTestTargetStub ::class)->willReturn (true );
56
+ $ container ->get (MethodFactoryTestTargetStub ::class)->willReturn ($ service );
72
57
$ container ->has ('dependency ' )->willReturn (true );
73
58
$ container ->get ('dependency ' )->willReturn ($ argObj );
74
59
75
- $ factory = new MethodFactory (MethodTarget ::class, 'acceptsObject ' , 'dependency ' );
60
+ $ factory = new MethodFactory (MethodFactoryTestTargetStub ::class, 'acceptsObject ' , 'dependency ' );
76
61
77
62
$ result = $ factory ($ container ->reveal ());
78
63
@@ -81,23 +66,50 @@ public function testInvokeResolvesArgumentsFromContainer(): void
81
66
82
67
public function testInvokeThrowsForNonPublicMethod (): void
83
68
{
84
- $ service = new MethodTarget ();
85
-
86
69
$ container = $ this ->prophesize (ContainerInterface::class);
87
- $ container ->has (MethodTarget::class)->willReturn (true );
88
- $ container ->get (MethodTarget::class)->willReturn ($ service );
70
+ $ container ->has (MethodFactoryTestTargetStub::class)->willReturn (false );
89
71
90
- $ factory = new MethodFactory (MethodTarget ::class, 'privateMethod ' );
72
+ $ factory = new MethodFactory (MethodFactoryTestTargetStub ::class, 'privateMethod ' );
91
73
92
74
$ this ->expectException (RuntimeException::class);
93
- $ this ->expectExceptionMessage ('Method "FastForward\Container\Tests\Factory\MethodTarget::privateMethod" MUST be public to be invoked as a service. ' );
94
75
95
76
$ factory ($ container ->reveal ());
96
77
}
78
+
79
+ public function testInvokeWillConstructTargetIfContainerDoesNotProvide (): void
80
+ {
81
+ $ container = $ this ->prophesize (ContainerInterface::class);
82
+ $ container ->has ('prefix ' )->willReturn (false );
83
+ $ container ->get (MethodFactoryTestTargetStub::class)->willThrow (new \Exception ());
84
+
85
+ $ factory = new MethodFactory (MethodFactoryTestTargetStub::class, 'instanceMethod ' , 'prefix ' );
86
+
87
+ $ result = $ factory ($ container ->reveal ());
88
+
89
+ self ::assertSame ('prefix-instance ' , $ result );
90
+ }
91
+
92
+ public function testConstructorDependencyIsResolvedFromContainer (): void
93
+ {
94
+ $ dependency = new MethodFactoryTestDependencyStub ();
95
+
96
+ $ container = $ this ->prophesize (ContainerInterface::class);
97
+ $ container ->has (MethodFactoryTestTargetStub::class)->willReturn (true );
98
+ $ container ->get (MethodFactoryTestTargetStub::class)->willReturn (new MethodFactoryTestTargetStub ($ dependency ))->shouldBeCalledOnce ();
99
+ $ container ->has ('suffix ' )->willReturn (false );
100
+
101
+ $ factory = new MethodFactory (MethodFactoryTestTargetStub::class, 'usesConstructorArgument ' , 'suffix ' );
102
+
103
+ $ result = $ factory ($ container ->reveal ());
104
+
105
+ self ::assertSame (MethodFactoryTestDependencyStub::class . '-suffix ' , $ result );
106
+ }
97
107
}
98
108
99
- class MethodTarget
109
+ class MethodFactoryTestTargetStub
100
110
{
111
+ public function __construct (private ?MethodFactoryTestDependencyStub $ dependency = null ) {}
112
+
101
113
public function instanceMethod (string $ prefix ): string
102
114
{
103
115
return $ prefix . '-instance ' ;
@@ -113,5 +125,14 @@ public function acceptsObject(object $obj): object
113
125
return $ obj ;
114
126
}
115
127
128
+ public function usesConstructorArgument (string $ suffix ): string
129
+ {
130
+ return $ this ->dependency ::class . '- ' . $ suffix ;
131
+ }
132
+
116
133
private function privateMethod (): void {}
117
134
}
135
+
136
+ class MethodFactoryTestDependencyStub
137
+ {
138
+ }
0 commit comments