From 2cce967eb09edd6ef35238c68f8ff5ed2348f5fd Mon Sep 17 00:00:00 2001 From: Igor Scheller Date: Wed, 6 Nov 2019 13:16:00 +0100 Subject: Tests: Replaced duplicates and formatting/cleanup --- tests/Unit/Config/ConfigServiceProviderTest.php | 57 +++++++++++----------- .../Controllers/PasswordResetControllerTest.php | 4 +- .../Unit/Helpers/Stub/UserModelImplementation.php | 5 +- .../Stub/ReturnResponseMiddlewareHandler.php | 3 +- tests/Unit/Models/EventConfigTest.php | 2 +- tests/Unit/Models/User/UserTest.php | 2 + .../Renderer/Twig/Extensions/ExtensionTest.php | 6 ++- tests/Unit/Renderer/TwigServiceProviderTest.php | 3 +- 8 files changed, 46 insertions(+), 36 deletions(-) diff --git a/tests/Unit/Config/ConfigServiceProviderTest.php b/tests/Unit/Config/ConfigServiceProviderTest.php index 62a662f8..8895f66d 100644 --- a/tests/Unit/Config/ConfigServiceProviderTest.php +++ b/tests/Unit/Config/ConfigServiceProviderTest.php @@ -23,21 +23,9 @@ class ConfigServiceProviderTest extends ServiceProviderTest */ public function testRegister() { + /** @var Application|MockObject $app */ /** @var Config|MockObject $config */ - $config = $this->getMockBuilder(Config::class) - ->getMock(); - - $app = $this->getApp(['make', 'instance', 'get']); - Application::setInstance($app); - - $this->setExpects($app, 'make', [Config::class], $config); - $this->setExpects($app, 'get', ['path.config'], __DIR__ . '/../../../config', $this->atLeastOnce()); - $app->expects($this->exactly(2)) - ->method('instance') - ->withConsecutive( - [Config::class, $config], - ['config', $config] - ); + list($app, $config) = $this->getConfiguredApp(__DIR__ . '/../../../config'); $this->setExpects($config, 'set', null, null, $this->exactly(2)); $config->expects($this->exactly(3)) @@ -64,21 +52,9 @@ class ConfigServiceProviderTest extends ServiceProviderTest */ public function testRegisterException() { + /** @var Application|MockObject $app */ /** @var Config|MockObject $config */ - $config = $this->getMockBuilder(Config::class) - ->getMock(); - - $app = $this->getApp(['make', 'instance', 'get']); - Application::setInstance($app); - - $this->setExpects($app, 'make', [Config::class], $config); - $app->expects($this->exactly(2)) - ->method('instance') - ->withConsecutive( - [Config::class, $config], - ['config', $config] - ); - $this->setExpects($app, 'get', ['path.config'], __DIR__ . '/not_existing', $this->atLeastOnce()); + list($app, $config) = $this->getConfiguredApp(__DIR__ . '/not_existing'); $this->setExpects($config, 'set', null, null, $this->never()); $this->setExpects($config, 'get', [null], []); @@ -151,4 +127,29 @@ class ConfigServiceProviderTest extends ServiceProviderTest $config->get(null) ); } + + /** + * @param string $configPath + * @return Application[]|Config[] + */ + protected function getConfiguredApp(string $configPath) + { + /** @var Config|MockObject $config */ + $config = $this->getMockBuilder(Config::class) + ->getMock(); + + $app = $this->getApp(['make', 'instance', 'get']); + Application::setInstance($app); + + $this->setExpects($app, 'make', [Config::class], $config); + $this->setExpects($app, 'get', ['path.config'], $configPath, $this->atLeastOnce()); + $app->expects($this->exactly(2)) + ->method('instance') + ->withConsecutive( + [Config::class, $config], + ['config', $config] + ); + + return [$app, $config]; + } } diff --git a/tests/Unit/Controllers/PasswordResetControllerTest.php b/tests/Unit/Controllers/PasswordResetControllerTest.php index 54046cef..09538416 100644 --- a/tests/Unit/Controllers/PasswordResetControllerTest.php +++ b/tests/Unit/Controllers/PasswordResetControllerTest.php @@ -61,7 +61,7 @@ class PasswordResetControllerTest extends TestCase $controller->postReset($request); - $this->assertNotEmpty(PasswordReset::find($user->id)->first()); + $this->assertNotEmpty((new PasswordReset())->find($user->id)->first()); $this->assertTrue($log->hasInfoThatContains($user->name)); } @@ -152,7 +152,7 @@ class PasswordResetControllerTest extends TestCase $response = $controller->postResetPassword($request); $this->assertEquals(200, $response->getStatusCode()); - $this->assertEmpty(PasswordReset::find($user->id)); + $this->assertEmpty((new PasswordReset)->find($user->id)); $this->assertNotNull(auth()->authenticate($user->name, $password)); } diff --git a/tests/Unit/Helpers/Stub/UserModelImplementation.php b/tests/Unit/Helpers/Stub/UserModelImplementation.php index 1b14a17e..69b7732b 100644 --- a/tests/Unit/Helpers/Stub/UserModelImplementation.php +++ b/tests/Unit/Helpers/Stub/UserModelImplementation.php @@ -4,6 +4,7 @@ namespace Engelsystem\Test\Unit\Helpers\Stub; use Engelsystem\Models\User\User; use Illuminate\Database\Eloquent\Collection; +use Illuminate\Database\Query\Builder as QueryBuilder; use InvalidArgumentException; class UserModelImplementation extends User @@ -22,7 +23,7 @@ class UserModelImplementation extends User * @param array $columns * @return User|null */ - public static function find($id, $columns = ['*']) + public function find($id, $columns = ['*']) { if ($id != static::$id) { throw new InvalidArgumentException('Wrong user ID searched'); @@ -33,7 +34,7 @@ class UserModelImplementation extends User /** * @param string $apiKey - * @return User[]|Collection|\Illuminate\Database\Query\Builder + * @return User[]|Collection|QueryBuilder */ public static function whereApiKey($apiKey) { diff --git a/tests/Unit/Middleware/Stub/ReturnResponseMiddlewareHandler.php b/tests/Unit/Middleware/Stub/ReturnResponseMiddlewareHandler.php index 370187dd..18d7a28b 100644 --- a/tests/Unit/Middleware/Stub/ReturnResponseMiddlewareHandler.php +++ b/tests/Unit/Middleware/Stub/ReturnResponseMiddlewareHandler.php @@ -2,6 +2,7 @@ namespace Engelsystem\Test\Unit\Middleware\Stub; +use Exception; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Server\RequestHandlerInterface; @@ -21,7 +22,7 @@ class ReturnResponseMiddlewareHandler implements RequestHandlerInterface * * @param ServerRequestInterface $request * @return ResponseInterface - * @throws \Exception + * @throws Exception */ public function handle(ServerRequestInterface $request): ResponseInterface { diff --git a/tests/Unit/Models/EventConfigTest.php b/tests/Unit/Models/EventConfigTest.php index 18d27007..4ab2fc75 100644 --- a/tests/Unit/Models/EventConfigTest.php +++ b/tests/Unit/Models/EventConfigTest.php @@ -65,7 +65,7 @@ class EventConfigTest extends TestCase ->save(); $this->assertEquals( '2001-02-03 00:00', - EventConfig::find('buildup_start') + (new EventConfig())->find('buildup_start') ->value ->format('Y-m-d H:i') ); diff --git a/tests/Unit/Models/User/UserTest.php b/tests/Unit/Models/User/UserTest.php index 3e793832..b89f832b 100644 --- a/tests/Unit/Models/User/UserTest.php +++ b/tests/Unit/Models/User/UserTest.php @@ -11,6 +11,7 @@ use Engelsystem\Models\User\State; use Engelsystem\Models\User\User; use Engelsystem\Test\Unit\HasDatabase; use Engelsystem\Test\Unit\TestCase; +use Exception; class UserTest extends TestCase { @@ -75,6 +76,7 @@ class UserTest extends TestCase * @param string $class * @param string $name * @param array $data + * @throws Exception */ public function testHasOneRelations($class, $name, $data) { diff --git a/tests/Unit/Renderer/Twig/Extensions/ExtensionTest.php b/tests/Unit/Renderer/Twig/Extensions/ExtensionTest.php index 2f64360e..921a06a3 100644 --- a/tests/Unit/Renderer/Twig/Extensions/ExtensionTest.php +++ b/tests/Unit/Renderer/Twig/Extensions/ExtensionTest.php @@ -3,6 +3,7 @@ namespace Engelsystem\Test\Unit\Renderer\Twig\Extensions; use DMS\PHPUnitExtensions\ArraySubset\ArraySubsetAsserts; +use Exception; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; use Twig_Function as TwigFunction; @@ -39,7 +40,8 @@ abstract class ExtensionTest extends TestCase * @param string $name * @param callable $callback * @param TwigFunction[] $functions - * @param array $options + * @param array $options + * @throws Exception */ protected function assertExtensionExists($name, $callback, $functions, $options = []) { @@ -69,6 +71,7 @@ abstract class ExtensionTest extends TestCase * @param string $name * @param mixed $value * @param mixed[] $globals + * @throws Exception */ protected function assertGlobalsExists($name, $value, $globals) { @@ -86,6 +89,7 @@ abstract class ExtensionTest extends TestCase * * @param $tokenParser * @param $tokenParsers + * @throws Exception */ protected function assertTokenParserExists($tokenParser, $tokenParsers) { diff --git a/tests/Unit/Renderer/TwigServiceProviderTest.php b/tests/Unit/Renderer/TwigServiceProviderTest.php index 3f027684..cd071349 100644 --- a/tests/Unit/Renderer/TwigServiceProviderTest.php +++ b/tests/Unit/Renderer/TwigServiceProviderTest.php @@ -9,6 +9,7 @@ use Engelsystem\Renderer\TwigServiceProvider; use Engelsystem\Test\Unit\ServiceProviderTest; use PHPUnit\Framework\MockObject\MockObject; use ReflectionClass as Reflection; +use ReflectionException; use stdClass; use Twig_Environment as Twig; use Twig_Extension_Core as TwigCore; @@ -163,7 +164,7 @@ class TwigServiceProviderTest extends ServiceProviderTest /** * @param TwigServiceProvider $serviceProvider * @param array $extensions - * @throws \ReflectionException + * @throws ReflectionException */ protected function setExtensionsTo($serviceProvider, $extensions) { -- cgit v1.2.3-54-g00ecf