v4/Mock/Engine.pm

filter: Statementstmt 97.7% 44 / 45 Branchbran 75.0% 6 / 8 Conditioncond 80.0% 4 / 5 MC/DCmcdc 66.6% 2 / 3 Subroutinesub 100% 10 / 10 Podpod 100% 5 / 5 totaltotal 93.4% 71 / 76 CC 9 SCAR 9.9
CC
9
Cov
93%
delete_user
5.0
user
3.0
update_user
2.0
CRAP 2.7
« v4/Mock/RESTClient.pm ↑ Summary v4/Constants.pm »
1 package GitLab::API::v4::Mock::Engine;
2 1 our $VERSION = '0.27';
3  
4 =encoding utf8
5  
6 =head1 NAME
7  
8 GitLab::API::v4::Mock::Engine - Mocking the internals of a GitLab server.
9  
10 =head1 SYNOPSIS
11  
12 use GitLab::API::v4::Mock::Engine;
13
14 my $engine = GitLab::API::v4::Mock::Engine->new();
15
16 my $user = $engine->create_user({
17 email => $email,
18 username => $username,
19 name => $name,
20 ...,
21 });
22
23 print "User created with ID: $user->{id}\n";
24  
25 =head1 DESCRIPTION
26  
27 This module provides the tooling to run a mock of the internal state
28 of a GitLab server.
29  
30 At this time very little is validated. For example, when you
31 create a user with L</create_user> there is no logic which double
32 checks that you specify the required C<email> field or that you
33 don't put in fields that are unexpected.
34  
35 =cut
36  
37 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 1 ▶use strictures 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 1 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
40  
41 =head1 ATTRIBUTES
42  
43 =head2 next_ids
44  
45 my $ids = $engine->next_ids();
46  
47 A hash reference containing object types to the next ID.
48  
49 Used by L</next_id_for>.
50  
51 =cut
52  
53 has next_ids => (
54 is => 'ro',
55 init_arg => undef,
56 2 ▶ default => sub{ {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
57 1 );
58  
59 =head2 users
60  
61 my $users = $engine->users();
62 foreach my $user (@$users) { ... }
63  
64 Returns the full array reference of all users hash references.
65  
66 =cut
67  
68 has users => (
69 is => 'ro',
70 init_arg => undef,
71 2 ▶ default => sub{ [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
72 1 );
73  
74 =head1 METHODS
75  
76 =head2 next_id_for
77  
78 my $id = $engine->next_id_for( 'user' );
79  
80 Given an object type this will return the next unused ID.
81  
82 =cut
83  
84 3 ▶sub next_id_for {
Subroutine next_id_for: called CC=2 SCAR=6.9
Pod documented
85 3 my ($self, $for) = @_;
86  
87 3 ▶ my $next_id = $self->next_ids->{$for} || 1;
Condition100%2/2
$self->next_ids->{$for} || 1
l!l
21
MC/DC100%1/1
$self->next_ids->{$for} || 1
$self->next_ids->{$for}
Truth table100%2/2
A
A= $self->next_ids->{$for}
Aresult
11
00
88 3 $self->next_ids->{$for} = $next_id + 1;
89  
90 3 return $next_id;
91 }
92  
93 =head1 USER METHODS
94  
95 =head2 user
96  
97 my $user = $engine->user( $id );
98  
99 Returns a user hash reference for the given ID.
100  
101 If no user is found with the ID then C<undef> is returned.
102  
103 =cut
104  
105 1 ▶sub user {
Subroutine user: called CC=3 SCAR=11.1
Pod documented
106 1 my ($self, $id) = @_;
107  
108 1 foreach my $user (@{ $self->users() }) {
109 3 ▶ return $user if $user->{id} == $id;
Branch: 1 branch
BranchTrueFalseTotal
if $user->{"id"} == $id 1 2 3
110 }
111  
112 0 return undef;
113 }
114  
115 =head2 create_user
116  
117 my $user = $engine->create_user( $user );
118 my $id = $user->{id};
119  
120 Takes a user hash reference, sets the C<id> field, and stores it in
121 L</users>.
122  
123 Returns the updated user hash reference.
124  
125 =cut
126  
127 3 ▶sub create_user {
Subroutine create_user: called CC=1 SCAR=0.0
Pod documented
128 3 my ($self, $user) = @_;
129  
130 3 $user->{id} = $self->next_id_for( 'user' );
131 3 push @{ $self->users() }, $user;
132  
133 3 return $user;
134 }
135  
136 =head2 update_user
137  
138 my $user = $engine->update_user( $id, $new_user_data );
139  
140 Takes the ID of the user to update and a hash reference of fields
141 to update.
142  
143 Returns the updated user hash reference.
144  
145 =cut
146  
147 1 ▶sub update_user {
Subroutine update_user: called CC=2 SCAR=7.0
Pod documented
148 1 my ($self, $id, $data) = @_;
149  
150 1 my $user = $self->user( $id );
151 1 ▶ return undef if !$user;
Branch: 1 branch
BranchTrueFalseTotal
unless $user 0 1 1
152  
153 1 %$user = (
154 %$user,
155 %$data,
156 );
157  
158 1 return $user;
159 }
160  
161 =head2 delete_user
162  
163 my $user = $engine->delete_user( $id );
164  
165 Deletes the user with the specified ID from L</users>.
166  
167 If no user is found with the ID then C<undef> is returned.
168 Otherwise the user hash reference is returned.
169  
170 =cut
171  
172 1 ▶sub delete_user {
Subroutine delete_user: called CC=5 SCAR=16.2
Pod documented
173 1 my ($self, $id) = @_;
174  
175 1 my $users = $self->users();
176  
177 1 my @new;
178 my $found_user;
179 1 foreach my $user (@$users) {
180 3 ▶ if ($user->{id} == $id and !$found_user) {
Branch: 1 branch
BranchTrueFalseTotal
if ($user->{"id"} == $id and not $found_user) 1 2 3
Condition66%2/3
$user->{"id"} == $id and not $found_user
!ll&&!rl&&r
201
MC/DC50%1/2
$user->{"id"} == $id and not $found_user
$user->{"id"} == $id not $found_user
Truth table66%2/3
A and not(B)
A= $user->{"id"} == $idB= not $found_user
ABresult
0X0
100
111
181 1 $found_user = $user;
182 1 next;
183 }
184 2 push @new, $user;
185 }
186  
187 1 ▶ return undef if !$found_user;
Branch: 1 branch
BranchTrueFalseTotal
unless $found_user 0 1 1
188  
189 1 @$users = @new;
190  
191 1 return $found_user;
192 }
193  
194 1 1;
195 __END__
« v4/Mock/RESTClient.pm ↑ Summary v4/Constants.pm »