generated from yiisoft/package-template
-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Sergei Predvoditelev <[email protected]>
- Loading branch information
Showing
4 changed files
with
81 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Csrf; | ||
|
||
use Yiisoft\Security\Random; | ||
|
||
/** | ||
* `StubCsrfToken` represents a simple implementation of `CsrfTokenInterface`. | ||
* | ||
* This implementation simply stores and returns a token string. It does not perform any additional validation. | ||
* It is primarily used for testing or as a placeholder implementation. | ||
*/ | ||
final class StubCsrfToken implements CsrfTokenInterface | ||
{ | ||
private string $token; | ||
|
||
public function __construct(?string $token = null) | ||
{ | ||
if (null === $token) { | ||
$token = Random::string(); | ||
} | ||
$this->token = $token; | ||
} | ||
|
||
public function getValue(): string | ||
{ | ||
return $this->token; | ||
} | ||
|
||
public function validate(string $token): bool | ||
{ | ||
return $this->token === $token; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Csrf\Tests; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Yiisoft\Csrf\StubCsrfToken; | ||
|
||
final class StubCsrfTokenTest extends TestCase | ||
{ | ||
public function testValue(): void | ||
{ | ||
$stubToken = new StubCsrfToken('test'); | ||
$this->assertSame('test', $stubToken->getValue()); | ||
} | ||
|
||
public function testValidate(): void | ||
{ | ||
$stubToken = new StubCsrfToken('test'); | ||
$this->assertTrue($stubToken->validate('test')); | ||
$this->assertFalse($stubToken->validate('other')); | ||
} | ||
|
||
public function testEmptyToken(): void | ||
{ | ||
$stubToken = new StubCsrfToken(); | ||
$token = $stubToken->getValue(); | ||
$this->assertNotEmpty($token); | ||
$this->assertTrue($stubToken->validate($token)); | ||
} | ||
} |