Mockify/Method.pm

filter: Statementstmt 99.1% 111 / 112 Branchbran 97.8% 45 / 46 Conditioncond 100% 15 / 15 MC/DCmcdc 100% 10 / 10 Subroutinesub 100% 17 / 17 Podpod 75.0% 3 / 4 totaltotal 98.5% 201 / 204 CC 34 SCAR 16.0
CC
34
Cov
99%
_getType
8.0
call
7.0
_testMatcherStore
6.0
CRAP 5.0
↑ Summary Mockify/Parameter.pm »
1 =pod
2  
3 =head1 Name
4  
5 Test::Mockify::Method - chained setup
6  
7 =head1 DESCRIPTION
8  
9 L<Test::Mockify::Method|Test::Mockify::Method> is used to provide the chained mock setup
10  
11 =head1 METHODS
12  
13 =cut
14 package Test::Mockify::Method;
15 18 ▶use Test::Mockify::Parameter;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 18 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 18 ▶use Test::Mockify::TypeTests qw (
Subroutine BEGIN: called CC=1 SCAR=0.0
18 IsInteger
19 IsFloat
20 IsString
21 IsArrayReference
22 IsHashReference
23 IsObjectReference
24 IsCodeReference
25 18 );
26 18 ▶use Test::Mockify::Matcher qw (SupportedTypes);
Subroutine BEGIN: called CC=1 SCAR=0.0
27 18 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
28 18 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 18 ▶use Test::Mockify::Tools qw (Error);
Subroutine BEGIN: called CC=1 SCAR=0.0
30 18 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 #---------------------------------------------------------------------
33 144 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod undocumented
34 144 my $Class = shift;
35 144 my $self = bless {
36 'TypeStore'=> undef,
37 'MatcherStore'=> undef,
38 'AnyStore'=> undef,
39 }, $Class;
40 144 foreach my $Type (SupportedTypes()){
41 144 $self->{'MatcherStore'}{$Type} = [];
42 }
43 144 return $self;
44 }
45 =pod
46  
47 =head2 when
48  
49 C<when> have to be called with a L<Test::Mockify::Matcher|Test::Mockify::Matcher> to specify the expected parameter list (signature).
50 This will create for every signature a L<Test::Mockify::Parameter|Test::Mockify::Parameter> Object which will stored and also returned. So it is possible to create multiple signatures for one Method.
51 It is not possible to mix C<when> with C<whenAny>.
52  
53 when(String())
54 when(Number(),String('abc'))
55  
56 =cut
57 151 ▶sub when {
Subroutine when: called CC=5 SCAR=16.1
Pod documented
58 151 my $self = shift;
59 151 my @Parameters = @_;
60 151 my @Signature;
61 151 foreach my $Signature (keys %{$self->{'TypeStore'}}){
62 67 ▶ if($Signature eq 'UsedWithWhenAny'){
Branch: 1 branch
BranchTrueFalseTotal
if ($Signature eq "UsedWithWhenAny") 1 66 67
63 1 Error('It is not possible to mix "when" and "whenAny" for the same method.');
64 }
65 }
66 150 foreach my $hParameter ( @Parameters ){
67 160 ▶ Error('Use Test::Mockify::Matcher to define proper matchers.') unless (ref($hParameter) eq 'HASH');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $hParameter eq "HASH" 1 159 160
68 159 push(@Signature, $hParameter->{'Type'});
69 }
70 149 $self->_checkExpectedParameters(\@Parameters);
71 142 return $self->_addToTypeStore(\@Signature, \@Parameters);
72 }
73 =pod
74  
75 =head2 whenAny
76  
77 C<whenAny> have to be called without parameter, when called it will accept any type and amount of parameter. It will return a L<Test::Mockify::Parameter|Test::Mockify::Parameter> Object.
78 It is not possible to mix C<whenAny> with C<when>.
79  
80 whenAny()
81  
82 =cut
83 12 ▶sub whenAny {
Subroutine whenAny: called CC=3 SCAR=11.0
Pod documented
84 12 my $self = shift;
85 12 ▶ Error ('"whenAny" doesn\'t allow any parameters' ) if (@_);
Branch: 1 branch
BranchTrueFalseTotal
if @_ 1 11 12
86 11 ▶ if((scalar keys %{$self->{'TypeStore'}})){
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %{$$self{"TypeStore"};}) 2 9 11
87 2 Error('You can use "whenAny" only once. Additionaly, it is not possible to mix "when" and "whenAny" for the same method.');
88 }
89 9 return $self->_addToTypeStore(['UsedWithWhenAny']);
90 }
91  
92 #---------------------------------------------------------------------
93 149 ▶sub _checkExpectedParameters{
Subroutine _checkExpectedParameters: called CC=4 SCAR=13.9
94 149 my $self = shift;
95 149 my ( $NewExpectedParameters) = @_;
96 149 my $SignatureKey = '';
97 149 ▶ for(my $i = 0; $i < scalar @{$NewExpectedParameters}; $i++){ ## no critic (ProhibitCStyleForLoops) i need the counter
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @{$NewExpectedParameters;}) 159 145 304
98 159 my $Type = $NewExpectedParameters->[$i]->{'Type'};
99 159 $SignatureKey .= $Type;
100 159 my $NewExpectedParameter = $NewExpectedParameters->[$i];
101 159 $self->_testMatcherStore($self->{'MatcherStore'}{$Type}->[$i], $NewExpectedParameter);
102 157 $self->{'MatcherStore'}{$Type}->[$i] = $NewExpectedParameter;
103 157 $self->_testAnyStore($self->{'AnyStore'}->[$i], $Type);
104 155 $self->{'AnyStore'}->[$i] = $Type;
105 }
106  
107 145 foreach my $ExistingParameter (@{$self->{'TypeStore'}{$SignatureKey}}){
108 9 ▶ if($ExistingParameter->compareExpectedParameters($NewExpectedParameters)){
Branch: 1 branch
BranchTrueFalseTotal
if ($ExistingParameter->compareExpectedParameters($NewExpectedParameters)) 3 6 9
109 3 Error('You can use a method signature only once.');
110 }
111 }
112 }
113  
114 #---------------------------------------------------------------------
115 159 ▶sub _testMatcherStore {
Subroutine _testMatcherStore: called CC=6 SCAR=17.9
116 159 my $self = shift;
117 159 my ($MatcherStore, $NewExpectedParameterValue) = @_;
118 159 ▶ if( defined $NewExpectedParameterValue->{'Value'} ){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $NewExpectedParameterValue->{'Value'}) { } 88 71 159
119 88 ▶ if($MatcherStore and not $MatcherStore->{'Value'}){
Branch: 1 branch
BranchTrueFalseTotal
if ($MatcherStore and not $MatcherStore->{"Value"}) 1 87 88
Condition100%3/3
$MatcherStore and not $MatcherStore->{"Value"}
!ll&&!rl&&r
8071
MC/DC100%2/2
$MatcherStore and not $MatcherStore->{"Value"}
$MatcherStore not $MatcherStore->{"Value"}
Truth table100%3/3
A and not(B)
A= $MatcherStoreB= not $MatcherStore->{"Value"}
ABresult
0X0
100
111
120 1 Error('It is not possibel to mix "expected parameter" with previously set "any parameter".');
121 }
122 } else {
123 71 ▶ if($MatcherStore && $MatcherStore->{'Value'}){
Branch: 1 branch
BranchTrueFalseTotal
if ($MatcherStore and $MatcherStore->{"Value"}) 1 70 71
Condition100%3/3
$MatcherStore and $MatcherStore->{"Value"}
!ll&&!rl&&r
6821
MC/DC100%2/2
$MatcherStore and $MatcherStore->{"Value"}
$MatcherStore $MatcherStore->{"Value"}
Truth table100%3/3
A and B
A= $MatcherStoreB= $MatcherStore->{"Value"}
ABresult
0X0
100
111
124 1 Error('It is not possibel to mix "any parameter" with previously set "expected parameter".');
125 }
126 }
127 157 return;
128 }
129 #---------------------------------------------------------------------
130 157 ▶sub _testAnyStore {
Subroutine _testAnyStore: called CC=6 SCAR=17.9
131 157 my $self = shift;
132 157 my ($AnyStore, $Type) = @_;
133 157 ▶ if($AnyStore){
Branch: 1 branch
BranchTrueFalseTotal
if ($AnyStore) 29 128 157
134 29 ▶ if($AnyStore eq 'any' and $Type ne 'any'){
Branch: 1 branch
BranchTrueFalseTotal
if ($AnyStore eq "any" and $Type ne "any") 1 28 29
Condition100%3/3
$AnyStore eq "any" and $Type ne "any"
!ll&&!rl&&r
2711
MC/DC100%2/2
$AnyStore eq "any" and $Type ne "any"
$AnyStore eq "any" $Type ne "any"
Truth table100%3/3
A and B
A= $AnyStore eq "any"B= $Type ne "any"
ABresult
0X0
100
111
135 1 Error('It is not possibel to mix "specific type" with previously set "any type".');
136 }
137 28 ▶ if($AnyStore ne 'any' and $Type eq 'any'){
Branch: 1 branch
BranchTrueFalseTotal
if ($AnyStore ne "any" and $Type eq "any") 1 27 28
Condition100%3/3
$AnyStore ne "any" and $Type eq "any"
!ll&&!rl&&r
1261
MC/DC100%2/2
$AnyStore ne "any" and $Type eq "any"
$AnyStore ne "any" $Type eq "any"
Truth table100%3/3
A and B
A= $AnyStore ne "any"B= $Type eq "any"
ABresult
0X0
100
111
138 1 Error('It is not possibel to mix "any type" with previously set "specific type".');
139 }
140 }
141 155 return;
142 }
143 #---------------------------------------------------------------------
144 124 ▶sub _addToTypeStore {
Subroutine _addToTypeStore: called CC=1 SCAR=0.0
145 124 my $self = shift;
146 124 my ($Signature, $NewExpectedParameters) = @_;
147 124 my $SignatureKey = join('',@{$Signature});
148 124 my $Parameter = Test::Mockify::Parameter->new($NewExpectedParameters);
149 124 push(@{$self->{'TypeStore'}{$SignatureKey}}, $Parameter );
150 124 return $Parameter->buildReturn();
151 }
152 =pod
153  
154 =head2 call
155  
156 C<call> will be called with a list of parameters. If the signature of this parameters match a stored signature it will call the corresponding parameter object.
157  
158 call()
159  
160 =cut
161 175 ▶sub call {
Subroutine call: called CC=7 SCAR=19.5
Pod documented
162 175 my $self = shift;
163 175 my @Parameters = @_;
164 175 my $SignatureKey = '';
165 175 ▶ for(my $i = 0; $i < scalar @Parameters; $i++){ ## no critic (ProhibitCStyleForLoops) i need the counter
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @Parameters) 200 175 375
166 200 ▶ if($self->{'AnyStore'}->[$i] && $self->{'AnyStore'}->[$i] eq 'any'){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'AnyStore'}[$i] and $self->{'AnyStore'}[$i] eq 'any') { } 18 182 200
Condition100%3/3
$self->{"AnyStore"}[$i] and $self->{"AnyStore"}[$i] eq "any"
!ll&&!rl&&r
2016218
MC/DC100%2/2
$self->{"AnyStore"}[$i] and $self->{"AnyStore"}[$i] eq "any"
$self->{"AnyStore"}[$i] $self->{"AnyStore"}[$i] eq "any"
Truth table100%3/3
A and B
A= $self->{"AnyStore"}[$i]B= $self->{"AnyStore"}[$i] eq "any"
ABresult
0X0
100
111
167 18 $SignatureKey .= 'any';
168 }else{
169 182 $SignatureKey .= $self->_getType($Parameters[$i]);
170 }
171 }
172 175 ▶ if($self->{'TypeStore'}{'UsedWithWhenAny'}){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'TypeStore'}{'UsedWithWhenAny'}) { } 13 162 175
173 13 return $self->{'TypeStore'}{'UsedWithWhenAny'}->[0]->call(@Parameters);
174 }else {
175 162 foreach my $ExistingParameter (@{$self->{'TypeStore'}{$SignatureKey}}){
176 156 ▶ if($ExistingParameter->matchWithExpectedParameters(@Parameters)){
Branch: 1 branch
BranchTrueFalseTotal
if ($ExistingParameter->matchWithExpectedParameters(@Parameters)) 142 14 156
177 142 return $ExistingParameter->call(@Parameters);
178 }
179 }
180 }
181 20 Error ("No matching found for signatur type '$SignatureKey' \nvalues:".Dumper(\@Parameters));
182 }
183 #---------------------------------------------------------------------
184 182 ▶sub _getType{
Subroutine _getType: called CC=8 SCAR=20.8
185 182 my $self = shift;
186 182 my ($Parameter) = @_;
187 182 ▶ return 'arrayref' if(IsArrayReference($Parameter));
Branch: 1 branch
BranchTrueFalseTotal
if IsArrayReference($Parameter) 10 172 182
188 172 ▶ return 'hashref' if(IsHashReference($Parameter));
Branch: 1 branch
BranchTrueFalseTotal
if IsHashReference($Parameter) 10 162 172
189 162 ▶ return 'object' if(IsObjectReference($Parameter));
Branch: 1 branch
BranchTrueFalseTotal
if IsObjectReference($Parameter) 14 148 162
190 148 ▶ return 'sub' if(IsCodeReference($Parameter));
Branch: 1 branch
BranchTrueFalseTotal
if IsCodeReference($Parameter) 4 144 148
191 144 ▶ return 'number' if(IsFloat($Parameter));
Branch: 1 branch
BranchTrueFalseTotal
if IsFloat($Parameter) 69 75 144
192 75 ▶ return 'string' if(IsString($Parameter));
Branch: 1 branch
BranchTrueFalseTotal
if IsString($Parameter) 67 8 75
193 8 ▶ return 'undef' if( not $Parameter);
Branch: 1 branch
BranchTrueFalseTotal
unless $Parameter 8 0 8
194 0 Error("UnexpectedParameterType for: '$Parameter'");
195 }
196  
197 18 1;
198  
199 __END__
↑ Summary Mockify/Parameter.pm »