Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
7 | class ArgumentResolverTest extends TestCase |
||
8 | { |
||
9 | |||
10 | public function tearDown() |
||
11 | { |
||
12 | Mockery::close(); |
||
13 | } |
||
14 | |||
15 | |||
16 | /** |
||
17 | * @test |
||
18 | */ |
||
19 | public function canReflectFunction() |
||
20 | { |
||
21 | $resolver = new ArgumentResolver(Mockery::mock(Container::class)); |
||
22 | $reflection = $resolver->reflectCallable(function () { |
||
23 | }); |
||
24 | |||
25 | $this->assertInstanceOf(ReflectionFunction::class, $reflection); |
||
26 | } |
||
27 | |||
28 | /** |
||
29 | * @test |
||
30 | */ |
||
31 | public function canReflectMethod() |
||
32 | { |
||
33 | $resolver = new ArgumentResolver(Mockery::mock(Container::class)); |
||
34 | $reflection = $resolver->reflectCallable([ArgumentResolver::class, 'reflectCallable']); |
||
35 | |||
36 | $this->assertInstanceOf(ReflectionMethod::class, $reflection); |
||
37 | } |
||
38 | |||
39 | /** |
||
40 | * @test |
||
41 | */ |
||
42 | public function canResolveOptionalArguments() |
||
43 | { |
||
44 | $resolver = new ArgumentResolver(Mockery::mock(Container::class)); |
||
45 | $function = function (string $scalar = 'reso') { |
||
46 | return $scalar . 'lved'; |
||
47 | }; |
||
48 | $closure = $resolver->resolveArguments(new ReflectionFunction($function)); |
||
49 | $arguments = $closure(); |
||
50 | |||
51 | $this->assertSame(['reso'], $arguments); |
||
52 | $this->assertSame('resolved', $function(...$arguments)); |
||
53 | } |
||
54 | |||
55 | /** |
||
56 | * @test |
||
57 | */ |
||
58 | public function canResolveWithClassArguments() |
||
59 | { |
||
60 | $mock = Mockery::mock(TestClassContract::class); |
||
61 | $container = Mockery::mock(Container::class); |
||
62 | $container->shouldReceive('has')->with(TestClassContract::class)->andReturn(true)->once(); |
||
63 | $container->shouldReceive('get')->with(TestClassContract::class)->andReturn($mock)->once(); |
||
64 | |||
65 | $function = function (TestClassContract $test) { |
||
66 | return $test; |
||
67 | }; |
||
68 | |||
69 | $resolver = new ArgumentResolver($container); |
||
70 | $closure = $resolver->resolveArguments(new ReflectionFunction($function)); |
||
71 | |||
72 | $arguments = $closure(); |
||
73 | |||
74 | $this->assertSame([$mock], $arguments); |
||
75 | $this->assertSame($mock, $function(...$arguments)); |
||
76 | } |
||
77 | |||
78 | /** |
||
79 | * @test |
||
80 | */ |
||
81 | public function canResolveWithPassedArguments() |
||
82 | { |
||
83 | $resolver = new ArgumentResolver(Mockery::mock(Container::class)); |
||
84 | $function = function (string $scalar) { |
||
85 | return $scalar . 'd'; |
||
86 | }; |
||
87 | $closure = $resolver->resolveArguments(new ReflectionFunction($function)); |
||
88 | $arguments = $closure(['resolve']); |
||
89 | |||
90 | $this->assertSame(['resolve'], $arguments); |
||
91 | $this->assertSame('resolved', $function(...$arguments)); |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * @test |
||
96 | */ |
||
97 | public function canResolveWithoutArguments() |
||
98 | { |
||
99 | $resolver = new ArgumentResolver(Mockery::mock(Container::class)); |
||
100 | $function = function () { |
||
101 | return 'resolved'; |
||
102 | }; |
||
103 | $closure = $resolver->resolveArguments(new ReflectionFunction($function)); |
||
104 | $arguments = $closure(); |
||
105 | |||
106 | $this->assertSame([], $arguments); |
||
107 | $this->assertSame('resolved', $function(...$arguments)); |
||
108 | } |
||
109 | |||
110 | /** |
||
111 | * @test |
||
112 | * @expectedException \Venta\Container\Exception\ArgumentResolverException |
||
113 | * @expectedExceptionMessage test |
||
114 | */ |
||
115 | public function failsToResolveMandatoryClassArgumentsContainerMisses() |
||
116 | { |
||
117 | $container = Mockery::mock(Container::class); |
||
118 | $container->shouldReceive('has')->with(TestClassContract::class)->andReturn(false)->once(); |
||
119 | $container->shouldNotReceive('get'); |
||
120 | |||
121 | $function = function (TestClassContract $test) { |
||
122 | return $test; |
||
123 | }; |
||
124 | |||
125 | $resolver = new ArgumentResolver($container); |
||
126 | $closure = $resolver->resolveArguments(new ReflectionFunction($function)); |
||
127 | |||
128 | $closure(); |
||
129 | } |
||
130 | |||
131 | /** |
||
132 | * @test |
||
133 | * |
||
134 | * @expectedException \Venta\Container\Exception\ArgumentResolverException |
||
135 | * @expectedExceptionMessage scalar |
||
136 | */ |
||
137 | public function failsToResolveMandatoryScalarArguments() |
||
138 | { |
||
139 | $resolver = new ArgumentResolver(Mockery::mock(Container::class)); |
||
140 | $function = function (string $scalar) { |
||
141 | return $scalar . 'd'; |
||
142 | }; |
||
143 | $closure = $resolver->resolveArguments(new ReflectionFunction($function)); |
||
144 | $closure(); |
||
145 | } |
||
146 | |||
147 | } |
||
148 |