Skip to content

Commit

Permalink
Merge pull request #61 from tarlepp/phpunit8
Browse files Browse the repository at this point in the history
Refactor/PHPUnit 8
  • Loading branch information
tarlepp committed Jun 6, 2019
2 parents 0a24380 + fdf66e6 commit ba39de1
Show file tree
Hide file tree
Showing 52 changed files with 1,855 additions and 1,004 deletions.
3 changes: 3 additions & 0 deletions .dockerignore
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -32,3 +32,6 @@ infection.json


### Vendor bin dependencies ### Vendor bin dependencies
/vendor-bin/*/vendor/ /vendor-bin/*/vendor/

### PHPUnit
.phpunit.result.cache
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -35,3 +35,6 @@ infection.json
###> lexik/jwt-authentication-bundle ### ###> lexik/jwt-authentication-bundle ###
/config/jwt/*.pem /config/jwt/*.pem
###< lexik/jwt-authentication-bundle ### ###< lexik/jwt-authentication-bundle ###

### PHPUnit
.phpunit.result.cache
4 changes: 2 additions & 2 deletions composer.json
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -115,11 +115,11 @@
"symfony/debug": "<3.3" "symfony/debug": "<3.3"
}, },
"extra": { "extra": {
"allow-contrib": "true",
"projectTitle": "Symfony Flex Backend", "projectTitle": "Symfony Flex Backend",
"symfony": { "symfony": {
"id": "01BFA135DXTPRA5CR250NTJJJP", "id": "01BFA135DXTPRA5CR250NTJJJP",
"allow-contrib": "true", "require": "4.3.1"
"require": "4.3.0"
} }
} }
} }
395 changes: 198 additions & 197 deletions composer.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion phpunit.xml.dist
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@


<!-- https://phpunit.de/manual/current/en/appendixes.configuration.html --> <!-- https://phpunit.de/manual/current/en/appendixes.configuration.html -->
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" <phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/6.3/phpunit.xsd" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/8.1/phpunit.xsd"
backupGlobals="false" backupGlobals="false"
colors="true" colors="true"
bootstrap="tests/bootstrap.php" bootstrap="tests/bootstrap.php"
Expand Down
1 change: 1 addition & 0 deletions psalm.xml
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@


<ignoreFiles> <ignoreFiles>
<directory name="vendor" /> <directory name="vendor" />
<directory name="src/Utils/Tests" />
<file name="src/Kernel.php" /> <file name="src/Kernel.php" />
</ignoreFiles> </ignoreFiles>
</projectFiles> </projectFiles>
Expand Down
4 changes: 2 additions & 2 deletions src/Utils/Tests/Auth.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
namespace App\Utils\Tests; namespace App\Utils\Tests;


use App\Utils\JSON; use App\Utils\JSON;
use Symfony\Bundle\FrameworkBundle\Client; use Symfony\Bundle\FrameworkBundle\KernelBrowser;
use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
use Throwable; use Throwable;
Expand Down Expand Up @@ -138,7 +138,7 @@ private function getToken(string $username, string $password): string
if (!array_key_exists($hash, $cache)) { if (!array_key_exists($hash, $cache)) {
// Get client // Get client
/** @noinspection MissingService */ /** @noinspection MissingService */
/** @var Client $client */ /** @var KernelBrowser $client */
$client = $this->testContainer->get('test.client'); $client = $this->testContainer->get('test.client');


// Create request to make login using given credentials // Create request to make login using given credentials
Expand Down
26 changes: 13 additions & 13 deletions src/Utils/Tests/RestTraitTestCase.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@


namespace App\Utils\Tests; namespace App\Utils\Tests;


use Exception;
use Generator; use Generator;
use Ramsey\Uuid\Uuid; use Ramsey\Uuid\Uuid;
use Throwable;
use function array_merge; use function array_merge;


/** /**
Expand Down Expand Up @@ -51,7 +51,7 @@ abstract public function getInvalidUsers(): Generator;
* @param string|null $password * @param string|null $password
* @param string|null $method * @param string|null $method
* *
* @throws Exception * @throws Throwable
*/ */
public function testThatCountRouteDoesNotAllowNotSupportedHttpMethods( public function testThatCountRouteDoesNotAllowNotSupportedHttpMethods(
?string $username = null, ?string $username = null,
Expand All @@ -76,7 +76,7 @@ public function testThatCountRouteDoesNotAllowNotSupportedHttpMethods(
* @param string|null $password * @param string|null $password
* @param string|null $method * @param string|null $method
* *
* @throws Exception * @throws Throwable
*/ */
public function testThatCountRouteWorksWithAllowedHttpMethods( public function testThatCountRouteWorksWithAllowedHttpMethods(
?string $username = null, ?string $username = null,
Expand All @@ -101,7 +101,7 @@ public function testThatCountRouteWorksWithAllowedHttpMethods(
* @param string|null $password * @param string|null $password
* @param string|null $method * @param string|null $method
* *
* @throws Exception * @throws Throwable
*/ */
public function testThatCountRouteDoesNotAllowInvalidUser( public function testThatCountRouteDoesNotAllowInvalidUser(
?string $username = null, ?string $username = null,
Expand All @@ -126,7 +126,7 @@ public function testThatCountRouteDoesNotAllowInvalidUser(
* @param string|null $password * @param string|null $password
* @param string|null $method * @param string|null $method
* *
* @throws Exception * @throws Throwable
*/ */
public function testThatRootRouteDoesNotAllowNotSupportedHttpMethods( public function testThatRootRouteDoesNotAllowNotSupportedHttpMethods(
?string $username = null, ?string $username = null,
Expand All @@ -151,7 +151,7 @@ public function testThatRootRouteDoesNotAllowNotSupportedHttpMethods(
* @param string|null $password * @param string|null $password
* @param string|null $method * @param string|null $method
* *
* @throws Exception * @throws Throwable
*/ */
public function testThatRootRouteWorksWithAllowedHttpMethods( public function testThatRootRouteWorksWithAllowedHttpMethods(
?string $username = null, ?string $username = null,
Expand All @@ -176,7 +176,7 @@ public function testThatRootRouteWorksWithAllowedHttpMethods(
* @param string|null $password * @param string|null $password
* @param string|null $method * @param string|null $method
* *
* @throws Exception * @throws Throwable
*/ */
public function testThatRootRouteDoesNotAllowInvalidUser( public function testThatRootRouteDoesNotAllowInvalidUser(
?string $username = null, ?string $username = null,
Expand All @@ -201,7 +201,7 @@ public function testThatRootRouteDoesNotAllowInvalidUser(
* @param string|null $password * @param string|null $password
* @param string|null $method * @param string|null $method
* *
* @throws Exception * @throws Throwable
*/ */
public function testThatRootRouteWithIdDoesNotAllowNotSupportedHttpMethods( public function testThatRootRouteWithIdDoesNotAllowNotSupportedHttpMethods(
?string $username = null, ?string $username = null,
Expand All @@ -228,7 +228,7 @@ public function testThatRootRouteWithIdDoesNotAllowNotSupportedHttpMethods(
* @param string|null $password * @param string|null $password
* @param string|null $method * @param string|null $method
* *
* @throws Exception * @throws Throwable
*/ */
public function testThatRootRouteWithIdWorksWithAllowedHttpMethods( public function testThatRootRouteWithIdWorksWithAllowedHttpMethods(
?string $username = null, ?string $username = null,
Expand All @@ -255,7 +255,7 @@ public function testThatRootRouteWithIdWorksWithAllowedHttpMethods(
* @param string|null $password * @param string|null $password
* @param string|null $method * @param string|null $method
* *
* @throws Exception * @throws Throwable
*/ */
public function testThatRootRouteWithIdDoesNotAllowInvalidUser( public function testThatRootRouteWithIdDoesNotAllowInvalidUser(
?string $username = null, ?string $username = null,
Expand All @@ -282,7 +282,7 @@ public function testThatRootRouteWithIdDoesNotAllowInvalidUser(
* @param string|null $password * @param string|null $password
* @param string|null $method * @param string|null $method
* *
* @throws Exception * @throws Throwable
*/ */
public function testThatIdsRouteDoesNotAllowNotSupportedHttpMethods( public function testThatIdsRouteDoesNotAllowNotSupportedHttpMethods(
?string $username = null, ?string $username = null,
Expand All @@ -307,7 +307,7 @@ public function testThatIdsRouteDoesNotAllowNotSupportedHttpMethods(
* @param string|null $password * @param string|null $password
* @param string|null $method * @param string|null $method
* *
* @throws Exception * @throws Throwable
*/ */
public function testThatIdsRouteWorksWithAllowedHttpMethods( public function testThatIdsRouteWorksWithAllowedHttpMethods(
?string $username = null, ?string $username = null,
Expand All @@ -332,7 +332,7 @@ public function testThatIdsRouteWorksWithAllowedHttpMethods(
* @param string|null $password * @param string|null $password
* @param string|null $method * @param string|null $method
* *
* @throws Exception * @throws Throwable
*/ */
public function testThatIdsRouteDoesNotAllowInvalidUser( public function testThatIdsRouteDoesNotAllowInvalidUser(
?string $username = null, ?string $username = null,
Expand Down
16 changes: 10 additions & 6 deletions tests/Functional/Maker/RestApiMakerTest.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use App\Maker\RestApiMaker; use App\Maker\RestApiMaker;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Bundle\MakerBundle\Command\MakerCommand; use Symfony\Bundle\MakerBundle\Command\MakerCommand;
use Symfony\Bundle\MakerBundle\Exception\RuntimeCommandException;
use Symfony\Bundle\MakerBundle\FileManager; use Symfony\Bundle\MakerBundle\FileManager;
use Symfony\Bundle\MakerBundle\Generator; use Symfony\Bundle\MakerBundle\Generator;
use Symfony\Bundle\MakerBundle\Util\AutoloaderUtil; use Symfony\Bundle\MakerBundle\Util\AutoloaderUtil;
Expand Down Expand Up @@ -39,20 +40,23 @@ public function testThatCommandRunsWithSuccess(): void


$tester = new CommandTester($command); $tester = new CommandTester($command);
$tester->setInputs($inputs); $tester->setInputs($inputs);
$tester->execute(array()); $tester->execute([]);


static::assertContains('Success', $tester->getDisplay()); static::assertStringContainsString('Success', $tester->getDisplay());


// Clean up files // Clean up files
$this->fs->remove($maker->getCreatedFiles()); $this->fs->remove($maker->getCreatedFiles());
} }


/**
* @expectedException \Symfony\Bundle\MakerBundle\Exception\RuntimeCommandException
* @expectedExceptionMessage "App\Controller\123BookController" is not valid as a PHP class name (it must start with a letter or underscore, followed by any number of letters, numbers, or underscores)
*/
public function testThatCommandReturnsAnErrorWithInvalidInput(): void public function testThatCommandReturnsAnErrorWithInvalidInput(): void
{ {
$this->expectException(RuntimeCommandException::class);

/** @codingStandardsIgnoreStart */
$this->expectExceptionMessage('"App\Controller\123BookController" is not valid as a PHP class name (it must start with a letter or underscore, followed by any number of letters, numbers, or underscores)');
/** @codingStandardsIgnoreEnd */


$maker = new RestApiMaker(); $maker = new RestApiMaker();
$inputs = [ $inputs = [
'123Book', '123Book',
Expand Down
16 changes: 8 additions & 8 deletions tests/Functional/Security/Provider/ApiKeyUserProviderTest.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
use App\Security\RolesService; use App\Security\RolesService;
use Generator; use Generator;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
use Symfony\Component\Security\Core\User\User; use Symfony\Component\Security\Core\User\User;
use Throwable; use Throwable;
use function array_map; use function array_map;
Expand Down Expand Up @@ -65,15 +67,14 @@ public function testThatGetApiKeyReturnsNullForInvalidToken(string $shortRole):
unset($apiKey); unset($apiKey);
} }


/** @noinspection PhpFullyQualifiedNameUsageInspection */
/** /**
* @expectedException \Symfony\Component\Security\Core\Exception\UsernameNotFoundException
* @expectedExceptionMessage API key is not valid
*
* @throws Throwable * @throws Throwable
*/ */
public function testThatLoadUserByUsernameThrowsAnExceptionWithInvalidGuid(): void public function testThatLoadUserByUsernameThrowsAnExceptionWithInvalidGuid(): void
{ {
$this->expectException(UsernameNotFoundException::class);
$this->expectExceptionMessage('API key is not valid');

$this->apiKeyUserProvider->loadUserByUsername((string)time()); $this->apiKeyUserProvider->loadUserByUsername((string)time());
} }


Expand All @@ -95,15 +96,14 @@ public function testThatLoadUserByUsernameWorksAsExpected(string $token, array $
unset($apiKeyUser); unset($apiKeyUser);
} }


/** @noinspection PhpFullyQualifiedNameUsageInspection */
/** /**
* @expectedException \Symfony\Component\Security\Core\Exception\UnsupportedUserException
* @expectedExceptionMessage API key cannot refresh user
*
* @throws Throwable * @throws Throwable
*/ */
public function testThatRefreshUserThrowsAnException(): void public function testThatRefreshUserThrowsAnException(): void
{ {
$this->expectException(UnsupportedUserException::class);
$this->expectExceptionMessage('API key cannot refresh user');

$user = new User('username', 'password'); $user = new User('username', 'password');


$this->apiKeyUserProvider->refreshUser($user); $this->apiKeyUserProvider->refreshUser($user);
Expand Down
21 changes: 10 additions & 11 deletions tests/Functional/Security/Provider/SecurityUserFactoryTest.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@


use App\Entity\User; use App\Entity\User;
use App\Repository\UserRepository; use App\Repository\UserRepository;
use App\Security\SecurityUser;
use App\Security\Provider\SecurityUserFactory; use App\Security\Provider\SecurityUserFactory;
use App\Security\SecurityUser;
use Generator; use Generator;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
use Symfony\Component\Security\Core\User\User as CoreUser; use Symfony\Component\Security\Core\User\User as CoreUser;
use Throwable; use Throwable;


Expand All @@ -35,14 +37,13 @@ class SecurityUserFactoryTest extends KernelTestCase
*/ */
private $userRepository; private $userRepository;


/** @noinspection PhpFullyQualifiedNameUsageInspection */
/** /**
* @expectedException \Symfony\Component\Security\Core\Exception\UsernameNotFoundException
*
* @throws Throwable * @throws Throwable
*/ */
public function testThatLoadUserByUsernameThrowsAnExceptionWithInvalidUsername(): void public function testThatLoadUserByUsernameThrowsAnExceptionWithInvalidUsername(): void
{ {
$this->expectException(UsernameNotFoundException::class);

static::assertNull($this->securityUserFactory->loadUserByUsername('foobar')); static::assertNull($this->securityUserFactory->loadUserByUsername('foobar'));
} }


Expand All @@ -62,14 +63,13 @@ public function testThatLoadUserByUsernameReturnsExpectedUserInstance(string $us
static::assertSame($roles, $domainUser->getRoles()); static::assertSame($roles, $domainUser->getRoles());
} }


/** @noinspection PhpFullyQualifiedNameUsageInspection */
/** /**
* @expectedException \Symfony\Component\Security\Core\Exception\UsernameNotFoundException
*
* @throws Throwable * @throws Throwable
*/ */
public function testThatRefreshUserThrowsAnExceptionIfUserIsNotFound(): void public function testThatRefreshUserThrowsAnExceptionIfUserIsNotFound(): void
{ {
$this->expectException(UsernameNotFoundException::class);

$this->securityUserFactory->refreshUser(new SecurityUser(new User())); $this->securityUserFactory->refreshUser(new SecurityUser(new User()));


unset($user); unset($user);
Expand Down Expand Up @@ -113,15 +113,14 @@ public function testThatRefreshUserReturnsANewInstanceOfSecurityUser(): void
unset($user); unset($user);
} }


/** @noinspection PhpFullyQualifiedNameUsageInspection */
/** /**
* @expectedException \Symfony\Component\Security\Core\Exception\UnsupportedUserException
* @expectedExceptionMessage Invalid user class "Symfony\Component\Security\Core\User\User"
*
* @throws Throwable * @throws Throwable
*/ */
public function testThatRefreshUserThrowsAnExceptionIfUserClassIsNotSupported(): void public function testThatRefreshUserThrowsAnExceptionIfUserClassIsNotSupported(): void
{ {
$this->expectException(UnsupportedUserException::class);
$this->expectExceptionMessage('Invalid user class "Symfony\Component\Security\Core\User\User"');

$user = new CoreUser('test', 'password'); $user = new CoreUser('test', 'password');


$this->securityUserFactory->refreshUser($user); $this->securityUserFactory->refreshUser($user);
Expand Down
Loading

0 comments on commit ba39de1

Please sign in to comment.