Skip to content

Commit

Permalink
phpstan should be happy now
Browse files Browse the repository at this point in the history
  • Loading branch information
davisenra committed Mar 24, 2024
1 parent 3c76ab5 commit b721fde
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 40 deletions.
2 changes: 2 additions & 0 deletions config/container.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use Psr\Log\LoggerInterface;
use Slim\App;
use Slim\Factory\AppFactory;
use Slim\Handlers\ErrorHandler;
use Symfony\Component\Console\Application;

return [
Expand Down Expand Up @@ -60,6 +61,7 @@
logger: $container->get(LoggerInterface::class)
);

/** @var ErrorHandler $errorHandler */
$errorHandler = $errorMiddleware->getDefaultErrorHandler();
$errorHandler->forceContentType('application/json');

Expand Down
5 changes: 3 additions & 2 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
parameters:
level: 5
level: 6
paths:
- src
- tests
- tests
- config
2 changes: 1 addition & 1 deletion src/Controller/HealthCheckController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ public function __invoke(): Response
return new Response(200, [], json_encode([
'status' => true,
'now' => (new \DateTime('now'))->format('Y-m-d H:i:s'),
]));
], flags: JSON_THROW_ON_ERROR));
}
}
6 changes: 5 additions & 1 deletion src/Repository/UserRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@

namespace App\Repository;

use App\Entity\User;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\Mapping\ClassMetadata;

/**
* @extends EntityRepository<User>
*/
class UserRepository extends EntityRepository
{
public function __construct(EntityManagerInterface $entityManager)
{
parent::__construct($entityManager, new ClassMetadata(self::class));
parent::__construct($entityManager, new ClassMetadata(User::class));
}
}
5 changes: 0 additions & 5 deletions tests/Traits/AppTestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,5 @@ protected function setUpApp(): void
$this->app = $container->get(App::class);

$this->setUpContainer($container);

/** @phpstan-ignore-next-line */
if (method_exists($this, 'setUpDatabase')) {
$this->setUpDatabase(__DIR__ . '/../../resources/schema/schema.sql');
}
}
}
8 changes: 4 additions & 4 deletions tests/Traits/ContainerTestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ trait ContainerTestTrait
*
* TestCases must call this method inside setUp().
*
* @param ContainerInterface|null $container The container
* @param ContainerInterface|null $container The container
*
* @throws UnexpectedValueException
*/
Expand All @@ -39,12 +39,12 @@ protected function setUpContainer(?ContainerInterface $container = null): void
/**
* Define an object or a value in the container.
*
* @param string $name The entry name
* @param mixed $value The value
* @param string $name The entry name
* @param mixed $value The value
*
* @throws BadMethodCallException
*/
protected function setContainerValue(string $name, $value): void
protected function setContainerValue(string $name, mixed $value): void
{
if (method_exists($this->container, 'set')) {
$this->container->set($name, $value);
Expand Down
28 changes: 14 additions & 14 deletions tests/Traits/HttpJsonTestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@ trait HttpJsonTestTrait
/**
* Create a JSON request.
*
* @param string $method The HTTP method
* @param string|UriInterface $uri The URI
* @param array|null $data The json data
* @param string $method The HTTP method
* @param string|UriInterface $uri The URI
* @param array<mixed>|null $data The json data
*/
protected function createJsonRequest(string $method, $uri, ?array $data = null): ServerRequestInterface
{
$request = $this->createRequest($method, $uri);

if ($data !== null) {
$request->getBody()->write((string) json_encode($data));
$request->getBody()->write((string)json_encode($data));
}

return $request->withHeader('Content-Type', 'application/json');
Expand All @@ -34,8 +34,8 @@ protected function createJsonRequest(string $method, $uri, ?array $data = null):
/**
* Verify that the specified array is an exact match for the returned JSON.
*
* @param ResponseInterface $response The response
* @param array $expected The expected array
* @param ResponseInterface $response The response
* @param array<string, mixed> $expected The expected array
*/
protected function assertJsonData(ResponseInterface $response, array $expected): void
{
Expand All @@ -47,21 +47,21 @@ protected function assertJsonData(ResponseInterface $response, array $expected):
/**
* Get JSON response as array.
*
* @param ResponseInterface $response The response
* @return array The data
* @param ResponseInterface $response The response
* @return array<mixed> The data
*/
protected function getJsonData(ResponseInterface $response): array
{
$actual = (string) $response->getBody();
$actual = (string)$response->getBody();
$this->assertJson($actual);

return (array) json_decode($actual, true);
return (array)json_decode($actual, true);
}

/**
* Verify JSON response.
*
* @param ResponseInterface $response The response
* @param ResponseInterface $response The response
*/
protected function assertJsonContentType(ResponseInterface $response): void
{
Expand All @@ -71,9 +71,9 @@ protected function assertJsonContentType(ResponseInterface $response): void
/**
* Verify that the specified array is an exact match for the returned JSON.
*
* @param mixed $expected The expected value
* @param string $path The array path
* @param ResponseInterface $response The response
* @param mixed $expected The expected value
* @param string $path The array path
* @param ResponseInterface $response The response
* @return void
*/
protected function assertJsonValue($expected, string $path, ResponseInterface $response)
Expand Down
26 changes: 13 additions & 13 deletions tests/Traits/HttpTestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ trait HttpTestTrait
/**
* Create a server request.
*
* @param string $method The HTTP method
* @param string|UriInterface $uri The URI
* @param array $serverParams The server parameters
* @param string $method The HTTP method
* @param string|UriInterface $uri The URI
* @param array<mixed> $serverParams The server parameters
* @return ServerRequestInterface The request
*
* @throws RuntimeException
*/
protected function createRequest(string $method, $uri, array $serverParams = []): ServerRequestInterface
{
if (! $this->container instanceof ContainerInterface) {
if (!$this->container instanceof ContainerInterface) {
throw new RuntimeException('DI container not found');
}

Expand All @@ -39,9 +39,9 @@ protected function createRequest(string $method, $uri, array $serverParams = [])
/**
* Create a form request.
*
* @param string $method The HTTP method
* @param string|UriInterface $uri The URI
* @param array|null $data The form data
* @param string $method The HTTP method
* @param string|UriInterface $uri The URI
* @param array<mixed>|null $data The form data
* @return ServerRequestInterface The request
*/
protected function createFormRequest(string $method, $uri, ?array $data = null): ServerRequestInterface
Expand All @@ -58,15 +58,15 @@ protected function createFormRequest(string $method, $uri, ?array $data = null):
/**
* Create a new response.
*
* @param int $code HTTP status code; defaults to 200
* @param string $reasonPhrase Reason phrase to associate with status code
* @param int $code HTTP status code; defaults to 200
* @param string $reasonPhrase Reason phrase to associate with status code
* @return ResponseInterface The response
*
* @throws RuntimeException
*/
protected function createResponse(int $code = 200, string $reasonPhrase = ''): ResponseInterface
{
if (! $this->container instanceof ContainerInterface) {
if (!$this->container instanceof ContainerInterface) {
throw new RuntimeException('DI container not found');
}

Expand All @@ -78,12 +78,12 @@ protected function createResponse(int $code = 200, string $reasonPhrase = ''): R
/**
* Assert that the response body contains a string.
*
* @param ResponseInterface $response The response
* @param string $needle The expected string
* @param ResponseInterface $response The response
* @param string $needle The expected string
*/
protected function assertResponseContains(ResponseInterface $response, string $needle): void
{
$body = (string) $response->getBody();
$body = (string)$response->getBody();

$this->assertStringContainsString($needle, $body);
}
Expand Down

0 comments on commit b721fde

Please sign in to comment.