Mockify/Sut.pm

filter: Statementstmt 100% 66 / 66 Branchbran 100% 14 / 14 Conditioncond 70.0% 14 / 20 MC/DCmcdc 45.4% 5 / 11 Subroutinesub 100% 14 / 14 Podpod 85.7% 6 / 7 totaltotal 90.1% 119 / 132 CC 15 SCAR 10.8
CC
15
Cov
94%
mockImported
4.0
spyImported
4.0
mockConstructor
4.0
CRAP 2.9
« Mockify/TypeTests.pm ↑ Summary Mockify.pm »
1 =pod
2  
3 =head1 NAME
4  
5 Test::Mockify::Sut - injection options for your System under test (Sut) based on Mockify
6  
7 =head1 SYNOPSIS
8  
9 use Test::Mockify::Sut;
10 use Test::Mockify::Verify qw ( WasCalled );
11 use Test::Mockify::Matcher qw ( String );
12  
13 # build a new system under text
14 my $MockifySut = Test::Mockify::Sut->new('Package::I::Like::To::Test', []);
15 $MockifySut->mockImported('Package::Name', 'ImportedFunctionName')->when(String())->thenReturn('Hello');
16 $MockifySut->mockStatic('Fully::Qualified::FunctionName')->when(String())->thenReturn('Hello');
17 $MockifySut->mockConstructor('Package::Name', $Object);# hint: build this object also with Mockify
18 my $PackageILikeToTest = $MockifySut->getMockObject();
19  
20 $PackageILikeToTest->do_something();# all injections are used here
21  
22 # verify that the mocked method were called
23 ok(WasCalled($PackageILikeToTest, 'ImportedFunctionName'), 'ImportedFunctionName was called');
24 done_testing();
25  
26 =head1 DESCRIPTION
27  
28 Use L<Test::Mockify::Sut|Test::Mockify::Sut> to create and configure Sut objects. Use L<Test::Mockify::Verify|Test::Mockify::Verify> to
29 verify the interactions with your mocks.
30  
31 You can find a Example Project in L<ExampleProject|https://github.com/ChristianBreitkreutz/Mockify/tree/master/t/ExampleProject>
32  
33 =head1 METHODS
34  
35 =cut
36  
37 package Test::Mockify::Sut;
38 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 9 ▶use parent 'Test::Mockify';
Subroutine BEGIN: called CC=1 SCAR=0.0
41 9 ▶use Test::Mockify::Matcher qw (String);
Subroutine BEGIN: called CC=1 SCAR=0.0
42 9 ▶use Test::Mockify::Tools qw (Error);
Subroutine BEGIN: called CC=1 SCAR=0.0
43 9 ▶use Test::Mockify::TypeTests qw ( IsString );
Subroutine BEGIN: called CC=1 SCAR=0.0
44  
45 46 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
46 46 my $class = shift;
47 46 my $self = $class->SUPER::new(@_);
48 46 return $self;
49 }
50  
51 =pod
52  
53 =head2 mockImported
54  
55 Sometimes it is not possible to inject the dependencies from the outside. This is especially the case when the package uses imports of static functions.
56 C<mockImported> provides the possibility to mock imported functions inside the mock.
57  
58 Unlike C<mockStatic> is the injection with C<mockImported> only in the mock valid.
59  
60 =head3 synopsis
61  
62 package Show::Magician;
63 use Magic::Tools qw ( Rabbit );
64 sub pullCylinder {
65 shift;
66 if(Rabbit('white')){
67 return 1;
68 }else{
69 return 0;
70 }
71 }
72 1;
73  
74  
75 In the Test it can be mocked
76  
77 package Test_Magician;
78 use Magic::Tools qw ( Rabbit );
79 my $Mockify = Test::Mockify::Sut->new( 'Show::Magician', [] );
80 $Mockify->mockImported('Magic::Tools','Rabbit')->when(String('white'))->thenReturn(1);
81  
82 my $Magician = $Mockify->getMockObject();
83 is($Magician ->pullCylinder(), 1);
84 Rabbit('white');# return original result
85 1;
86  
87  
88 It can be mixed with normal C<spy>
89  
90 =cut
91 17 ▶sub mockImported {
Subroutine mockImported: called CC=4 SCAR=14.0
Pod documented
92 17 my $self = shift;
93 17 my @Parameters = @_;
94  
95 17 my $ParameterAmount = scalar @Parameters;
96 17 ▶ if($ParameterAmount == 2 && IsString($Parameters[0]) && IsString($Parameters[1])){
Branch: 1 branch
BranchTrueFalseTotal
if ($ParameterAmount == 2 and IsString($Parameters[0]) and IsString($Parameters[1])) { } 13 4 17
Condition66%4/6
$ParameterAmount == 2 and IsString($Parameters[0]) and IsString($Parameters[1])
!ll&&!rl&&r
4013
$ParameterAmount == 2 and IsString($Parameters[0])
!ll&&!rl&&r
4013
MC/DC33%1/3
$ParameterAmount == 2 and IsString($Parameters[0]) and IsString($Parameters[1])
$ParameterAmount == 2 IsString($Parameters[0]) IsString($Parameters[1])
Truth table50%2/4
A and B and C
A= $ParameterAmount == 2B= IsString($Parameters[0])C= IsString($Parameters[1])
ABCresult
0XX0
10X0
1100
1111
97 13 $self->{'IsImportedMockStore'}{$Parameters[1]} = {
98 'Path' => $Parameters[0],
99 'MethodName' => $Parameters[1],
100 };
101 13 return $self->_addMockWithMethod($Parameters[1]);
102 }else{
103 4 Error('"mockImported" Needs to be called with two Parameters which need to be a fully qualified path as String and the Function name. e.g. "Path::To::Your", "Function"');
104 }
105  
106 }
107 =pod
108  
109 =head2 spyImported
110  
111 C<spyImported> provides the possibility to spy imported functions inside the mock.
112  
113 Unlike C<spyStatic> is the injection with C<spyImported> only in the mock valid.
114  
115 =head3 synopsis
116  
117 package Show::Magician;
118 use Magic::Tools qw ( Rabbit );
119 sub pullCylinder {
120 shift;
121 if(Rabbit('white')){
122 return 1;
123 }else{
124 return 0;
125 }
126 }
127 1;
128  
129  
130 In the Test it can be mocked
131  
132 package Test_Magician;
133 use Magic::Tools qw ( Rabbit );
134 my $Mockify = Test::Mockify::Sut->new( 'Show::Magician', [] );
135 $Mockify->spyImported('Magic::Tools','Rabbit')->when(String());
136  
137 my $Magician = $Mockify->getMockObject();
138 is($Magician->pullCylinder(), 'SomeValue');
139 is(GetCallCount($Magician, 'Rabbit'), 1);
140 1;
141  
142 It can be mixed with normal C<spy>
143  
144 =cut
145 9 ▶sub spyImported {
Subroutine spyImported: called CC=4 SCAR=13.9
Pod documented
146 9 my $self = shift;
147 9 my @Parameters = @_;
148  
149 9 my $ParameterAmount = scalar @Parameters;
150 9 ▶ if($ParameterAmount == 2 && IsString($Parameters[0]) && IsString($Parameters[1])){
Branch: 1 branch
BranchTrueFalseTotal
if ($ParameterAmount == 2 and IsString($Parameters[0]) and IsString($Parameters[1])) { } 5 4 9
Condition66%4/6
$ParameterAmount == 2 and IsString($Parameters[0]) and IsString($Parameters[1])
!ll&&!rl&&r
405
$ParameterAmount == 2 and IsString($Parameters[0])
!ll&&!rl&&r
405
MC/DC33%1/3
$ParameterAmount == 2 and IsString($Parameters[0]) and IsString($Parameters[1])
$ParameterAmount == 2 IsString($Parameters[0]) IsString($Parameters[1])
Truth table50%2/4
A and B and C
A= $ParameterAmount == 2B= IsString($Parameters[0])C= IsString($Parameters[1])
ABCresult
0XX0
10X0
1100
1111
151 5 $self->{'IsImportedMockStore'}{$Parameters[1]} = {
152 'Path' => $Parameters[0],
153 'MethodName' => $Parameters[1],
154 };
155 5 my $PointerOriginalMethod = \&{sprintf ('%s::%s', $self->_mockedModulePath(), $Parameters[1])};
156 5 return $self->_addMockWithMethodSpy($Parameters[1], $PointerOriginalMethod);
157 }else{
158 4 Error('"spyImported" Needs to be called with two Parameters which need to be a fully qualified path as String and the Function name. e.g. "Path::To::Your", "Function"');
159 }
160  
161 }
162 =pod
163  
164 =head2 mockStatic
165  
166 Sometimes it is not possible to inject the dependencies from the outside.
167 C<mockStatic> provides the possibility to mock static functions inside the mock.
168  
169 Attention: The mocked function is valid as long as the $Mockify is defined. If You leave the scope or set the $Mockify to undef the injected method will be released.
170  
171 =head3 synopsis
172  
173 package Show::Magician;
174 use Magic::Tools;
175 sub pullCylinder {
176 shift;
177 if(Magic::Tools::Rabbit('black')){
178 return 1;
179 }else{
180 return 0;
181 }
182 }
183 1;
184  
185  
186 In the Test it can be mocked like:
187  
188 package Test_Magician;
189 { # start scope
190 my $Mockify = Test::Mockify::Sut->new( 'Show::Magician', [] );
191 $Mockify->mockStatic('Magic::Tools::Rabbit')->when(String('black'))->thenReturn(1);
192 $Mockify->spy('log')->when(String());
193 my $Magician = $Mockify->getMockObject();
194  
195 is($Magician->pullCylinder('black'), 1);
196 is(Magic::Tools::Rabbit('black'), 1);
197 } # end scope
198 is(Magic::Tools::Rabbit('black'), 'someValue'); # The orignal method in in place again
199  
200  
201 It can be mixed with normal C<spy>
202  
203 =head4 ACKNOWLEDGEMENTS
204 Thanks to @dbucky for this amazing idea
205  
206 =cut
207 22 ▶sub mockStatic {
Subroutine mockStatic: called CC=4 SCAR=13.9
Pod documented
208 22 my $self = shift;
209 22 my @Parameters = @_;
210  
211 22 my $ParameterAmount = scalar @Parameters;
212 22 ▶ if($ParameterAmount == 1 && IsString($Parameters[0])){
Branch: 1 branch
BranchTrueFalseTotal
if ($ParameterAmount == 1 and IsString($Parameters[0])) { } 20 2 22
Condition66%2/3
$ParameterAmount == 1 and IsString($Parameters[0])
!ll&&!rl&&r
2020
MC/DC50%1/2
$ParameterAmount == 1 and IsString($Parameters[0])
$ParameterAmount == 1 IsString($Parameters[0])
Truth table66%2/3
A and B
A= $ParameterAmount == 1B= IsString($Parameters[0])
ABresult
0X0
100
111
213 20 ▶ if( $Parameters[0] =~ /.*::.*/xsm ){
Branch: 1 branch
BranchTrueFalseTotal
if ($Parameters[0] =~ /.*::.*/msx) { } 18 2 20
214 18 $self->{'IsStaticMockStore'}{$Parameters[0]} = 1;
215 18 return $self->_addMockWithMethod($Parameters[0]);
216 }else{
217 2 Error("The function you like to mock needs to be defined with a fully qualified path. e.g. 'Path::To::Your::$Parameters[0]' instead of only '$Parameters[0]'");
218 }
219 }else{
220 2 Error('"mockStatic" Needs to be called with one Parameter which need to be a fully qualified path as String. e.g. "Path::To::Your::Function"');
221 }
222  
223 }
224 =pod
225  
226 =head2 spyStatic
227  
228 Provides the possibility to spy static functions around the mock.
229  
230 =head3 synopsis
231  
232 package Show::Magician;
233 sub pullCylinder {
234 shift;
235 if(Magic::Tools::Rabbit('black')){
236 return 1;
237 }else{
238 return 0;
239 }
240 }
241 1;
242  
243 In the Test it can be mocked
244  
245 package Test_Magician;
246 use Magic::Tools;
247 my $Mockify = Test::Mockify::Sut->new( 'Show::Magician', [] );
248 $Mockify->spyStatic('Magic::Tools::Rabbit')->whenAny();
249 my $Magician = $Mockify->getMockObject();
250  
251 $Magician->pullCylinder();
252 Magic::Tools::Rabbit('black');
253 is(GetCallCount($Magician, 'Magic::Tools::Rabbit'), 2); # count as long as $Mockify is valid
254  
255 1;
256  
257 It can be mixed with normal C<spy>. For more options see, C<mockStatic>
258  
259 =cut
260 9 ▶sub spyStatic {
Subroutine spyStatic: called CC=3 SCAR=11.0
Pod documented
261 9 my $self = shift;
262 9 my ($MethodName) = @_;
263 9 ▶ if(! $MethodName){
Branch: 1 branch
BranchTrueFalseTotal
unless ($MethodName) 2 7 9
264 2 Error('"spyStatic" Needs to be called with one Parameter which need to be a fully qualified path as String. e.g. "Path::To::Your::Function"');
265 }
266 7 ▶ if( $MethodName =~ /.*::.*/xsm){
Branch: 1 branch
BranchTrueFalseTotal
if ($MethodName =~ /.*::.*/msx) { } 5 2 7
267 5 $self->{'IsStaticMockStore'}{$MethodName} = 1;
268 5 my $PointerOriginalMethod = \&{$MethodName};
269 #In order to have the current object available in the parameter list, it has to be injected here.
270 7 ▶ return $self->_addMockWithMethodSpy($MethodName, sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
271 7 return $PointerOriginalMethod->(@_);
272 5 });
273 }else{
274 2 Error("The function you like to spy needs to be defined with a fully qualified path. e.g. 'Path::To::Your::$MethodName' instead of only '$MethodName'");
275 }
276 }
277 =pod
278  
279 =head2 mockConstructor
280  
281 Sometimes it is not possible to inject the dependencies from the outside. This method gives you the posibility to override the constructor of a package where your Sut depends on.
282 The defaut constructor is C<new> if you need another constructor name, use the third parameter.
283  
284 Attention: The mocked constructor is valid as long as the Mockify object is defined. If You leave the scope or set the Mockify object to undef the injected constructor will be released.
285  
286 =head3 synopsis
287  
288 package Path::To::SUT;
289 use Path::To::Package;
290 sub callToAction {
291 shift;
292 return Path::To::Package->new()->doAction();
293 }
294 1;
295  
296 In the Test it can be mocked like:
297  
298 package Test_SUT;
299 { # start scope
300 my $MockifySut = Test::Mockify::Sut->new( 'Path::To::SUT', [] );
301 $MockifySut->mockConstructor('Path::To::Package', $self->_createPathToPackage());
302 my $Test_SUT = $MockifySut->getMockObject();
303  
304 is($Test_SUT->callToAction(), 'hello');
305 } # end scope
306  
307 sub _createPathToPackage{
308 my $self = shift;
309 my $Mockify = Test::Mockify::Sut->new( 'Path::To::Package', [] );
310 $Mockify->mock('doAction')->when()->thenReturn('hello');
311 return $Mockify->getMockObject();
312 }
313  
314 It can be mixed with normal C<spy>.
315  
316 =cut
317 5 ▶sub mockConstructor {
Subroutine mockConstructor: called CC=4 SCAR=13.9
Pod documented
318 5 my $self = shift;
319 5 my ($PackageName, $Object, $ConstructorName) = @_;
320 5 ▶ $ConstructorName //= 'new';
Condition100%2/2
$ConstructorName //= "new"
l!l
14
MC/DC100%1/1
$ConstructorName //= "new"
$ConstructorName
Truth table100%2/2
A
A= $ConstructorName
Aresult
11
00
321 5 ▶ if($PackageName && IsString($PackageName)){
Branch: 1 branch
BranchTrueFalseTotal
if ($PackageName and IsString($PackageName)) { } 4 1 5
Condition66%2/3
$PackageName and IsString($PackageName)
!ll&&!rl&&r
104
MC/DC50%1/2
$PackageName and IsString($PackageName)
$PackageName IsString($PackageName)
Truth table66%2/3
A and B
A= $PackageNameB= IsString($PackageName)
ABresult
0X0
100
111
322 #note for my self: It is not possible to use thenReturn one level up since 'mockStatic' will mock the constructor before this line has finished.
323 # It is impossible to create an instance with the already mocked constructor.
324 4 return $self->mockStatic( sprintf('%s::%s', $PackageName, $ConstructorName) )->whenAny()->thenReturn($Object);
325 }else{
326 1 Error('Wrong or missing parameter list. Please use it like: $Mockify->mockConstructor(\'Path::To::Package\', $Object, \'new\')'); ## no critic (RequireInterpolationOfMetachars)
327 }
328 }
329 =pod
330  
331 =head2 getVerificationObject
332  
333 Provides the actual mock object, which you can use for verification.
334 This is code sugar for the method C<getMockObject>.
335  
336 my $Mockify = Test::Mockify::Sut->new( 'My::Module', [] );
337 my $VerificationObject = $Mockify->getVerificationObject();
338 ok(WasCalled($VerificationObject, 'FunctionName'));
339 =cut
340 4 ▶sub getVerificationObject{
Subroutine getVerificationObject: called CC=1 SCAR=0.0
Pod documented
341 4 my $self = shift;
342 4 return $self->getMockObject();
343 }
344  
345 9 1;
« Mockify/TypeTests.pm ↑ Summary Mockify.pm »