| 1 | =pod | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | =head1 NAME | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | Test::Mockify::Verify - To verify mock interactions | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | Sometimes you will need to verify that a specific method was called a certain amount of times, each time with specific parameters. | ||||||||||||||||||||||||||||
| 10 | Mockify provides the following methods to access this data. | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | =head1 METHODS | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | =cut | ||||||||||||||||||||||||||||
| 15 | package Test::Mockify::Verify; | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 14 | ▶ | use Test::Mockify::Tools qw ( Error IsValid ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 14 | ▶ | use Test::Mockify::TypeTests qw ( IsInteger ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | 14 | ▶ | use Scalar::Util qw( blessed ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 14 | ▶ | use base qw ( Exporter ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 14 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | 14 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 14 | our @EXPORT_OK = qw ( | |||||||||||||||||||||||||||
| 27 | GetParametersFromMockifyCall | ||||||||||||||||||||||||||||
| 28 | WasCalled | ||||||||||||||||||||||||||||
| 29 | GetCallCount | ||||||||||||||||||||||||||||
| 30 | ); | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | #----------------------------------------------------------------------------------------= | ||||||||||||||||||||||||||||
| 33 | =pod | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | =head2 GetParametersFromMockifyCall | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | my $aParameters = GetParametersFromMockifyCall($MockifiedObject, 'nameOfMethod', $OptionalPosition); | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | This function returns all the parameters after the I<mockified> module was used. If the test calls the method multiple times, the "$OptionalPosition" can be used to get the specific call. The default is "0". | ||||||||||||||||||||||||||||
| 40 | Returns an array ref with the parameters of the specific method call. | ||||||||||||||||||||||||||||
| 41 | I<(Note: The calls are counted starting from zero. You will get the parameters from the first call with 0, the ones from the second call with 1, and so on.)> | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | =cut | ||||||||||||||||||||||||||||
| 44 | 23 | ▶ | sub GetParametersFromMockifyCall { | ||||||||||||||||||||||||||
|
Subroutine
GetParametersFromMockifyCall: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 45 | 23 | my ( $MockifiedMockedObject, $MethodName, $Position ) = @_; | |||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 23 | ▶ | if( not blessed $MockifiedMockedObject){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | 1 | Error('The first argument must be blessed'); | |||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | 22 | my $PackageName = ref($MockifiedMockedObject); | |||||||||||||||||||||||||||
| 51 | 22 | ▶ | if( not IsValid( $MethodName )){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | 1 | Error('Method name must be specified', {'Position'=>$Position, 'Package' => $PackageName}); | |||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | 21 | ▶ | if ( not $MockifiedMockedObject->can('__getParametersFromMockifyCall') ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | 1 | Error("$PackageName was not mockified", { 'Position'=>$Position, 'Method' => $MethodName}); | |||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | 20 | ▶ | if( !( $Position ) || !(IsInteger( $Position ))){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$Position and IsInteger($Position)
MC/DC100%2/2
$Position and IsInteger($Position)
$Position
IsInteger($Position)
Truth table100%3/3
A and B
A= $PositionB= IsInteger($Position)
| |||||||||||||||||||||||||||||
| 58 | 18 | $Position = 0; | |||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 20 | return $MockifiedMockedObject->__getParametersFromMockifyCall( $MethodName, $Position ); | |||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | #----------------------------------------------------------------------------------------= | ||||||||||||||||||||||||||||
| 65 | =pod | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | =head2 WasCalled | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | my $WasCalled = WasCalled($MockifiedObject, 'nameOfMethod'); | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | This function returns the information if the method was called on the I<mockified> module. | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | =cut | ||||||||||||||||||||||||||||
| 74 | 6 | ▶ | sub WasCalled { | ||||||||||||||||||||||||||
|
Subroutine
WasCalled: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 75 | 6 | my ( $MockifiedMockedObject, $MethodName ) = @_; | |||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 6 | my $WasCalled; | |||||||||||||||||||||||||||
| 78 | 6 | my $AmountOfCalles = GetCallCount( $MockifiedMockedObject, $MethodName ); | |||||||||||||||||||||||||||
| 79 | 6 | ▶ | if($AmountOfCalles > 0){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | 5 | $WasCalled = 1; | |||||||||||||||||||||||||||
| 81 | }else{ | ||||||||||||||||||||||||||||
| 82 | 1 | $WasCalled = 0; | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 6 | return $WasCalled; | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | #----------------------------------------------------------------------------------------= | ||||||||||||||||||||||||||||
| 88 | =pod | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | =head2 GetCallCount | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | my $AmountOfCalls = GetCallCount($MockifiedObject, 'nameOfMethod'); | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | This function returns the information on how often the method was called on the I<mockified> module. If the method was not called it will return "0". | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | =cut | ||||||||||||||||||||||||||||
| 97 | 34 | ▶ | sub GetCallCount { | ||||||||||||||||||||||||||
|
Subroutine
GetCallCount: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 98 | 34 | my ( $MockifiedMockedObject, $MethodName ) = @_; | |||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 34 | _TestMockifyObject( $MockifiedMockedObject ); | |||||||||||||||||||||||||||
| 101 | 34 | return $MockifiedMockedObject->{'__MethodCallCounter'}->getAmountOfCalls( $MethodName ); | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | #---------------------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 105 | 34 | ▶ | sub _TestMockifyObject { | ||||||||||||||||||||||||||
|
Subroutine
_TestMockifyObject: called CC=3 SCAR=12.3
| |||||||||||||||||||||||||||||
| 106 | 34 | my ( $MockifiedMockedObject ) = @_; | |||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 34 | my $ObjectPath = ref( $MockifiedMockedObject ); | |||||||||||||||||||||||||||
| 109 | 34 | ▶ | if( not IsValid( $ObjectPath ) ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | 0 | Error( 'Object is not defined' ); | |||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | 34 | ▶ | if ( $MockifiedMockedObject->{'__isMockified'} != 1){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | 0 | Error( "The Object: '$ObjectPath' is not mockified" ); | |||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 34 | return; | |||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 14 | 1; | |||||||||||||||||||||||||||
| 120 | __END__ | ||||||||||||||||||||||||||||