Skip to content

Commit 9786d68

Browse files
authored
Merge pull request #339 from kenjis/fix-PostgreSQL-tests
fix: PostgreSQL tests
2 parents e3cafec + eb3d174 commit 9786d68

File tree

10 files changed

+88
-19
lines changed

10 files changed

+88
-19
lines changed

src/Database/Migrations/2020-12-28-223112_create_auth_tables.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,13 +136,13 @@ public function down(): void
136136
{
137137
$this->db->disableForeignKeyChecks();
138138

139-
$this->forge->dropTable('users', true);
140139
$this->forge->dropTable('auth_logins', true);
141140
$this->forge->dropTable('auth_token_logins', true);
142141
$this->forge->dropTable('auth_remember_tokens', true);
143142
$this->forge->dropTable('auth_identities', true);
144143
$this->forge->dropTable('auth_groups_users', true);
145144
$this->forge->dropTable('auth_permissions_users', true);
145+
$this->forge->dropTable('users', true);
146146

147147
$this->db->enableForeignKeyChecks();
148148
}

src/Entities/Cast/IntBoolCast.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace CodeIgniter\Shield\Entities\Cast;
4+
5+
use CodeIgniter\Entity\Cast\BaseCast;
6+
7+
/**
8+
* Int Bool Cast
9+
*
10+
* DB column: int (0/1) <--> Class property: bool
11+
*/
12+
final class IntBoolCast extends BaseCast
13+
{
14+
/**
15+
* @param int $value
16+
*/
17+
public static function get($value, array $params = []): bool
18+
{
19+
return (bool) $value;
20+
}
21+
22+
/**
23+
* @param bool|int|string $value
24+
*/
25+
public static function set($value, array $params = []): int
26+
{
27+
return (int) $value;
28+
}
29+
}

src/Entities/Entity.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace CodeIgniter\Shield\Entities;
4+
5+
use CodeIgniter\Entity\Entity as FrameworkEntity;
6+
use CodeIgniter\Shield\Entities\Cast\IntBoolCast;
7+
8+
/**
9+
* Base Entity
10+
*/
11+
abstract class Entity extends FrameworkEntity
12+
{
13+
/**
14+
* Custom convert handlers
15+
*
16+
* @var array<string, string>
17+
* @phpstan-var array<string, class-string>
18+
*/
19+
protected $castHandlers = [
20+
'int_bool' => IntBoolCast::class,
21+
];
22+
}

src/Entities/Login.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,13 @@
22

33
namespace CodeIgniter\Shield\Entities;
44

5-
use CodeIgniter\Entity\Entity;
6-
75
class Login extends Entity
86
{
97
/**
108
* @var array<string, string>
119
*/
1210
protected $casts = [
1311
'date' => 'datetime',
14-
'success' => 'boolean',
12+
'success' => 'int_bool',
1513
];
1614
}

src/Entities/User.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace CodeIgniter\Shield\Entities;
44

55
use CodeIgniter\Database\Exceptions\DataException;
6-
use CodeIgniter\Entity\Entity;
76
use CodeIgniter\Shield\Authentication\Authenticators\Session;
87
use CodeIgniter\Shield\Authentication\Traits\HasAccessTokens;
98
use CodeIgniter\Shield\Authorization\Traits\Authorizable;
@@ -41,7 +40,7 @@ class User extends Entity
4140
*/
4241
protected $casts = [
4342
'id' => '?integer',
44-
'active' => 'boolean',
43+
'active' => 'int_bool',
4544
'permissions' => 'array',
4645
'groups' => 'array',
4746
];

src/Entities/UserIdentity.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace CodeIgniter\Shield\Entities;
44

5-
use CodeIgniter\Entity\Entity;
65
use CodeIgniter\Shield\Authentication\Passwords;
76

87
/**
@@ -25,7 +24,7 @@ class UserIdentity extends Entity
2524
* @var array<string, string>
2625
*/
2726
protected $casts = [
28-
'force_reset' => 'boolean',
27+
'force_reset' => 'int_bool',
2928
];
3029

3130
/**

src/Models/CheckQueryReturnTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ private function checkQueryReturn(bool $return): void
2121
$message = 'Query error: ' . $error['code'] . ', '
2222
. $error['message'] . ', query: ' . $this->db->getLastQuery();
2323

24-
throw new DatabaseException($message, $error['code']);
24+
throw new DatabaseException($message, (int) $error['code']);
2525
}
2626
}
2727

tests/Controllers/LoginTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public function testLoginActionEmailSuccess(): void
8585
$this->seeInDatabase('auth_logins', [
8686
'identifier' => '[email protected]',
8787
'user_id' => $this->user->id,
88-
'success' => true,
88+
'success' => 1,
8989
]);
9090
// Last Used date should have been set
9191
$identity = $this->user->getEmailIdentity();
@@ -147,7 +147,7 @@ public function testLoginActionUsernameSuccess(): void
147147
$this->seeInDatabase('auth_logins', [
148148
'identifier' => $this->user->username,
149149
'user_id' => $this->user->id,
150-
'success' => true,
150+
'success' => 1,
151151
]);
152152
// Last Used date should have been set
153153
$identity = $this->user->getEmailIdentity();

tests/Unit/UserModelTest.php

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,25 @@ public function testInsertUserObject(): void
6060
]);
6161
}
6262

63+
/**
64+
* This test is not correct.
65+
*
66+
* Entity's `toArray()` method returns array with properties and values.
67+
* The array may have different keys with the DB column names.
68+
* And the values may be different types with the DB column types.
69+
* So $userArray is not data to be inserted into the database.
70+
*/
6371
public function testInsertUserArray(): void
6472
{
6573
$users = $this->createUserModel();
6674

6775
$user = $this->createNewUser();
6876

6977
$userArray = $user->toArray();
70-
$id = $users->insert($userArray);
78+
// Fix value type
79+
$userArray['active'] = (int) $userArray['active'];
80+
81+
$id = $users->insert($userArray);
7182

7283
$this->dontSeeInDatabase('auth_identities', [
7384
'user_id' => $id,
@@ -85,7 +96,7 @@ private function createNewUser(): User
8596
$user->username = 'foo';
8697
$user->email = '[email protected]';
8798
$user->password = 'password';
88-
$user->active = 0;
99+
$user->active = false;
89100

90101
return $user;
91102
}
@@ -100,7 +111,7 @@ public function testSaveUpdateUserObjectWithUserDataToUpdate(): void
100111

101112
$user->username = 'bar';
102113
$user->email = '[email protected]';
103-
$user->active = 1;
114+
$user->active = true;
104115

105116
$users->save($user);
106117

@@ -124,7 +135,7 @@ public function testUpdateUserObjectWithUserDataToUpdate(): void
124135

125136
$user->username = 'bar';
126137
$user->email = '[email protected]';
127-
$user->active = 1;
138+
$user->active = true;
128139

129140
$users->update(null, $user);
130141

@@ -138,6 +149,14 @@ public function testUpdateUserObjectWithUserDataToUpdate(): void
138149
]);
139150
}
140151

152+
/**
153+
* This test is not correct.
154+
*
155+
* Entity's `toArray()` method returns array with properties and values.
156+
* The array may have different keys with the DB column names.
157+
* And the values may be different types with the DB column types.
158+
* So $userArray is not data to be inserted into the database.
159+
*/
141160
public function testUpdateUserArrayWithUserDataToUpdate(): void
142161
{
143162
$users = $this->createUserModel();
@@ -148,9 +167,12 @@ public function testUpdateUserArrayWithUserDataToUpdate(): void
148167

149168
$user->username = 'bar';
150169
$user->email = '[email protected]';
151-
$user->active = 1;
170+
$user->active = true;
152171

153172
$userArray = $user->toArray();
173+
// Fix value type
174+
$userArray['active'] = (int) $userArray['active'];
175+
154176
$users->update(null, $userArray);
155177

156178
$this->dontSeeInDatabase('auth_identities', [

tests/Unit/UserTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ public function testUpdateEmail(): void
160160
{
161161
// Update user's email
162162
$this->user->email = '[email protected]';
163-
$this->user->active = 0;
163+
$this->user->active = false;
164164

165165
$users = model(UserModel::class);
166166
$users->save($this->user);
@@ -182,7 +182,7 @@ public function testUpdatePassword(): void
182182
// Update user's email
183183
$this->user->email = '[email protected]';
184184
$this->user->password = 'foobar';
185-
$this->user->active = 0;
185+
$this->user->active = false;
186186

187187
$users = model(UserModel::class);
188188
$users->save($this->user);
@@ -201,7 +201,7 @@ public function testUpdatePasswordHash(): void
201201
$hash = service('passwords')->hash('foobar');
202202
$this->user->email = '[email protected]';
203203
$this->user->password_hash = $hash;
204-
$this->user->active = 0;
204+
$this->user->active = false;
205205

206206
$users = model(UserModel::class);
207207
$users->save($this->user);

0 commit comments

Comments
 (0)