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 9f672be commit d681b49
Showing 1 changed file with 137 additions and 0 deletions.
137 changes: 137 additions & 0 deletions tests/Commands/UserTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
<?php

declare(strict_types=1);

namespace Tests\Commands;

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

/**
* @internal
*/
final class UserTest extends DatabaseTestCase
{
private $streamFilter;

Check failure on line 18 in tests/Commands/UserTest.php

View workflow job for this annotation

GitHub Actions / phpstan / PHP 8.1 Static Analysis

Property Tests\Commands\UserTest::$streamFilter is unused.

Check failure on line 18 in tests/Commands/UserTest.php

View workflow job for this annotation

GitHub Actions / phpstan / PHP 7.4 Static Analysis

Property Tests\Commands\UserTest::$streamFilter is unused.

Check failure on line 18 in tests/Commands/UserTest.php

View workflow job for this annotation

GitHub Actions / phpstan / PHP 8.2 Static Analysis

Property Tests\Commands\UserTest::$streamFilter is unused.

Check failure on line 18 in tests/Commands/UserTest.php

View workflow job for this annotation

GitHub Actions / phpstan / PHP 8.0 Static Analysis

Property Tests\Commands\UserTest::$streamFilter is unused.

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.
$user = fake(UserModel::class, ['username' => 'user2']);
$user->createEmailIdentity([

Check failure on line 84 in tests/Commands/UserTest.php

View workflow job for this annotation

GitHub Actions / phpstan / PHP 8.1 Static Analysis

Cannot call method createEmailIdentity() on array|object.

Check failure on line 84 in tests/Commands/UserTest.php

View workflow job for this annotation

GitHub Actions / phpstan / PHP 7.4 Static Analysis

Cannot call method createEmailIdentity() on array|object.

Check failure on line 84 in tests/Commands/UserTest.php

View workflow job for this annotation

GitHub Actions / phpstan / PHP 8.2 Static Analysis

Cannot call method createEmailIdentity() on array|object.

Check failure on line 84 in tests/Commands/UserTest.php

View workflow job for this annotation

GitHub Actions / phpstan / PHP 8.0 Static Analysis

Cannot call method createEmailIdentity() on array|object.
'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.
$user = fake(UserModel::class, ['username' => 'user3', 'active' => 1]);
$user->createEmailIdentity([

Check failure on line 113 in tests/Commands/UserTest.php

View workflow job for this annotation

GitHub Actions / phpstan / PHP 8.1 Static Analysis

Cannot call method createEmailIdentity() on array|object.

Check failure on line 113 in tests/Commands/UserTest.php

View workflow job for this annotation

GitHub Actions / phpstan / PHP 7.4 Static Analysis

Cannot call method createEmailIdentity() on array|object.

Check failure on line 113 in tests/Commands/UserTest.php

View workflow job for this annotation

GitHub Actions / phpstan / PHP 8.2 Static Analysis

Cannot call method createEmailIdentity() on array|object.

Check failure on line 113 in tests/Commands/UserTest.php

View workflow job for this annotation

GitHub Actions / phpstan / PHP 8.0 Static Analysis

Cannot call method createEmailIdentity() on array|object.
'email' => '[email protected]',
'password' => 'secret123',
]);

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

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

$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 d681b49

Please sign in to comment.