Skip to content

Commit

Permalink
test: add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kenjis committed Sep 15, 2023
1 parent 7d8ae25 commit a62f529
Showing 1 changed file with 138 additions and 0 deletions.
138 changes: 138 additions & 0 deletions tests/Commands/UserTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
<?php

declare(strict_types=1);

namespace Tests\Commands;

use CodeIgniter\CodeIgniter;
use CodeIgniter\Shield\Entities\User;
use CodeIgniter\Shield\Models\UserModel;
use CodeIgniter\Test\Filters\CITestStreamFilter;
use CodeIgniter\Test\PhpStreamWrapper;
use Tests\Support\DatabaseTestCase;

/**
* @internal
*/
final class UserTest extends DatabaseTestCase
{
protected function setUp(): void
{
parent::setUp();

if (version_compare(CodeIgniter::CI_VERSION, '4.3.0', '>=')) {
CITestStreamFilter::registration();
CITestStreamFilter::addOutputFilter();
} else {
$this->markTestSkipped('Cannot write tests with CI 4.2.x');
}
}

protected function tearDown(): void
{
parent::tearDown();

CITestStreamFilter::removeOutputFilter();
CITestStreamFilter::removeErrorFilter();
}

private function setUserInput(string $input): void
{
// Register the PhpStreamWrapper.
PhpStreamWrapper::register();

PhpStreamWrapper::setContent($input);
}

private function resetUserInput(): void
{
// Restore php protocol wrapper.
PhpStreamWrapper::restore();
}

public function testCreate(): void
{
$input = 'Secret Passw0rd!';
$this->setUserInput($input);

command('shield:user create -n user1 -e [email protected]');

$this->resetUserInput();

$this->assertStringContainsString(
'User user1 created',
CITestStreamFilter::$buffer
);

$users = model(UserModel::class);
$user = $users->findByCredentials(['email' => '[email protected]']);
$this->seeInDatabase($this->tables['identities'], [
'user_id' => $user->id,
'secret' => '[email protected]',
]);
$this->seeInDatabase($this->tables['users'], [
'id' => $user->id,
'active' => 0,
]);
}

public function testActivate(): void
{
// Create a user.
/** @var User $user */
$user = fake(UserModel::class, ['username' => 'user2']);
$user->createEmailIdentity([
'email' => '[email protected]',
'password' => 'secret123',
]);

$input = 'y';
$this->setUserInput($input);

command('shield:user activate -n user2');

$this->resetUserInput();

$this->assertStringContainsString(
'User user2 activated',
CITestStreamFilter::$buffer
);

$users = model(UserModel::class);
$user = $users->findByCredentials(['email' => '[email protected]']);
$this->seeInDatabase($this->tables['users'], [
'id' => $user->id,
'active' => 1,
]);
}

public function testDeactivate(): void
{
// Create a user.
/** @var User $user */
$user = fake(UserModel::class, ['username' => 'user3', 'active' => 1]);
$user->createEmailIdentity([
'email' => '[email protected]',
'password' => 'secret123',
]);

$input = 'y';
$this->setUserInput($input);

command('shield:user deactivate -n user3');

$this->resetUserInput();

$this->assertStringContainsString(
'User user3 deactivated',
CITestStreamFilter::$buffer
);

$users = model(UserModel::class);
$user = $users->findByCredentials(['email' => '[email protected]']);
$this->seeInDatabase($this->tables['users'], [
'id' => $user->id,
'active' => 0,
]);
}
}

0 comments on commit a62f529

Please sign in to comment.