Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.
Merged
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ matrix:
- php: 7.2
env:
- DEPS=latest
- php: nightly
env:
- DEPS=latest

before_install:
- if [[ $TEST_COVERAGE != 'true' ]]; then phpenv config-rm xdebug.ini || return 0 ; fi
Expand Down
5 changes: 4 additions & 1 deletion src/ConfigProvider.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
/**
* @see https://github.com/zendframework/zend-expressive-authentication-oauth2 for the canonical source repository
* @copyright Copyright (c) 2017 Zend Technologies USA Inc. (https://www.zend.com)
* @copyright Copyright (c) 2017-2018 Zend Technologies USA Inc. (https://www.zend.com)
* @license https://github.com/zendframework/zend-expressive-authentication-oauth2/blob/master/LICENSE.md
* New BSD License
*/
Expand Down Expand Up @@ -31,6 +31,9 @@
use Zend\Expressive\Authentication\OAuth2\Grant\RefreshTokenGrantFactory;
use Zend\Expressive\Authentication\OAuth2\Repository\Pdo;

/**
* @codeCoverageIgnore
*/
class ConfigProvider
{
/**
Expand Down
11 changes: 8 additions & 3 deletions src/ConfigTrait.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
/**
* @see https://github.com/zendframework/zend-expressive-authentication-oauth2 for the canonical source repository
* @copyright Copyright (c) 2017 Zend Technologies USA Inc. (https://www.zend.com)
* @copyright Copyright (c) 2017-2018 Zend Technologies USA Inc. (https://www.zend.com)
* @license https://github.com/zendframework/zend-expressive-authentication-oauth2/blob/master/LICENSE.md
* New BSD License
*/
Expand Down Expand Up @@ -90,9 +90,14 @@ protected function getGrantsConfig(ContainerInterface $container) : array
{
$config = $container->get('config')['authentication'] ?? [];

if (empty($config['grants']) || ! is_array($config['grants'])) {
if (empty($config['grants'])) {
throw new InvalidConfigException(
'The grant value is missing in config authentication'
'The grants value is missing in config authentication and must be an array'
);
}
if (! is_array($config['grants'])) {
throw new InvalidConfigException(
'The grants must be an array value'
);
}

Expand Down
18 changes: 10 additions & 8 deletions src/Entity/TimestampableTrait.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
/**
* @see https://github.com/zendframework/zend-expressive-authentication-oauth2 for the canonical source repository
* @copyright Copyright (c) 2017 Zend Technologies USA Inc. (https://www.zend.com)
* @copyright Copyright (c) 2017-2018 Zend Technologies USA Inc. (https://www.zend.com)
* @license https://github.com/zendframework/zend-expressive-authentication-oauth2/blob/master/LICENSE.md
* New BSD License
*/
Expand All @@ -10,7 +10,8 @@

namespace Zend\Expressive\Authentication\OAuth2\Entity;

use DateTime;
use DateTimeImmutable;
use DateTimeInterface;
use DateTimeZone;

use function method_exists;
Expand All @@ -27,22 +28,22 @@ trait TimestampableTrait
*/
protected $updatedAt;

public function getCreatedAt() : DateTime
public function getCreatedAt() : DateTimeInterface
{
return $this->createdAt;
}

public function setCreatedAt(DateTime $createdAt) : void
public function setCreatedAt(DateTimeInterface $createdAt) : void
{
$this->createdAt = $createdAt;
}

public function getUpdatedAt() : DateTime
public function getUpdatedAt() : DateTimeInterface
{
return $this->updatedAt;
}

public function setUpdatedAt(DateTime $updatedAt) : void
public function setUpdatedAt(DateTimeInterface $updatedAt) : void
{
$this->updatedAt = $updatedAt;
}
Expand All @@ -54,9 +55,10 @@ public function setUpdatedAt(DateTime $updatedAt) : void
public function timestampOnCreate() : void
{
if (! $this->createdAt) {
$this->createdAt = new DateTime();
if (method_exists($this, 'getTimezone')) {
$this->createdAt->setTimezone(new DateTimeZone($this->getTimezone()->getValue()));
$this->createdAt = new DateTimeImmutable('now', new DateTimeZone($this->getTimezone()->getValue()));
} else {
$this->createdAt = new DateTimeImmutable();
}
}
}
Expand Down
162 changes: 162 additions & 0 deletions test/ConfigTraitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
<?php
/**
* @see https://github.com/zendframework/zend-expressive-authentication-oauth2 for the canonical source repository
* @copyright Copyright (c) 2018 Zend Technologies USA Inc. (https://www.zend.com)
* @license https://github.com/zendframework/zend-expressive-authentication-oauth2/blob/master/LICENSE.md
* New BSD License
*/

declare(strict_types=1);

namespace ZendTest\Expressive\Authentication\OAuth2;

use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;
use Zend\Expressive\Authentication\OAuth2\ConfigTrait;

class ConfigTraitTest extends TestCase
{
public function setUp()
{
$this->trait = $trait = new class {
use ConfigTrait;

public function proxy(string $name, ContainerInterface $container)
{
return $this->$name($container);
}
};
$this->config = [
'authentication' => [
'private_key' => 'xxx',
'encryption_key' => 'xxx',
'access_token_expire' => '3600',
'refresh_token_expire' => '3600',
'auth_code_expire' => '120',
'grants' => ['xxx']
]
];
$this->container = $this->prophesize(ContainerInterface::class);
$this->container
->get('config')
->willReturn($this->config);
}

/**
* @expectedException Zend\Expressive\Authentication\OAuth2\Exception\InvalidConfigException
*/
public function testGetPrivateKeyWhenNoConfigPresentWillResultInAnException()
{
$this->container
->get('config')
->willReturn([]);
$this->trait->proxy('getPrivateKey', $this->container->reveal());
}

public function testGetPrivateKey()
{
$result = $this->trait->proxy('getPrivateKey', $this->container->reveal());
$this->assertEquals($this->config['authentication']['private_key'], $result);
}

/**
* @expectedException Zend\Expressive\Authentication\OAuth2\Exception\InvalidConfigException
*/
public function testGetEncryptionKeyNoConfig()
{
$this->container
->get('config')
->willReturn([]);
$this->trait->proxy('getEncryptionKey', $this->container->reveal());
}

public function testGetEncryptionKey()
{
$result = $this->trait->proxy('getEncryptionKey', $this->container->reveal());
$this->assertEquals($this->config['authentication']['encryption_key'], $result);
}

/**
* @expectedException Zend\Expressive\Authentication\OAuth2\Exception\InvalidConfigException
*/
public function testGetAccessTokenExpireNoConfig()
{
$this->container
->get('config')
->willReturn([]);
$this->trait->proxy('getAccessTokenExpire', $this->container->reveal());
}

public function testGetAccessTokenExpire()
{
$result = $this->trait->proxy('getAccessTokenExpire', $this->container->reveal());
$this->assertEquals($this->config['authentication']['access_token_expire'], $result);
}

/**
* @expectedException Zend\Expressive\Authentication\OAuth2\Exception\InvalidConfigException
*/
public function testGetRefreshTokenExpireNoConfig()
{
$this->container
->get('config')
->willReturn([]);
$this->trait->proxy('getRefreshTokenExpire', $this->container->reveal());
}

public function testGetRefreshTokenExpire()
{
$result = $this->trait->proxy('getRefreshTokenExpire', $this->container->reveal());
$this->assertEquals($this->config['authentication']['refresh_token_expire'], $result);
}

/**
* @expectedException Zend\Expressive\Authentication\OAuth2\Exception\InvalidConfigException
*/
public function testGetAuthCodeExpireNoConfig()
{
$this->container
->get('config')
->willReturn([]);
$this->trait->proxy('getAuthCodeExpire', $this->container->reveal());
}

public function testGetAuthCodeExpire()
{
$result = $this->trait->proxy('getAuthCodeExpire', $this->container->reveal());
$this->assertEquals($this->config['authentication']['auth_code_expire'], $result);
}

/**
* @expectedException Zend\Expressive\Authentication\OAuth2\Exception\InvalidConfigException
*/
public function testGetGrantsConfigNoConfig()
{
$this->container
->get('config')
->willReturn([]);
$this->trait->proxy('getGrantsConfig', $this->container->reveal());
}

/**
* @expectedException Zend\Expressive\Authentication\OAuth2\Exception\InvalidConfigException
*/
public function testGetGrantsConfigNoArrayValue()
{
$this->container
->get('config')
->willReturn([
'authentication' => [
'grants' => 'xxx',
],
]);

$this->trait->proxy('getGrantsConfig', $this->container->reveal());
}

public function testGetGrantsConfig()
{
$result = $this->trait->proxy('getGrantsConfig', $this->container->reveal());
$this->assertEquals($this->config['authentication']['grants'], $result);
}
}
24 changes: 24 additions & 0 deletions test/Entity/AccessTokenEntityTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
/**
* @see https://github.com/zendframework/zend-expressive-authentication-oauth2 for the canonical source repository
* @copyright Copyright (c) 2018 Zend Technologies USA Inc. (https://www.zend.com)
* @license https://github.com/zendframework/zend-expressive-authentication-oauth2/blob/master/LICENSE.md
* New BSD License
*/

declare(strict_types=1);

namespace ZendTest\Expressive\Authentication\OAuth2\Entity;

use League\OAuth2\Server\Entities\AccessTokenEntityInterface;
use PHPUnit\Framework\TestCase;
use Zend\Expressive\Authentication\OAuth2\Entity\AccessTokenEntity;

class AccessTokenEntityTest extends TestCase
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need this test. If it's just testing that the constructor returns an instance of the class, that's something we know already.

If there's other behavior to test — e.g., default property values, property values based on constructor arguments, etc. — then tests make sense.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I remove the AccessTokenEntityTest we need to use @codeCoverageIgnore to omit this class from the code coverage. IMHO I think a test class like this, even if just checking for constructor, can be useful as a constrain for future changes. For instance, if someone changes the AccessTokenEntityInterface this test is able to intercept it.

{
public function testImplementsInstanceAccessTokenEntityInterface()
{
$entity = new AccessTokenEntity();
$this->assertInstanceOf(AccessTokenEntityInterface::class, $entity);
}
}
24 changes: 24 additions & 0 deletions test/Entity/AuthCodeEntityTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
/**
* @see https://github.com/zendframework/zend-expressive-authentication-oauth2 for the canonical source repository
* @copyright Copyright (c) 2018 Zend Technologies USA Inc. (https://www.zend.com)
* @license https://github.com/zendframework/zend-expressive-authentication-oauth2/blob/master/LICENSE.md
* New BSD License
*/

declare(strict_types=1);

namespace ZendTest\Expressive\Authentication\OAuth2\Entity;

use League\OAuth2\Server\Entities\AuthCodeEntityInterface;
use PHPUnit\Framework\TestCase;
use Zend\Expressive\Authentication\OAuth2\Entity\AuthCodeEntity;

class AuthCodeEntityTest extends TestCase
{
public function testImplementsInstanceAuthCodeEntityInterface()
{
$entity = new AuthCodeEntity();
$this->assertInstanceOf(AuthCodeEntityInterface::class, $entity);
}
}
67 changes: 67 additions & 0 deletions test/Entity/ClientEntityTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php
/**
* @see https://github.com/zendframework/zend-expressive-authentication-oauth2 for the canonical source repository
* @copyright Copyright (c) 2018 Zend Technologies USA Inc. (https://www.zend.com)
* @license https://github.com/zendframework/zend-expressive-authentication-oauth2/blob/master/LICENSE.md
* New BSD License
*/

declare(strict_types=1);

namespace ZendTest\Expressive\Authentication\OAuth2\Entity;

use League\OAuth2\Server\Entities\ClientEntityInterface;
use PHPUnit\Framework\TestCase;
use Zend\Expressive\Authentication\OAuth2\Entity\ClientEntity;

class ClientEntityTest extends TestCase
{
public function setUp()
{
$this->entity = new ClientEntity('foo', 'bar', 'http://localhost');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would write tests that ensure that the values passed to the constructor affected the state of the constructed entity.

}

public function testImplementsAuthCodeEntityInterface()
{
$this->assertInstanceOf(ClientEntityInterface::class, $this->entity);
}

public function testConstructorSetsIdentifier()
{
$this->assertSame('foo', $this->entity->getIdentifier());
}

public function testConstructorSetsName()
{
$this->assertSame('bar', $this->entity->getName());
}

public function testConstructorSetsRedirectUri()
{
$this->assertSame(['http://localhost'], $this->entity->getRedirectUri());
}

public function testSecret()
{
$this->entity->setSecret('secret');
$this->assertEquals('secret', $this->entity->getSecret());
}

public function testPersonalAccessClient()
{
$this->entity->setPersonalAccessClient(true);
$this->assertTrue($this->entity->hasPersonalAccessClient());

$this->entity->setPersonalAccessClient(false);
$this->assertFalse($this->entity->hasPersonalAccessClient());
}

public function testPasswordClient()
{
$this->entity->setPasswordClient(true);
$this->assertTrue($this->entity->hasPasswordClient());

$this->entity->setPasswordClient(false);
$this->assertFalse($this->entity->hasPasswordClient());
}
}
Loading