Skip to content

Commit

Permalink
Improve coverage (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
IonBazan committed Jul 10, 2020
1 parent c28377b commit a6aaea6
Show file tree
Hide file tree
Showing 14 changed files with 71 additions and 36 deletions.
9 changes: 7 additions & 2 deletions grumphp.yml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,16 @@ parameters:
verbose: true
metadata:
priority: 3000
clover_coverage:
clover_file: build/logs/clover.xml
level: 80
metadata:
priority: 2000
infection:
threads: 10
test_framework: phpspec
configuration: infection.json.dist
min_msi: 50
min_covered_msi: 50
min_msi: 70
min_covered_msi: 90
metadata:
priority: 2000
7 changes: 6 additions & 1 deletion phpspec.yml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ extensions:
- clover
- php
- text
- html
output:
html: build/coverage
clover: build/logs/clover.xml
php: build/coverage.php
php: build/coverage.php
whitelist:
- src
blacklist:
- src/Resources
6 changes: 0 additions & 6 deletions spec/EcPhp/CasBundle/Cas.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,6 @@ public static function getHttpClientMock()
return new MockHttpClient($callback);
}

/**
* @return \EcPhp\CasLib\Configuration\Properties
*/
public static function getTestProperties(): CasProperties
{
return new CasProperties([
Expand Down Expand Up @@ -234,9 +231,6 @@ public static function getTestProperties(): CasProperties
]);
}

/**
* @return \EcPhp\CasLib\Configuration\Properties
*/
public static function getTestPropertiesWithPgtUrl(): CasProperties
{
$properties = self::getTestProperties()->all();
Expand Down
22 changes: 22 additions & 0 deletions spec/EcPhp/CasBundle/Controller/LoginSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,32 @@
namespace spec\EcPhp\CasBundle\Controller;

use EcPhp\CasBundle\Controller\Login;
use EcPhp\CasLib\CasInterface;
use PhpSpec\ObjectBehavior;
use Psr\Http\Message\ResponseInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Security;

class LoginSpec extends ObjectBehavior
{
public function it_can_be_invoked(CasInterface $cas, Security $security, ResponseInterface $casResponse)
{
$request = Request::create('');
$response = $this
->__invoke($request, $cas, $security);
$response->shouldBeAnInstanceOf(RedirectResponse::class);
$response->headers->get('location')->shouldBe('/');

$casResponse->getHeaderLine('location')->willReturn('https://foo.com');
$cas->login(['renew' => false])->willReturn($casResponse);

$response = $this
->__invoke($request, $cas, $security);
$response->shouldBeAnInstanceOf(RedirectResponse::class);
$response->headers->get('location')->shouldBe('https://foo.com');
}

public function it_is_initializable()
{
$this->shouldHaveType(Login::class);
Expand Down
8 changes: 8 additions & 0 deletions spec/EcPhp/CasBundle/Controller/LogoutSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use EcPhp\CasBundle\Controller\Logout;
use EcPhp\CasLib\Cas;
use EcPhp\CasLib\CasInterface;
use Nyholm\Psr7\Factory\Psr17Factory;
use Nyholm\Psr7\ServerRequest;
use PhpSpec\ObjectBehavior;
Expand Down Expand Up @@ -54,4 +55,11 @@ public function it_is_initializable()
{
$this->shouldHaveType(Logout::class);
}

public function it_redirects_to_index(CasInterface $cas, TokenStorageInterface $tokenStorage)
{
$response = $this->__invoke($cas, $tokenStorage);
$response->shouldBeAnInstanceOf(RedirectResponse::class);
$response->headers->get('location')->shouldReturn('/');
}
}
2 changes: 1 addition & 1 deletion src/Controller/Homepage.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ final class Homepage
/**
* @return \Symfony\Component\HttpFoundation\Response
*/
public function __invoke()
public function __invoke(): Response
{
$body = <<< 'EOF'
<p>You have been redirected here by default. You are most probably using the default CAS configuration.</p>
Expand Down
4 changes: 2 additions & 2 deletions src/Controller/Login.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ final class Login
* @param \EcPhp\CasLib\CasInterface $cas
* @param \Symfony\Component\Security\Core\Security $security
*
* @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response
* @return \Symfony\Component\HttpFoundation\RedirectResponse
*/
public function __invoke(
Request $request,
CasInterface $cas,
Security $security
) {
): RedirectResponse {
$parameters = $request->query->all() + [
'renew' => null !== $security->getUser(),
];
Expand Down
4 changes: 2 additions & 2 deletions src/Controller/Logout.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ final class Logout
* @param \EcPhp\CasLib\CasInterface $cas
* @param \Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface $tokenStorage
*
* @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response
* @return \Symfony\Component\HttpFoundation\RedirectResponse
*/
public function __invoke(
CasInterface $cas,
TokenStorageInterface $tokenStorage
) {
): RedirectResponse {
if (null !== $response = $cas->logout()) {
$tokenStorage->setToken();

Expand Down
2 changes: 1 addition & 1 deletion src/Controller/ProxyCallback.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ final class ProxyCallback
*
* @return \Symfony\Component\HttpFoundation\Response
*/
public function __invoke(CasInterface $casProtocol, HttpFoundationFactoryInterface $httpFoundationFactory)
public function __invoke(CasInterface $casProtocol, HttpFoundationFactoryInterface $httpFoundationFactory): Response
{
if (null !== $response = $casProtocol->handleProxyCallback()) {
return $httpFoundationFactory->createResponse($response);
Expand Down
2 changes: 1 addition & 1 deletion src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Configuration implements ConfigurationInterface
/**
* {@inheritdoc}
*/
public function getConfigTreeBuilder()
public function getConfigTreeBuilder(): TreeBuilder
{
$treeBuilder = new TreeBuilder('cas');

Expand Down
4 changes: 3 additions & 1 deletion src/Resources/config/routes/routes.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
<?php

use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;
declare(strict_types=1);

use EcPhp\CasBundle\Controller\Homepage;
use EcPhp\CasBundle\Controller\Login;
use EcPhp\CasBundle\Controller\Logout;
use EcPhp\CasBundle\Controller\ProxyCallback;
use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;

return static function (RoutingConfigurator $routes) {
$routes
Expand Down
19 changes: 10 additions & 9 deletions src/Security/CasGuardAuthenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use EcPhp\CasLib\Introspection\Introspector;
use EcPhp\CasLib\Utils\Uri;
use InvalidArgumentException;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Symfony\Bridge\PsrHttpMessage\HttpMessageFactoryInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
Expand Down Expand Up @@ -48,7 +49,7 @@ public function __construct(
/**
* {@inheritdoc}
*/
public function checkCredentials($credentials, UserInterface $user)
public function checkCredentials($credentials, UserInterface $user): bool
{
try {
$introspect = Introspector::detect($credentials);
Expand All @@ -68,7 +69,7 @@ public function checkCredentials($credentials, UserInterface $user)
/**
* {@inheritdoc}
*/
public function getCredentials(Request $request)
public function getCredentials(Request $request): ?ResponseInterface
{
$response = $this
->cas->withServerRequest($this->toPsr($request))
Expand All @@ -84,7 +85,7 @@ public function getCredentials(Request $request)
/**
* {@inheritdoc}
*/
public function getUser($credentials, UserProviderInterface $userProvider)
public function getUser($credentials, UserProviderInterface $userProvider): ?UserInterface
{
if (false === ($userProvider instanceof CasUserProviderInterface)) {
throw new AuthenticationException('Unable to load the user through the given User Provider.');
Expand All @@ -102,7 +103,7 @@ public function getUser($credentials, UserProviderInterface $userProvider)
/**
* {@inheritdoc}
*/
public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
public function onAuthenticationFailure(Request $request, AuthenticationException $exception): ?Response
{
$uri = $this->toPsr($request)->getUri();

Expand All @@ -125,7 +126,7 @@ public function onAuthenticationFailure(Request $request, AuthenticationExceptio
/**
* {@inheritdoc}
*/
public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $providerKey)
public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $providerKey): Response
{
return new RedirectResponse(
(string) Uri::removeParams(
Expand All @@ -139,7 +140,7 @@ public function onAuthenticationSuccess(Request $request, TokenInterface $token,
/**
* {@inheritdoc}
*/
public function onLogoutSuccess(Request $request)
public function onLogoutSuccess(Request $request): Response
{
$response = $this
->cas
Expand All @@ -158,7 +159,7 @@ public function onLogoutSuccess(Request $request)
/**
* {@inheritdoc}
*/
public function start(Request $request, ?AuthenticationException $authException = null)
public function start(Request $request, ?AuthenticationException $authException = null): Response
{
if (true === $request->isXmlHttpRequest()) {
return new JsonResponse(
Expand All @@ -184,7 +185,7 @@ public function start(Request $request, ?AuthenticationException $authException
/**
* {@inheritdoc}
*/
public function supports(Request $request)
public function supports(Request $request): bool
{
return $this
->cas
Expand All @@ -195,7 +196,7 @@ public function supports(Request $request)
/**
* {@inheritdoc}
*/
public function supportsRememberMe()
public function supportsRememberMe(): bool
{
return false;
}
Expand Down
10 changes: 5 additions & 5 deletions src/Security/Core/User/CasUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function getAttributes(): array
/**
* {@inheritdoc}
*/
public function getPassword()
public function getPassword(): ?string
{
return null;
}
Expand All @@ -76,15 +76,15 @@ public function getPgt(): ?string
/**
* {@inheritdoc}
*/
public function getRoles()
public function getRoles(): array
{
return ['ROLE_CAS_AUTHENTICATED'];
}

/**
* {@inheritdoc}
*/
public function getSalt()
public function getSalt(): ?string
{
return null;
}
Expand All @@ -100,7 +100,7 @@ public function getUser(): string
/**
* {@inheritdoc}
*/
public function getUsername()
public function getUsername(): string
{
return $this->getUser();
}
Expand All @@ -110,7 +110,7 @@ public function getUsername()
*
* @return array<mixed>
*/
private function getStorage()
private function getStorage(): array
{
return $this->storage;
}
Expand Down
8 changes: 3 additions & 5 deletions src/Security/Core/User/CasUserProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ public function loadUserByResponse(ResponseInterface $response): CasUserInterfac
/**
* {@inheritdoc}
*/
public function loadUserByUsername(string $username)
public function loadUserByUsername(string $username): UserInterface
{
throw new UnsupportedUserException(sprintf('Username "%s" does not exist.', $username));
}

/**
* {@inheritdoc}
*/
public function refreshUser(UserInterface $user)
public function refreshUser(UserInterface $user): UserInterface
{
if (!$user instanceof CasUserInterface) {
throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', get_class($user)));
Expand All @@ -56,10 +56,8 @@ public function refreshUser(UserInterface $user)

/**
* {@inheritdoc}
*
* @return bool
*/
public function supportsClass(string $class)
public function supportsClass(string $class): bool
{
return CasUser::class === $class;
}
Expand Down

0 comments on commit a6aaea6

Please sign in to comment.