From a17301752dd5e442f738dd757c3de29a11b76615 Mon Sep 17 00:00:00 2001 From: Oliver Klee Date: Thu, 22 Jun 2017 16:24:54 +0200 Subject: [PATCH] [FEATURE] AdministratorToken model This is the bare-bone model without any associations, and it is not hooked up to Doctrine ORM yet. Both will come in later commits. --- .../Model/Identity/AdministratorToken.php | 105 ++++++++++++++ .../Model/Identity/AdministratorTokenTest.php | 136 ++++++++++++++++++ 2 files changed, 241 insertions(+) create mode 100644 Classes/Domain/Model/Identity/AdministratorToken.php create mode 100644 Tests/Unit/Domain/Model/Identity/AdministratorTokenTest.php diff --git a/Classes/Domain/Model/Identity/AdministratorToken.php b/Classes/Domain/Model/Identity/AdministratorToken.php new file mode 100644 index 00000000..4d823a66 --- /dev/null +++ b/Classes/Domain/Model/Identity/AdministratorToken.php @@ -0,0 +1,105 @@ + + */ +class AdministratorToken +{ + /** + * @var string + */ + const DEFAULT_EXPIRY = '+1 hour'; + + /** + * @var int + */ + private $id = 0; + + /** + * @var \DateTime + */ + private $expiry = null; + + /** + * @var string + */ + private $key = ''; + + /** + * The Constructor. + */ + public function __construct() + { + $this->setExpiry(new \DateTime()); + } + + /** + * @return int + */ + public function getId(): int + { + return $this->id; + } + + /** + * @return \DateTime + */ + public function getExpiry(): \DateTime + { + return $this->expiry; + } + + /** + * @param \DateTime $expiry + * + * @return void + */ + public function setExpiry(\DateTime $expiry) + { + $this->expiry = $expiry; + } + + /** + * @return string + */ + public function getKey(): string + { + return $this->key; + } + + /** + * @param string $key + * + * @return void + */ + public function setKey(string $key) + { + $this->key = $key; + } + + /** + * Generates a new, random key. + * + * @return void + */ + public function generateKey() + { + $key = md5(random_bytes(256)); + $this->setKey($key); + } + + /** + * Generates and sets an expiry one hour in the future. + * + * @return void + */ + public function generateExpiry() + { + $this->setExpiry(new \DateTime(self::DEFAULT_EXPIRY)); + } +} diff --git a/Tests/Unit/Domain/Model/Identity/AdministratorTokenTest.php b/Tests/Unit/Domain/Model/Identity/AdministratorTokenTest.php new file mode 100644 index 00000000..59524b59 --- /dev/null +++ b/Tests/Unit/Domain/Model/Identity/AdministratorTokenTest.php @@ -0,0 +1,136 @@ + + */ +class AdministratorTokenTest extends TestCase +{ + /** + * @var AdministratorToken + */ + private $subject = null; + + protected function setUp() + { + $this->subject = new AdministratorToken(); + } + + /** + * @test + */ + public function getIdInitiallyReturnsZero() + { + self::assertSame(0, $this->subject->getId()); + } + + /** + * @test + */ + public function getIdReturnsId() + { + $id = 123456; + $this->setSubjectId($id); + + self::assertSame($id, $this->subject->getId()); + } + + /** + * Sets the (private) ID of $this->subject. + * + * @param int $id + * @return void + * @internal param AdministratorToken $subject + */ + private function setSubjectId(int $id) + { + $reflectionObject = new \ReflectionObject($this->subject); + $reflectionProperty = $reflectionObject->getProperty('id'); + $reflectionProperty->setAccessible(true); + $reflectionProperty->setValue($this->subject, $id); + } + + /** + * @test + */ + public function getKeyInitiallyReturnsEmptyString() + { + self::assertSame('', $this->subject->getKey()); + } + + /** + * @test + */ + public function setKeySetsKey() + { + $value = 'Club-Mate'; + $this->subject->setKey($value); + + self::assertSame($value, $this->subject->getKey()); + } + + /** + * @test + */ + public function getExpiryInitiallyReturnsDateTime() + { + self::assertInstanceOf(\DateTime::class, $this->subject->getExpiry()); + } + + /** + * @test + */ + public function setExpirySetsExpiry() + { + $expiry = new \DateTime(); + $this->subject->setExpiry($expiry); + + self::assertSame($expiry, $this->subject->getExpiry()); + } + + /** + * @test + */ + public function generateExpirySetsExpiryOneHourInTheFuture() + { + $expectedExpiry = new \DateTime('+1 hour'); + + $this->subject->generateExpiry(); + + $actualExpiry = $this->subject->getExpiry(); + $difference = $actualExpiry->diff($expectedExpiry, true); + $differenceInSeconds = $difference->s + $difference->i * 60 + $difference->h * 3600; + self::assertLessThan(2, $differenceInSeconds); + } + + /** + * @test + */ + public function generateKeyCreates32CharacterKey() + { + $this->subject->generateKey(); + + self::assertRegExp('/^[a-z0-9]{32}$/', $this->subject->getKey()); + } + + /** + * @test + */ + public function generateKeyCreatesDifferentKeysForEachCall() + { + $this->subject->generateKey(); + $firstKey = $this->subject->getKey(); + + $this->subject->generateKey(); + $secondKey = $this->subject->getKey(); + + self::assertNotSame($firstKey, $secondKey); + } +}