File Coverage

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


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 3     3   1482 use Test::Mockify::Tools qw ( Error IsValid );
  3         5  
  3         152  
18 3     3   19 use Test::Mockify::TypeTests qw ( IsInteger );
  3         3  
  3         119  
19 3     3   12 use Scalar::Util qw( blessed );
  3         4  
  3         109  
20              
21 3     3   9 use base qw ( Exporter );
  3         3  
  3         254  
22              
23 3     3   13 use strict;
  3         3  
  3         59  
24 3     3   9 use warnings;
  3         3  
  3         1080  
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<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             sub GetParametersFromMockifyCall {
45 15     15 1 1253     my ( $MockifiedMockedObject, $MethodName, $Position ) = @_;
46              
47 15 100       57     if( not blessed $MockifiedMockedObject){
48 1         4         Error('The first argument must be blessed');
49                 }
50 14         22     my $PackageName = ref($MockifiedMockedObject);
51 14 100       36     if( not IsValid( $MethodName )){
52 1         4         Error('Method name must be specified', {'Position'=>$Position, 'Package' => $PackageName});
53                 }
54 13 100       43     if ( not $MockifiedMockedObject->can('__getParametersFromMockifyCall') ){
55 1         5         Error("$PackageName was not mockified", { 'Position'=>$Position, 'Method' => $MethodName});
56                 }
57 12 100 66     151     if( !( $Position ) || !(IsInteger( $Position ))){
58 10         8         $Position = 0;
59                 }
60              
61 12         28     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             sub WasCalled {
75 4     4 1 15     my ( $MockifiedMockedObject, $MethodName ) = @_;
76              
77 4         6     my $WasCalled;
78 4         9     my $AmountOfCalles = GetCallCount( $MockifiedMockedObject, $MethodName );
79 4 100       13     if($AmountOfCalles > 0){
80 3         5         $WasCalled = 1;
81                 }else{
82 1         1         $WasCalled = 0;
83                 }
84              
85 4         11     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             sub GetCallCount {
98 15     15 1 545     my ( $MockifiedMockedObject, $MethodName ) = @_;
99              
100 15         43     _TestMockifyObject( $MockifiedMockedObject );
101 15         37     return $MockifiedMockedObject->{'__MethodCallCounter'}->getAmountOfCalls( $MethodName );
102             }
103              
104             #----------------------------------------------------------------------------------------
105             sub _TestMockifyObject {
106 15     15   13     my ( $MockifiedMockedObject ) = @_;
107              
108 15         18     my $ObjectPath = ref( $MockifiedMockedObject );
109 15 50       42     if( not IsValid( $ObjectPath ) ){
110 0         0         Error( 'Object is not defined' );
111                 }
112 15 50       40     if ( $MockifiedMockedObject->{'__isMockified'} != 1){
113 0         0         Error( "The Object: '$ObjectPath' is not mockified" );
114                 }
115              
116 15         16     return;
117             }
118              
119             1;
120             __END__
121            
122             =head1 LICENSE
123            
124             Copyright (C) 2017 ePages GmbH
125            
126             This library is free software; you can redistribute it and/or modify
127             it under the same terms as Perl itself.
128            
129             =head1 AUTHOR
130            
131             Christian Breitkreutz E<lt>christianbreitkreutz@gmx.deE<gt>
132            
133             =cut