File Coverage

blib/lib/Test/Mockify/Verify.pm
Criterion Covered Total %
statement 44 46 95.6
branch 12 14 85.7
condition 3 3 100.0
subroutine 10 10 100.0
pod 3 3 100.0
total 72 76 94.7


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 14     14   50220 use Test::Mockify::Tools qw ( Error IsValid );
  14         33  
  14         811  
18 14     14   93 use Test::Mockify::TypeTests qw ( IsInteger );
  14         62  
  14         607  
19 14     14   124 use Scalar::Util qw( blessed );
  14         30  
  14         632  
20              
21 14     14   100 use base qw ( Exporter );
  14         44  
  14         1213  
22              
23 14     14   92 use strict;
  14         37  
  14         337  
24 14     14   90 use warnings;
  14         302  
  14         6057  
25              
26             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 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             sub GetParametersFromMockifyCall {
45 23     23 1 7250 my ( $MockifiedMockedObject, $MethodName, $Position ) = @_;
46              
47 23 100       113 if( not blessed $MockifiedMockedObject){
48 1         3 Error('The first argument must be blessed');
49             }
50 22         52 my $PackageName = ref($MockifiedMockedObject);
51 22 100       72 if( not IsValid( $MethodName )){
52 1         6 Error('Method name must be specified', {'Position'=>$Position, 'Package' => $PackageName});
53             }
54 21 100       76 if ( not $MockifiedMockedObject->can('__getParametersFromMockifyCall') ){
55 1         6 Error("$PackageName was not mockified", { 'Position'=>$Position, 'Method' => $MethodName});
56             }
57 20 100 100     326 if( !( $Position ) || !(IsInteger( $Position ))){
58 18         33 $Position = 0;
59             }
60              
61 20         62 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 module.
72              
73             =cut
74             sub WasCalled {
75 6     6 1 1127 my ( $MockifiedMockedObject, $MethodName ) = @_;
76              
77 6         9 my $WasCalled;
78 6         16 my $AmountOfCalles = GetCallCount( $MockifiedMockedObject, $MethodName );
79 6 100       20 if($AmountOfCalles > 0){
80 5         10 $WasCalled = 1;
81             }else{
82 1         9 $WasCalled = 0;
83             }
84              
85 6         23 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 module. If the method was not called it will return "0".
95              
96             =cut
97             sub GetCallCount {
98 34     34 1 10343 my ( $MockifiedMockedObject, $MethodName ) = @_;
99              
100 34         92 _TestMockifyObject( $MockifiedMockedObject );
101 34         118 return $MockifiedMockedObject->{'__MethodCallCounter'}->getAmountOfCalls( $MethodName );
102             }
103              
104             #----------------------------------------------------------------------------------------
105             sub _TestMockifyObject {
106 34     34   64 my ( $MockifiedMockedObject ) = @_;
107              
108 34         63 my $ObjectPath = ref( $MockifiedMockedObject );
109 34 50       91 if( not IsValid( $ObjectPath ) ){
110 0         0 Error( 'Object is not defined' );
111             }
112 34 50       103 if ( $MockifiedMockedObject->{'__isMockified'} != 1){
113 0         0 Error( "The Object: '$ObjectPath' is not mockified" );
114             }
115              
116 34         51 return;
117             }
118              
119             1;
120             __END__