| 1 | =pod | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | =head1 NAME | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | Test::Mockify - minimal mocking framework for perl | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | use Test::Mockify; | ||||||||||||||||||||||||||||
| 10 | use Test::Mockify::Verify qw ( WasCalled ); | ||||||||||||||||||||||||||||
| 11 | use Test::Mockify::Matcher qw ( String ); | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | # build a new mocked object | ||||||||||||||||||||||||||||
| 14 | my $MockObjectBuilder = Test::Mockify->new('SampleLogger', []); | ||||||||||||||||||||||||||||
| 15 | $MockObjectBuilder->mock('log')->when(String())->thenReturnUndef(); | ||||||||||||||||||||||||||||
| 16 | my $MockedLogger = $MockLoggerBuilder->getMockObject(); | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | # inject mocked object into the code you want to test | ||||||||||||||||||||||||||||
| 19 | my $App = SampleApp->new('logger'=> $MockedLogger); | ||||||||||||||||||||||||||||
| 20 | $App->do_something(); | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | # verify that the mocked method was called | ||||||||||||||||||||||||||||
| 23 | ok(WasCalled($MockedLogger, 'log'), 'log was called'); | ||||||||||||||||||||||||||||
| 24 | done_testing(); | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | Use L<Test::Mockify|Test::Mockify> to create and configure mock objects. Use L<Test::Mockify::Verify|Test::Mockify::Verify> to | ||||||||||||||||||||||||||||
| 29 | verify the interactions with your mocks. Use L<Test::Mockify::Sut|Test::Mockify::Sut> to inject dependencies into your Sut. | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | You can find a Example Project in L<ExampleProject|https://github.com/ChristianBreitkreutz/Mockify/tree/master/t/ExampleProject> | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | It is possible to use alternative constructor name | ||||||||||||||||||||||||||||
| 34 | my $MockObjectBuilder = Test::Mockify->new('SampleLogger', [], 'create'); | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | =head1 METHODS | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | =cut | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | package Test::Mockify; | ||||||||||||||||||||||||||||
| 41 | 17 | ▶ | use Test::Mockify::Tools qw ( Error ExistsMethod LoadPackage ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 42 | 17 | ▶ | use Test::Mockify::TypeTests qw ( IsString IsArrayReference); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 43 | 17 | ▶ | use Test::Mockify::MethodCallCounter; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 44 | 17 | ▶ | use Test::Mockify::Method; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 45 | 17 | ▶ | use Test::Mockify::MethodSpy; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 46 | 17 | ▶ | use Test::MockObject::Extends; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 47 | 17 | ▶ | use Scalar::Util qw( blessed ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 48 | 17 | ▶ | use Sub::Override; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 17 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 17 | our $VERSION = '2.4'; | |||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 114 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=5 SCAR=16.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 55 | 114 | my $class = shift; | |||||||||||||||||||||||||||
| 56 | 114 | my ( $FakeModulePath, $aFakeParams, $AlternativeConstructorName ) = @_; | |||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 114 | my $self = bless {}, $class; | |||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 114 | ▶ | $AlternativeConstructorName //= 'new'; | ||||||||||||||||||||||||||
|
Condition100%2/2
$AlternativeConstructorName //= "new"
MC/DC100%1/1
$AlternativeConstructorName //= "new"
$AlternativeConstructorName
Truth table100%2/2
A
A= $AlternativeConstructorName
| |||||||||||||||||||||||||||||
| 61 | 114 | LoadPackage( $FakeModulePath ); | |||||||||||||||||||||||||||
| 62 | 114 | ▶ | if(!$FakeModulePath->can($AlternativeConstructorName)){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | 27 | ▶ | if(defined $aFakeParams ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | 1 | Error("'$FakeModulePath' have no constructor. If you like to create a mock of a package without constructor please use it without parameter list"); | |||||||||||||||||||||||||||
| 65 | }else{ | ||||||||||||||||||||||||||||
| 66 | 26 | $self->{'MockStaticModule'} = 1; | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | 113 | ▶ | my $FakeClass = $aFakeParams ? $FakeModulePath->$AlternativeConstructorName( @{$aFakeParams} ) : $FakeModulePath; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | 113 | $self->_mockedModulePath($FakeModulePath); | |||||||||||||||||||||||||||
| 71 | 113 | $self->_mockedSelf(Test::MockObject::Extends->new( $FakeClass )); | |||||||||||||||||||||||||||
| 72 | 113 | $self->_initMockedModule(); | |||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 113 | return $self; | |||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | #---------------------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 78 | 273 | ▶ | sub _mockedModulePath { | ||||||||||||||||||||||||||
|
Subroutine
_mockedModulePath: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 79 | 273 | my $self = shift; | |||||||||||||||||||||||||||
| 80 | 273 | my ($ModulePath) = @_; | |||||||||||||||||||||||||||
| 81 | 273 | ▶ | return $self->{'MockedModulePath'} unless ($ModulePath); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | 113 | $self->{'MockedModulePath'} = $ModulePath; | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | #---------------------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 85 | 905 | ▶ | sub _mockedSelf { | ||||||||||||||||||||||||||
|
Subroutine
_mockedSelf: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 86 | 905 | my $self = shift; | |||||||||||||||||||||||||||
| 87 | 905 | my ($MockedSelf) = @_; | |||||||||||||||||||||||||||
| 88 | 905 | ▶ | return $self->{'MockedModule'} unless ($MockedSelf); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | 113 | $self->{'MockedModule'} = $MockedSelf; | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | #---------------------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 92 | 113 | ▶ | sub _initMockedModule { | ||||||||||||||||||||||||||
|
Subroutine
_initMockedModule: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 93 | 113 | my $self = shift; | |||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 113 | $self->_mockedSelf()->{'__MethodCallCounter'} = Test::Mockify::MethodCallCounter->new(); | |||||||||||||||||||||||||||
| 96 | 113 | $self->_mockedSelf()->{'__isMockified'} = 1; | |||||||||||||||||||||||||||
| 97 | 113 | $self->_addGetParameterFromMockifyCall(); | |||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 113 | $self->{'__override'} = Sub::Override->new(); | |||||||||||||||||||||||||||
| 100 | 113 | $self->_mockedSelf()->{'__override'} = $self->{'__override'}; | |||||||||||||||||||||||||||
| 101 | 113 | $self->{'IsStaticMockStore'} = undef; | |||||||||||||||||||||||||||
| 102 | 113 | $self->{'IsImportedMockStore'} = undef; | |||||||||||||||||||||||||||
| 103 | 113 | return; | |||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | #---------------------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 107 | =pod | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | =head2 getMockObject | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | Provides the actual mock object, which you can use in the test. | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | my $aParameterList = ['SomeValueForConstructor']; | ||||||||||||||||||||||||||||
| 114 | my $MockObjectBuilder = Test::Mockify->new( 'My::Module', $aParameterList ); | ||||||||||||||||||||||||||||
| 115 | my $MyModuleObject = $MockObjectBuilder->getMockObject(); | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | =cut | ||||||||||||||||||||||||||||
| 118 | 98 | ▶ | sub getMockObject { | ||||||||||||||||||||||||||
|
Subroutine
getMockObject: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 119 | 98 | my $self = shift; | |||||||||||||||||||||||||||
| 120 | 98 | return $self->_mockedSelf(); | |||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | #----------------------------------------------------------------------------------------= | ||||||||||||||||||||||||||||
| 124 | =pod | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | =head2 mock | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | This is the place where the mocked methods are defined. The method also proves that the method you like to mock actually exists. | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | =head3 synopsis | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | This method takes one parameter, which is the name of the method you like to mock. | ||||||||||||||||||||||||||||
| 133 | Because you need to specify more detailed the behaviour of this mock you have to chain the method signature (when) and the expected return value (then...). | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | For example, the next line will create a mocked version of the method log, but only if this method is called with any string and the number 123. In this case it will return the String 'Hello World'. Mockify will throw an error if this method is called somehow else. | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | my $MockObjectBuilder = Test::Mockify->new( 'Sample::Logger', [] ); | ||||||||||||||||||||||||||||
| 138 | $MockObjectBuilder->mock('log')->when(String(), Number(123))->thenReturn('Hello World'); | ||||||||||||||||||||||||||||
| 139 | my $SampleLogger = $MockObjectBuilder->getMockObject(); | ||||||||||||||||||||||||||||
| 140 | is($SampleLogger->log('abc',123), 'Hello World'); | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | =head4 when | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | To define the signature in the needed structure you must use the L<Test::Mockify::Matcher|Test::Mockify::Matcher>. | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | =head4 whenAny | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | If you don't want to specify the method signature at all, you can use whenAny. | ||||||||||||||||||||||||||||
| 150 | It is not possible to mix C<whenAny> and C<when> for the same method. | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | =head4 then ... | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | For possible return types please look in L<Test::Mockify::ReturnValue|Test::Mockify::ReturnValue> | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | =cut | ||||||||||||||||||||||||||||
| 157 | 66 | ▶ | sub mock { | ||||||||||||||||||||||||||
|
Subroutine
mock: called CC=3 SCAR=12.6
Pod
documented
| |||||||||||||||||||||||||||||
| 158 | 66 | my $self = shift; | |||||||||||||||||||||||||||
| 159 | 66 | my @Parameters = @_; | |||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | 66 | my $ParameterAmount = scalar @Parameters; | |||||||||||||||||||||||||||
| 162 | 66 | ▶ | if($ParameterAmount == 1 && IsString($Parameters[0]) ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$ParameterAmount == 1 and IsString($Parameters[0])
MC/DC0%0/2
$ParameterAmount == 1 and IsString($Parameters[0])
$ParameterAmount == 1
IsString($Parameters[0])
Truth table33%1/3
A and B
A= $ParameterAmount == 1B= IsString($Parameters[0])
| |||||||||||||||||||||||||||||
| 163 | 66 | $self->{'__UsedSubMock'} = 1; | |||||||||||||||||||||||||||
| 164 | 66 | return $self->_addMockWithMethod($Parameters[0]); | |||||||||||||||||||||||||||
| 165 | }else{ | ||||||||||||||||||||||||||||
| 166 | 0 | Error('"mock" Needs to be called with one Parameter which needs to be a String. '); | |||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||
| 168 | 0 | return; | |||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||
| 170 | #---------------------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | =pod | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | =head2 spy | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | Use spy if you want to observe a method. You can use the L<Test::Mockify::Verify|Test::Mockify::Verify> to ensure that the method was called with the expected parameters. | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | =head3 synopsis | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | This method takes one parameter, which is the name of the method you like to spy. | ||||||||||||||||||||||||||||
| 185 | Because you need to specify more detailed the behaviour of this spy you have to define the method signature with C<when> | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | For example, the next line will create a method spy of the method log, but only if this method is called with any string and the number 123. Mockify will throw an error if this method is called in another way. | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | my $MockObjectBuilder = Test::Mockify->new( 'Sample::Logger', [] ); | ||||||||||||||||||||||||||||
| 190 | $MockObjectBuilder->spy('log')->when(String(), Number(123)); | ||||||||||||||||||||||||||||
| 191 | my $SampleLogger = $MockObjectBuilder->getMockObject(); | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | # call spied method | ||||||||||||||||||||||||||||
| 194 | $SampleLogger->log('abc', 123); | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | # verify that the spied method was called | ||||||||||||||||||||||||||||
| 197 | is_deeply(GetParametersFromMockifyCall($MockedLogger, 'log'),['abc', 123], 'Check parameters of first call'); | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | =head4 when | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | To define the signature in the needed structure you must use the L<Test::Mockify::Matcher|Test::Mockify::Matcher>. | ||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | =head4 whenAny | ||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | If you don't want to specify the method signature at all, you can use whenAny. | ||||||||||||||||||||||||||||
| 206 | It is not possible to mix C<whenAny> and C<when> for the same method. | ||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | =cut | ||||||||||||||||||||||||||||
| 209 | 16 | ▶ | sub spy { | ||||||||||||||||||||||||||
|
Subroutine
spy: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 210 | 16 | my $self = shift; | |||||||||||||||||||||||||||
| 211 | 16 | my ($MethodName) = @_; | |||||||||||||||||||||||||||
| 212 | 16 | my $PointerOriginalMethod = \&{sprintf ('%s::%s', $self->_mockedModulePath(), $MethodName)}; | |||||||||||||||||||||||||||
| 213 | #In order to have the current object available in the parameter list, it has to be injected here. | ||||||||||||||||||||||||||||
| 214 | 15 | ▶ | return $self->_addMockWithMethodSpy($MethodName, sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 215 | 15 | return $PointerOriginalMethod->($self->_mockedSelf(), @_); | |||||||||||||||||||||||||||
| 216 | 16 | }); | |||||||||||||||||||||||||||
| 217 | } | ||||||||||||||||||||||||||||
| 218 | #---------------------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 219 | 97 | ▶ | sub _addMockWithMethod { | ||||||||||||||||||||||||||
|
Subroutine
_addMockWithMethod: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 220 | 97 | my $self = shift; | |||||||||||||||||||||||||||
| 221 | 97 | my ( $MethodName ) = @_; | |||||||||||||||||||||||||||
| 222 | 97 | $self->_testMockTypeUsage($MethodName); | |||||||||||||||||||||||||||
| 223 | 96 | ▶ | if($self->{'IsStaticMockStore'}{$MethodName}){ | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 224 | 18 | return $self->_addStaticMock($MethodName, Test::Mockify::Method->new()); | |||||||||||||||||||||||||||
| 225 | }elsif($self->{'IsImportedMockStore'}{$MethodName}){ | ||||||||||||||||||||||||||||
| 226 | 13 | return $self->_addImportedMock($MethodName, Test::Mockify::Method->new()); | |||||||||||||||||||||||||||
| 227 | }else{ | ||||||||||||||||||||||||||||
| 228 | 65 | return $self->_addMock($MethodName, Test::Mockify::Method->new()); | |||||||||||||||||||||||||||
| 229 | } | ||||||||||||||||||||||||||||
| 230 | } | ||||||||||||||||||||||||||||
| 231 | #---------------------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 232 | 26 | ▶ | sub _addMockWithMethodSpy { | ||||||||||||||||||||||||||
|
Subroutine
_addMockWithMethodSpy: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 233 | 26 | my $self = shift; | |||||||||||||||||||||||||||
| 234 | 26 | my ( $MethodName, $PointerOriginalMethod ) = @_; | |||||||||||||||||||||||||||
| 235 | 26 | $self->_testMockTypeUsage($MethodName); | |||||||||||||||||||||||||||
| 236 | 25 | ▶ | if($self->{'IsStaticMockStore'}{$MethodName}){ | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 237 | 5 | return $self->_addStaticMock($MethodName, Test::Mockify::MethodSpy->new($PointerOriginalMethod)); | |||||||||||||||||||||||||||
| 238 | }elsif($self->{'IsImportedMockStore'}{$MethodName}){ | ||||||||||||||||||||||||||||
| 239 | 5 | return $self->_addImportedMock($MethodName, Test::Mockify::MethodSpy->new($PointerOriginalMethod)); | |||||||||||||||||||||||||||
| 240 | }else{ | ||||||||||||||||||||||||||||
| 241 | 15 | return $self->_addMock($MethodName, Test::Mockify::MethodSpy->new($PointerOriginalMethod)); | |||||||||||||||||||||||||||
| 242 | } | ||||||||||||||||||||||||||||
| 243 | } | ||||||||||||||||||||||||||||
| 244 | #------------------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 245 | 80 | ▶ | sub _addMock { | ||||||||||||||||||||||||||
|
Subroutine
_addMock: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 246 | 80 | my $self = shift; | |||||||||||||||||||||||||||
| 247 | 80 | my ($MethodName, $Method) = @_; | |||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||
| 249 | 80 | ExistsMethod( $self->_mockedModulePath(), $MethodName ); | |||||||||||||||||||||||||||
| 250 | 79 | $self->_mockedSelf()->{'__MethodCallCounter'}->addMethod( $MethodName ); | |||||||||||||||||||||||||||
| 251 | 79 | ▶ | if(not $self->{'MethodStore'}{$MethodName}){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 252 | 69 | ▶ | if($self->{'MockStaticModule'}){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 253 | 1 | return $self->_addStaticMock($MethodName, Test::Mockify::Method->new()); | |||||||||||||||||||||||||||
| 254 | }else{ | ||||||||||||||||||||||||||||
| 255 | 68 | ▶ | $self->{'MethodStore'}{$MethodName} //= $Method; | ||||||||||||||||||||||||||
|
Condition33%1/3
$self->{"MethodStore"}{$MethodName} //= $Method
MC/DC0%0/2
$self->{"MethodStore"}{$MethodName} //= $Method
$self->{"MethodStore"}{$MethodName}
$Method
Truth table33%1/3
A //= B
A= $self->{"MethodStore"}{$MethodName}B= $Method
| |||||||||||||||||||||||||||||
| 256 | 82 | ▶ | $self->_mockedSelf()->mock($MethodName, sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 257 | 82 | my $MockedSelf = shift; | |||||||||||||||||||||||||||
| 258 | 82 | $MockedSelf->{'__MethodCallCounter'}->increment( $MethodName ); | |||||||||||||||||||||||||||
| 259 | 82 | my @MockedParameters = @_; | |||||||||||||||||||||||||||
| 260 | 82 | push @{$MockedSelf->{$MethodName.'_MockifyParams'}}, \@MockedParameters; | |||||||||||||||||||||||||||
| 261 | 82 | ▶ | my $WantAList = wantarray ? 1 : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 262 | 82 | return _callInjectedMethod($Method, \@MockedParameters, $WantAList, $MethodName); | |||||||||||||||||||||||||||
| 263 | 68 | }); | |||||||||||||||||||||||||||
| 264 | } | ||||||||||||||||||||||||||||
| 265 | } | ||||||||||||||||||||||||||||
| 266 | 78 | return $self->{'MethodStore'}{$MethodName}; | |||||||||||||||||||||||||||
| 267 | } | ||||||||||||||||||||||||||||
| 268 | #---------------------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 269 | 138 | ▶ | sub _callInjectedMethod { | ||||||||||||||||||||||||||
|
Subroutine
_callInjectedMethod: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 270 | # my $self = shift; #In Order to keep the mockify object out of the mocked method, I can't use the self. | ||||||||||||||||||||||||||||
| 271 | 138 | my ($Method, $aMockedParameters, $WantAList, $MethodName) = @_; | |||||||||||||||||||||||||||
| 272 | 138 | my $ReturnValue; | |||||||||||||||||||||||||||
| 273 | my @ReturnValue; | ||||||||||||||||||||||||||||
| 274 | 138 | eval { | |||||||||||||||||||||||||||
| 275 | 138 | ▶ | if($WantAList){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 276 | 16 | @ReturnValue = $Method->call(@{$aMockedParameters}); | |||||||||||||||||||||||||||
| 277 | }else{ | ||||||||||||||||||||||||||||
| 278 | 122 | $ReturnValue = $Method->call(@{$aMockedParameters}); | |||||||||||||||||||||||||||
| 279 | } | ||||||||||||||||||||||||||||
| 280 | }; | ||||||||||||||||||||||||||||
| 281 | # $@ -> current error | ||||||||||||||||||||||||||||
| 282 | 138 | ▶ | if ($@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 283 | 19 | Error("\nError when calling method '$MethodName'\n".$@) | |||||||||||||||||||||||||||
| 284 | } | ||||||||||||||||||||||||||||
| 285 | 119 | ▶ | if($WantAList){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 286 | 16 | return @ReturnValue; | |||||||||||||||||||||||||||
| 287 | }else{ | ||||||||||||||||||||||||||||
| 288 | 103 | return $ReturnValue; | |||||||||||||||||||||||||||
| 289 | } | ||||||||||||||||||||||||||||
| 290 | 0 | return; | |||||||||||||||||||||||||||
| 291 | } | ||||||||||||||||||||||||||||
| 292 | #---------------------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 293 | 38 | ▶ | sub _buildMockSub{ | ||||||||||||||||||||||||||
|
Subroutine
_buildMockSub: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 294 | 38 | my $self = shift; | |||||||||||||||||||||||||||
| 295 | 38 | my ($MockedSelf, $MethodName, $Method) = @_; | |||||||||||||||||||||||||||
| 296 | # The Sub::Override lexical scope don't get released if there is any Mockify var is pointing to it. | ||||||||||||||||||||||||||||
| 297 | # So the $MockedSelf needs to be resolved outside of the sub lexical scope. | ||||||||||||||||||||||||||||
| 298 | 38 | my $MethodCallCounter = \$MockedSelf->{'__MethodCallCounter'}; | |||||||||||||||||||||||||||
| 299 | 38 | my $MockifyParamsStore = \$MockedSelf->{$MethodName.'_MockifyParams'}; | |||||||||||||||||||||||||||
| 300 | 38 | my $MustUseShift = $self->{'__UsedSubMock'}; | |||||||||||||||||||||||||||
| 301 | 56 | ▶ | return sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 302 | 56 | ▶ | shift @_ if($MustUseShift); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 303 | 56 | ${$MethodCallCounter}->increment( $MethodName ); | |||||||||||||||||||||||||||
| 304 | 56 | my @MockedParameters = @_; | |||||||||||||||||||||||||||
| 305 | 56 | push( @{${$MockifyParamsStore}}, \@MockedParameters ); | |||||||||||||||||||||||||||
| 306 | 56 | ▶ | my $WantAList = wantarray ? 1 : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 307 | 56 | return _callInjectedMethod($Method, \@MockedParameters, $WantAList, $MethodName); | |||||||||||||||||||||||||||
| 308 | 38 | }; | |||||||||||||||||||||||||||
| 309 | } | ||||||||||||||||||||||||||||
| 310 | #---------------------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 311 | 24 | ▶ | sub _addStaticMock { | ||||||||||||||||||||||||||
|
Subroutine
_addStaticMock: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 312 | 24 | my $self = shift; | |||||||||||||||||||||||||||
| 313 | 24 | my ( $MethodName, $Method) = @_; | |||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||
| 315 | 24 | ExistsMethod( $self->_mockedModulePath(), $MethodName ); | |||||||||||||||||||||||||||
| 316 | 24 | $self->_mockedSelf()->{'__MethodCallCounter'}->addMethod( $MethodName ); | |||||||||||||||||||||||||||
| 317 | 24 | ▶ | if(not $self->{'MethodStore'}{$MethodName}){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 318 | 22 | $self->{'MethodStore'}{$MethodName} = $Method; | |||||||||||||||||||||||||||
| 319 | 22 | my $MockedSelf = $self->_mockedSelf(); | |||||||||||||||||||||||||||
| 320 | 22 | my $MockedMethodBody = $self->_buildMockSub($MockedSelf, $MethodName, $Method); | |||||||||||||||||||||||||||
| 321 | 22 | ▶ | if(!($MethodName =~ qr/::/sm)){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 322 | 1 | $self->_overrideInternalFunction($MethodName, $MockedMethodBody); | |||||||||||||||||||||||||||
| 323 | }else{ | ||||||||||||||||||||||||||||
| 324 | 21 | $self->_overrideExternalFunction($MethodName, $MockedMethodBody); | |||||||||||||||||||||||||||
| 325 | } | ||||||||||||||||||||||||||||
| 326 | } | ||||||||||||||||||||||||||||
| 327 | 24 | return $self->{'MethodStore'}{$MethodName}; | |||||||||||||||||||||||||||
| 328 | } | ||||||||||||||||||||||||||||
| 329 | #---------------------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 330 | 1 | ▶ | sub _overrideInternalFunction { | ||||||||||||||||||||||||||
|
Subroutine
_overrideInternalFunction: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 331 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 332 | 1 | my ($MethodName, $MockedMethodBody) = @_; | |||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||
| 334 | 1 | my $FullyQualifiedMethodName = sprintf('%s::%s', $self->_mockedModulePath(), $MethodName); | |||||||||||||||||||||||||||
| 335 | 1 | $self->_replaceWithPrototype($FullyQualifiedMethodName, $MockedMethodBody); | |||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||
| 337 | 1 | return; | |||||||||||||||||||||||||||
| 338 | } | ||||||||||||||||||||||||||||
| 339 | #---------------------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 340 | 21 | ▶ | sub _overrideExternalFunction { | ||||||||||||||||||||||||||
|
Subroutine
_overrideExternalFunction: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 341 | 21 | my $self = shift; | |||||||||||||||||||||||||||
| 342 | 21 | my ($FullyQualifiedMethodName, $MockedMethodBody) = @_; | |||||||||||||||||||||||||||
| 343 | 21 | $self->_replaceWithPrototype($FullyQualifiedMethodName, $MockedMethodBody); | |||||||||||||||||||||||||||
| 344 | 21 | return; | |||||||||||||||||||||||||||
| 345 | } | ||||||||||||||||||||||||||||
| 346 | #---------------------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 347 | 38 | ▶ | sub _replaceWithPrototype { | ||||||||||||||||||||||||||
|
Subroutine
_replaceWithPrototype: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 348 | 38 | my $self = shift; | |||||||||||||||||||||||||||
| 349 | 38 | my ($FullyQualifiedMethodName, $Sub) = @_; | |||||||||||||||||||||||||||
| 350 | 38 | ▶ | Error ('not a code ref ') unless ref $Sub eq 'CODE'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 351 | 38 | my $Prototype = prototype($FullyQualifiedMethodName); | |||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||
| 353 | 38 | ▶ | if(defined $Prototype){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 354 | 33 | my $SubWithPrototype = eval( 'return sub ('. $Prototype .') {$Sub->(@_)}'); ## no critic (ProhibitStringyEval RequireInterpolationOfMetachars ) This is the only dynamic way to add the prototype | |||||||||||||||||||||||||||
| 355 | 33 | ▶ | Error($@, {'Error in eval, line' => __LINE__ - 1}) if($@); # Rethrow error if something went wrong in the eval. (For debugging) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 356 | 33 | $self->{'__override'}->replace($FullyQualifiedMethodName, $SubWithPrototype); | |||||||||||||||||||||||||||
| 357 | 33 | return; | |||||||||||||||||||||||||||
| 358 | } | ||||||||||||||||||||||||||||
| 359 | 5 | $self->{'__override'}->replace($FullyQualifiedMethodName, $Sub); | |||||||||||||||||||||||||||
| 360 | 5 | return; | |||||||||||||||||||||||||||
| 361 | } | ||||||||||||||||||||||||||||
| 362 | #---------------------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 363 | 18 | ▶ | sub _addImportedMock { | ||||||||||||||||||||||||||
|
Subroutine
_addImportedMock: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 364 | 18 | my $self = shift; | |||||||||||||||||||||||||||
| 365 | 18 | my ( $MethodName, $Method) = @_; | |||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||
| 367 | ExistsMethod( | ||||||||||||||||||||||||||||
| 368 | $self->{'IsImportedMockStore'}{$MethodName}->{'Path'}, | ||||||||||||||||||||||||||||
| 369 | 18 | $self->{'IsImportedMockStore'}{$MethodName}->{'MethodName'}, | |||||||||||||||||||||||||||
| 370 | {'Mock Imported In' => $self->_mockedModulePath()} | ||||||||||||||||||||||||||||
| 371 | ); | ||||||||||||||||||||||||||||
| 372 | |||||||||||||||||||||||||||||
| 373 | 18 | $self->_mockedSelf()->{'__MethodCallCounter'}->addMethod( $MethodName ); | |||||||||||||||||||||||||||
| 374 | 18 | ▶ | if(not $self->{'MethodStore'}{$MethodName}){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 375 | 16 | $self->{'MethodStore'}{$MethodName} = $Method; | |||||||||||||||||||||||||||
| 376 | 16 | my $MockedSelf = $self->_mockedSelf(); | |||||||||||||||||||||||||||
| 377 | 16 | my $MockedMethodBody = $self->_buildMockSub($MockedSelf, $MethodName, $Method); | |||||||||||||||||||||||||||
| 378 | 16 | my $FullyQualifiedMethodName = sprintf ('%s::%s', $self->_mockedModulePath(), $self->{'IsImportedMockStore'}{$MethodName}->{'MethodName'}); | |||||||||||||||||||||||||||
| 379 | 16 | $self->_replaceWithPrototype( $FullyQualifiedMethodName, $MockedMethodBody ); | |||||||||||||||||||||||||||
| 380 | } | ||||||||||||||||||||||||||||
| 381 | 18 | return $self->{'MethodStore'}{$MethodName}; | |||||||||||||||||||||||||||
| 382 | } | ||||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||
| 384 | #---------------------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 385 | 113 | ▶ | sub _addGetParameterFromMockifyCall { | ||||||||||||||||||||||||||
|
Subroutine
_addGetParameterFromMockifyCall: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 386 | 113 | my $self = shift; | |||||||||||||||||||||||||||
| 387 | |||||||||||||||||||||||||||||
| 388 | $self->_mockedSelf()->mock('__getParametersFromMockifyCall', | ||||||||||||||||||||||||||||
| 389 | 20 | ▶ | sub{ | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 390 | 20 | my $MockedSelf = shift; | |||||||||||||||||||||||||||
| 391 | 20 | my ( $MethodName, $Position ) = @_; | |||||||||||||||||||||||||||
| 392 | |||||||||||||||||||||||||||||
| 393 | 20 | my $aParametersFromAllCalls = $MockedSelf->{$MethodName.'_MockifyParams'}; | |||||||||||||||||||||||||||
| 394 | 20 | ▶ | if( ref $aParametersFromAllCalls ne 'ARRAY' ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 395 | 1 | Error( "$MethodName was not called" ); | |||||||||||||||||||||||||||
| 396 | } | ||||||||||||||||||||||||||||
| 397 | 19 | ▶ | if( scalar @{$aParametersFromAllCalls} < $Position ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 398 | 1 | Error( "$MethodName was not called ".( $Position+1 ).' times',{ | |||||||||||||||||||||||||||
| 399 | 'Method' => "$MethodName", | ||||||||||||||||||||||||||||
| 400 | 'Postion' => $Position, | ||||||||||||||||||||||||||||
| 401 | } ); | ||||||||||||||||||||||||||||
| 402 | } | ||||||||||||||||||||||||||||
| 403 | else { | ||||||||||||||||||||||||||||
| 404 | 18 | my $ParameterFromMockifyCall = $MockedSelf->{$MethodName.'_MockifyParams'}[$Position]; | |||||||||||||||||||||||||||
| 405 | 18 | return $ParameterFromMockifyCall; | |||||||||||||||||||||||||||
| 406 | } | ||||||||||||||||||||||||||||
| 407 | 0 | return; | |||||||||||||||||||||||||||
| 408 | } | ||||||||||||||||||||||||||||
| 409 | 113 | ); | |||||||||||||||||||||||||||
| 410 | |||||||||||||||||||||||||||||
| 411 | 113 | return; | |||||||||||||||||||||||||||
| 412 | } | ||||||||||||||||||||||||||||
| 413 | #---------------------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 414 | 123 | ▶ | sub _testMockTypeUsage { | ||||||||||||||||||||||||||
|
Subroutine
_testMockTypeUsage: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 415 | 123 | my $self = shift; | |||||||||||||||||||||||||||
| 416 | 123 | my ($MethodName) = @_; | |||||||||||||||||||||||||||
| 417 | 123 | my $PositionInCallerStack = 2; | |||||||||||||||||||||||||||
| 418 | 123 | my $MethodMockType = (caller($PositionInCallerStack))[3]; # autodetect mock type (spy or mock) | |||||||||||||||||||||||||||
| 419 | 123 | ▶ | if($self->{'MethodMockType'}{$MethodName} && $self->{'MethodMockType'}{$MethodName} ne $MethodMockType){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->{"MethodMockType"}{$MethodName} and $self->{"MethodMockType"}{$MethodName} ne $MethodMockType
MC/DC100%2/2
$self->{"MethodMockType"}{$MethodName} and $self->{"MethodMockType"}{$MethodName} ne $MethodMockType
$self->{"MethodMockType"}{$MethodName}
$self->{"MethodMockType"}{$MethodName} ne $MethodMockType
Truth table100%3/3
A and B
A= $self->{"MethodMockType"}{$MethodName}B= $self->{"MethodMockType"}{$MethodName} ne $MethodMockType
| |||||||||||||||||||||||||||||
| 420 | 2 | Error('It is not possible to mix spy and mock'); | |||||||||||||||||||||||||||
| 421 | }else{ | ||||||||||||||||||||||||||||
| 422 | 121 | $self->{'MethodMockType'}{$MethodName} = $MethodMockType; | |||||||||||||||||||||||||||
| 423 | } | ||||||||||||||||||||||||||||
| 424 | 121 | return; | |||||||||||||||||||||||||||
| 425 | } | ||||||||||||||||||||||||||||
| 426 | 17 | 1; | |||||||||||||||||||||||||||
| 427 | |||||||||||||||||||||||||||||
| 428 | __END__ | ||||||||||||||||||||||||||||