File Coverage

blib/lib/Test/Mockify/Verify.pm
Criterion Covered Total %
statement 41 43 95.3
branch 12 14 85.7
condition 2 3 66.6
subroutine 9 9 100.0
pod 3 3 100.0
total 67 72 93.0


line stmt bran cond sub pod time code
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 1     1   389 use Test::Mockify::Tools qw ( Error IsValid );
  1         2  
  1         62  
18 1     1   5 use Test::Mockify::TypeTests qw ( IsInteger );
  1         1  
  1         32  
19 1     1   4 use Scalar::Util qw( blessed );
  1         1  
  1         33  
20              
21 1     1   3 use base qw ( Exporter );
  1         1  
  1         71  
22              
23 1     1   4 use strict;
  1         1  
  1         281  
24              
25             our @EXPORT_OK = qw (
26             GetParametersFromMockifyCall
27             WasCalled
28             GetCallCount
29             );
30              
31             #----------------------------------------------------------------------------------------=
32             =pod
33            
34             =head2 GetParametersFromMockifyCall
35            
36             my $aParameters = GetParametersFromMockifyCall($MockifiedObject, 'nameOfMethod', $OptionalPosition);
37            
38             This function returns all the parameters after the *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".
39             Returns an array ref with the parameters of the specific method call.
40             *(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.)*
41            
42             =cut
43             sub GetParametersFromMockifyCall {
44 13     13 1 899     my ( $MockifiedMockedObject, $MethodName, $Position ) = @_;
45              
46 13 100       40     if( not blessed $MockifiedMockedObject){
47 1         3         Error('The first argument must be blessed');
48                 }
49 12         15     my $PackageName = ref($MockifiedMockedObject);
50 12 100       26     if( not IsValid( $MethodName )){
51 1         5         Error('Method name must be specified', {'Position'=>$Position, 'Package' => $PackageName});
52                 }
53 11 100       27     if ( not $MockifiedMockedObject->can('__getParametersFromMockifyCall') ){
54 1         6         Error("$PackageName was not mockified", { 'Position'=>$Position, 'Method' => $MethodName});
55                 }
56 10 100 66     99     if( !( $Position ) || !(IsInteger( $Position ))){
57 8         9         $Position = 0;
58                 }
59              
60 10         15     return $MockifiedMockedObject->__getParametersFromMockifyCall( $MethodName, $Position );
61             }
62              
63             #----------------------------------------------------------------------------------------=
64             =pod
65            
66             =head2 WasCalled
67            
68             my $WasCalled = WasCalled($MockifiedObject, 'nameOfMethod');
69            
70             This function returns the information if the method was called on the *mockified* module.
71            
72             =cut
73             sub WasCalled {
74 2     2 1 8     my ( $MockifiedMockedObject, $MethodName ) = @_;
75              
76 2         2     my $WasCalled;
77 2         3     my $AmountOfCalles = GetCallCount( $MockifiedMockedObject, $MethodName );
78 2 100       6     if($AmountOfCalles > 0){
79 1         2         $WasCalled = 1;
80                 }else{
81 1         1         $WasCalled = 0;
82                 }
83              
84 2         4     return $WasCalled;
85             }
86             #----------------------------------------------------------------------------------------=
87             =pod
88            
89             =head2 GetCallCount
90            
91             my $AmountOfCalls = GetCallCount($MockifiedObject, 'nameOfMethod');
92            
93             This function returns the information on how often the method was called on the *mockified* module. If the method was not called it will return "0".
94            
95             =cut
96             sub GetCallCount {
97 11     11 1 81     my ( $MockifiedMockedObject, $MethodName ) = @_;
98              
99 11         16     _TestMockifyObject( $MockifiedMockedObject );
100 11         27     return $MockifiedMockedObject->{'__MethodCallCounter'}->getAmountOfCalls( $MethodName );
101             }
102              
103             #----------------------------------------------------------------------------------------
104             sub _TestMockifyObject {
105 11     11   9     my ( $MockifiedMockedObject ) = @_;
106              
107 11         12     my $ObjectPath = ref( $MockifiedMockedObject );
108 11 50       22     if( not IsValid( $ObjectPath ) ){
109 0         0         Error( 'Object is not defined' );
110                 }
111 11 50       52     if ( $MockifiedMockedObject->{'__isMockified'} != 1){
112 0         0         Error( "The Object: '$ObjectPath' is not mockified" );
113                 }
114              
115 11         15     return;
116             }
117              
118             1;
119