Skip to content

fix: PostgreSQL tests #339

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Aug 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -136,13 +136,13 @@ public function down(): void
{
$this->db->disableForeignKeyChecks();

$this->forge->dropTable('users', true);
$this->forge->dropTable('auth_logins', true);
$this->forge->dropTable('auth_token_logins', true);
$this->forge->dropTable('auth_remember_tokens', true);
$this->forge->dropTable('auth_identities', true);
$this->forge->dropTable('auth_groups_users', true);
$this->forge->dropTable('auth_permissions_users', true);
$this->forge->dropTable('users', true);

$this->db->enableForeignKeyChecks();
}
Expand Down
29 changes: 29 additions & 0 deletions src/Entities/Cast/IntBoolCast.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace CodeIgniter\Shield\Entities\Cast;

use CodeIgniter\Entity\Cast\BaseCast;

/**
* Int Bool Cast
*
* DB column: int (0/1) <--> Class property: bool
*/
final class IntBoolCast extends BaseCast
{
/**
* @param int $value
*/
public static function get($value, array $params = []): bool
{
return (bool) $value;
}

/**
* @param bool|int|string $value
*/
public static function set($value, array $params = []): int
{
return (int) $value;
}
}
22 changes: 22 additions & 0 deletions src/Entities/Entity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace CodeIgniter\Shield\Entities;

use CodeIgniter\Entity\Entity as FrameworkEntity;
use CodeIgniter\Shield\Entities\Cast\IntBoolCast;

/**
* Base Entity
*/
abstract class Entity extends FrameworkEntity
{
/**
* Custom convert handlers
*
* @var array<string, string>
* @phpstan-var array<string, class-string>
*/
protected $castHandlers = [
'int_bool' => IntBoolCast::class,
];
}
4 changes: 1 addition & 3 deletions src/Entities/Login.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@

namespace CodeIgniter\Shield\Entities;

use CodeIgniter\Entity\Entity;

class Login extends Entity
{
/**
* @var array<string, string>
*/
protected $casts = [
'date' => 'datetime',
'success' => 'boolean',
'success' => 'int_bool',
];
}
3 changes: 1 addition & 2 deletions src/Entities/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace CodeIgniter\Shield\Entities;

use CodeIgniter\Database\Exceptions\DataException;
use CodeIgniter\Entity\Entity;
use CodeIgniter\Shield\Authentication\Authenticators\Session;
use CodeIgniter\Shield\Authentication\Traits\HasAccessTokens;
use CodeIgniter\Shield\Authorization\Traits\Authorizable;
Expand Down Expand Up @@ -41,7 +40,7 @@ class User extends Entity
*/
protected $casts = [
'id' => '?integer',
'active' => 'boolean',
'active' => 'int_bool',
'permissions' => 'array',
'groups' => 'array',
];
Expand Down
3 changes: 1 addition & 2 deletions src/Entities/UserIdentity.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace CodeIgniter\Shield\Entities;

use CodeIgniter\Entity\Entity;
use CodeIgniter\Shield\Authentication\Passwords;

/**
Expand All @@ -25,7 +24,7 @@ class UserIdentity extends Entity
* @var array<string, string>
*/
protected $casts = [
'force_reset' => 'boolean',
'force_reset' => 'int_bool',
];

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Models/CheckQueryReturnTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ private function checkQueryReturn(bool $return): void
$message = 'Query error: ' . $error['code'] . ', '
. $error['message'] . ', query: ' . $this->db->getLastQuery();

throw new DatabaseException($message, $error['code']);
throw new DatabaseException($message, (int) $error['code']);
}
}

Expand Down
4 changes: 2 additions & 2 deletions tests/Controllers/LoginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public function testLoginActionEmailSuccess(): void
$this->seeInDatabase('auth_logins', [
'identifier' => '[email protected]',
'user_id' => $this->user->id,
'success' => true,
'success' => 1,
]);
// Last Used date should have been set
$identity = $this->user->getEmailIdentity();
Expand Down Expand Up @@ -147,7 +147,7 @@ public function testLoginActionUsernameSuccess(): void
$this->seeInDatabase('auth_logins', [
'identifier' => $this->user->username,
'user_id' => $this->user->id,
'success' => true,
'success' => 1,
]);
// Last Used date should have been set
$identity = $this->user->getEmailIdentity();
Expand Down
32 changes: 27 additions & 5 deletions tests/Unit/UserModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,25 @@ public function testInsertUserObject(): void
]);
}

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

$user = $this->createNewUser();

$userArray = $user->toArray();
$id = $users->insert($userArray);
// Fix value type
$userArray['active'] = (int) $userArray['active'];

$id = $users->insert($userArray);

$this->dontSeeInDatabase('auth_identities', [
'user_id' => $id,
Expand All @@ -85,7 +96,7 @@ private function createNewUser(): User
$user->username = 'foo';
$user->email = '[email protected]';
$user->password = 'password';
$user->active = 0;
$user->active = false;

return $user;
}
Expand All @@ -100,7 +111,7 @@ public function testSaveUpdateUserObjectWithUserDataToUpdate(): void

$user->username = 'bar';
$user->email = '[email protected]';
$user->active = 1;
$user->active = true;

$users->save($user);

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

$user->username = 'bar';
$user->email = '[email protected]';
$user->active = 1;
$user->active = true;

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

Expand All @@ -138,6 +149,14 @@ public function testUpdateUserObjectWithUserDataToUpdate(): void
]);
}

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

$user->username = 'bar';
$user->email = '[email protected]';
$user->active = 1;
$user->active = true;

$userArray = $user->toArray();
// Fix value type
$userArray['active'] = (int) $userArray['active'];

$users->update(null, $userArray);

$this->dontSeeInDatabase('auth_identities', [
Expand Down
6 changes: 3 additions & 3 deletions tests/Unit/UserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ public function testUpdateEmail(): void
{
// Update user's email
$this->user->email = '[email protected]';
$this->user->active = 0;
$this->user->active = false;

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

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

$users = model(UserModel::class);
$users->save($this->user);
Expand Down