| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package GitLab::API::v4::Mock::Engine; | 
| 2 |  |  |  |  |  |  | our $VERSION = '0.25'; | 
| 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 there is no logic which double | 
| 32 |  |  |  |  |  |  | checks that you specify the required C field or that you | 
| 33 |  |  |  |  |  |  | don't put in fields that are unexpected. | 
| 34 |  |  |  |  |  |  |  | 
| 35 |  |  |  |  |  |  | =cut | 
| 36 |  |  |  |  |  |  |  | 
| 37 | 1 |  |  | 1 |  | 522 | use Moo; | 
|  | 1 |  |  |  |  | 11178 |  | 
|  | 1 |  |  |  |  | 5 |  | 
| 38 | 1 |  |  | 1 |  | 1486 | use strictures 2; | 
|  | 1 |  |  |  |  | 8 |  | 
|  | 1 |  |  |  |  | 45 |  | 
| 39 | 1 |  |  | 1 |  | 676 | use namespace::clean; | 
|  | 1 |  |  |  |  | 10871 |  | 
|  | 1 |  |  |  |  | 6 |  | 
| 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. | 
| 50 |  |  |  |  |  |  |  | 
| 51 |  |  |  |  |  |  | =cut | 
| 52 |  |  |  |  |  |  |  | 
| 53 |  |  |  |  |  |  | has next_ids => ( | 
| 54 |  |  |  |  |  |  | is       => 'ro', | 
| 55 |  |  |  |  |  |  | init_arg => undef, | 
| 56 |  |  |  |  |  |  | default  => sub{ {} }, | 
| 57 |  |  |  |  |  |  | ); | 
| 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 |  |  |  |  |  |  | default  => sub{ [] }, | 
| 72 |  |  |  |  |  |  | ); | 
| 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 |  |  |  |  |  |  | sub next_id_for { | 
| 85 | 3 |  |  | 3 | 1 | 7 | my ($self, $for) = @_; | 
| 86 |  |  |  |  |  |  |  | 
| 87 | 3 |  | 100 |  |  | 15 | my $next_id = $self->next_ids->{$for} || 1; | 
| 88 | 3 |  |  |  |  | 10 | $self->next_ids->{$for} = $next_id + 1; | 
| 89 |  |  |  |  |  |  |  | 
| 90 | 3 |  |  |  |  | 8 | 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 is returned. | 
| 102 |  |  |  |  |  |  |  | 
| 103 |  |  |  |  |  |  | =cut | 
| 104 |  |  |  |  |  |  |  | 
| 105 |  |  |  |  |  |  | sub user { | 
| 106 | 1 |  |  | 1 | 1 | 3 | my ($self, $id) = @_; | 
| 107 |  |  |  |  |  |  |  | 
| 108 | 1 |  |  |  |  | 3 | foreach my $user (@{ $self->users() }) { | 
|  | 1 |  |  |  |  | 4 |  | 
| 109 | 3 | 100 |  |  |  | 10 | return $user if $user->{id} == $id; | 
| 110 |  |  |  |  |  |  | } | 
| 111 |  |  |  |  |  |  |  | 
| 112 | 0 |  |  |  |  | 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 field, and stores it in | 
| 121 |  |  |  |  |  |  | L. | 
| 122 |  |  |  |  |  |  |  | 
| 123 |  |  |  |  |  |  | Returns the updated user hash reference. | 
| 124 |  |  |  |  |  |  |  | 
| 125 |  |  |  |  |  |  | =cut | 
| 126 |  |  |  |  |  |  |  | 
| 127 |  |  |  |  |  |  | sub create_user { | 
| 128 | 3 |  |  | 3 | 1 | 26 | my ($self, $user) = @_; | 
| 129 |  |  |  |  |  |  |  | 
| 130 | 3 |  |  |  |  | 18 | $user->{id} = $self->next_id_for( 'user' ); | 
| 131 | 3 |  |  |  |  | 4 | push @{ $self->users() }, $user; | 
|  | 3 |  |  |  |  | 10 |  | 
| 132 |  |  |  |  |  |  |  | 
| 133 | 3 |  |  |  |  | 7 | 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 |  |  |  |  |  |  | sub update_user { | 
| 148 | 1 |  |  | 1 | 1 | 11 | my ($self, $id, $data) = @_; | 
| 149 |  |  |  |  |  |  |  | 
| 150 | 1 |  |  |  |  | 4 | my $user = $self->user( $id ); | 
| 151 | 1 | 50 |  |  |  | 4 | return undef if !$user; | 
| 152 |  |  |  |  |  |  |  | 
| 153 | 1 |  |  |  |  | 5 | %$user = ( | 
| 154 |  |  |  |  |  |  | %$user, | 
| 155 |  |  |  |  |  |  | %$data, | 
| 156 |  |  |  |  |  |  | ); | 
| 157 |  |  |  |  |  |  |  | 
| 158 | 1 |  |  |  |  | 4 | 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. | 
| 166 |  |  |  |  |  |  |  | 
| 167 |  |  |  |  |  |  | If no user is found with the ID then C is returned. | 
| 168 |  |  |  |  |  |  | Otherwise the user hash reference is returned. | 
| 169 |  |  |  |  |  |  |  | 
| 170 |  |  |  |  |  |  | =cut | 
| 171 |  |  |  |  |  |  |  | 
| 172 |  |  |  |  |  |  | sub delete_user { | 
| 173 | 1 |  |  | 1 | 1 | 11 | my ($self, $id) = @_; | 
| 174 |  |  |  |  |  |  |  | 
| 175 | 1 |  |  |  |  | 3 | my $users = $self->users(); | 
| 176 |  |  |  |  |  |  |  | 
| 177 | 1 |  |  |  |  | 3 | my @new; | 
| 178 |  |  |  |  |  |  | my $found_user; | 
| 179 | 1 |  |  |  |  | 3 | foreach my $user (@$users) { | 
| 180 | 3 | 100 | 66 |  |  | 30 | if ($user->{id} == $id and !$found_user) { | 
| 181 | 1 |  |  |  |  | 2 | $found_user = $user; | 
| 182 | 1 |  |  |  |  | 3 | next; | 
| 183 |  |  |  |  |  |  | } | 
| 184 | 2 |  |  |  |  | 6 | push @new, $user; | 
| 185 |  |  |  |  |  |  | } | 
| 186 |  |  |  |  |  |  |  | 
| 187 | 1 | 50 |  |  |  | 3 | return undef if !$found_user; | 
| 188 |  |  |  |  |  |  |  | 
| 189 | 1 |  |  |  |  | 4 | @$users = @new; | 
| 190 |  |  |  |  |  |  |  | 
| 191 | 1 |  |  |  |  | 4 | return $found_user; | 
| 192 |  |  |  |  |  |  | } | 
| 193 |  |  |  |  |  |  |  | 
| 194 |  |  |  |  |  |  | 1; | 
| 195 |  |  |  |  |  |  | __END__ |