Skip to content

Commit

Permalink
QA: phpstan, tests, github actions
Browse files Browse the repository at this point in the history
  • Loading branch information
f3l1x committed Feb 10, 2022
1 parent 57e5406 commit e25e03a
Show file tree
Hide file tree
Showing 129 changed files with 100 additions and 59 deletions.
4 changes: 1 addition & 3 deletions .github/workflows/codesniffer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ on:
- cron: "0 8 * * 1"

jobs:
build:
codesniffer:
name: "Codesniffer"
uses: contributte/.github/.github/workflows/codesniffer.yml@v1
with:
php: "8.0"
17 changes: 17 additions & 0 deletions .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: "Coverage"

on:
pull_request:

push:
branches: ["*"]

schedule:
- cron: "0 8 * * 1"

jobs:
test80:
name: "Nette Tester"
uses: contributte/.github/.github/workflows/nette-tester-coverage.yml@v1
with:
php: "8.0"
4 changes: 1 addition & 3 deletions .github/workflows/phpstan.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ on:
- cron: "0 8 * * 1"

jobs:
build:
phpstan:
name: "Phpstan"
uses: contributte/.github/.github/workflows/phpstan.yml@v1
with:
php: "8.0"
15 changes: 11 additions & 4 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,27 @@ on:
- cron: "0 8 * * 1"

jobs:
build81:
test81:
name: "Nette Tester"
uses: contributte/.github/.github/workflows/nette-tester.yml@v1
with:
php: "8.0"
php: "8.1"

build80:
test80:
name: "Nette Tester"
uses: contributte/.github/.github/workflows/nette-tester.yml@v1
with:
php: "8.0"

build74:
test74:
name: "Nette Tester"
uses: contributte/.github/.github/workflows/nette-tester.yml@v1
with:
php: "7.4"

test74-lower:
name: "Nette Tester"
uses: contributte/.github/.github/workflows/nette-tester.yml@v1
with:
php: "7.4"
composer: "composer update --no-interaction --no-progress --prefer-dist --prefer-stable --prefer-lowest"
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ csf:
vendor/bin/codefixer src tests

phpstan:
vendor/bin/phpstan analyse -l max -c phpstan.neon src
vendor/bin/phpstan analyse -c phpstan.neon

tests:
vendor/bin/tester -s -p php --colors 1 -C tests/cases
vendor/bin/tester -s -p php --colors 1 -C tests/Cases

coverage:
ifdef GITHUB_ACTION
vendor/bin/tester -s -p phpdbg --colors 1 -C --coverage coverage.xml --coverage-src src tests/cases
vendor/bin/tester -s -p phpdbg --colors 1 -C --coverage coverage.xml --coverage-src src tests/Cases
else
vendor/bin/tester -s -p phpdbg --colors 1 -C --coverage coverage.html --coverage-src src tests/cases
vendor/bin/tester -s -p phpdbg --colors 1 -C --coverage coverage.html --coverage-src src tests/Cases
endif
19 changes: 9 additions & 10 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,34 +19,34 @@
}
],
"require": {
"php": ">=7.4",
"php": ">=7.3",
"ext-json": "*",
"contributte/psr7-http-message": "~0.7.0",
"contributte/middlewares": "^0.10.0",
"doctrine/annotations": "^1.13",
"koriym/attributes": "^1.0.2",
"nette/utils": "^3.2"
"nette/utils": "^3.2.7"
},
"require-dev": {
"mockery/mockery": "^1.1.0",
"mockery/mockery": "^1.5.0",
"nette/application": "^3.0.0",
"nette/di": "^3.0.0",
"nette/http": "^3.0.1",
"nettrine/annotations": "^0.7.0",
"ninjify/nunjuck": "^0.4",
"ninjify/qa": "^0.13",
"phpstan/phpstan": "^0.12",
"phpstan/phpstan-deprecation-rules": "^0.12",
"phpstan/phpstan-nette": "^0.12",
"phpstan/phpstan-strict-rules": "^0.12",
"phpstan/phpstan": "^1.0.0",
"phpstan/phpstan-deprecation-rules": "^1.0.0",
"phpstan/phpstan-nette": "^1.0.0",
"phpstan/phpstan-strict-rules": "^1.0.0",
"psr/log": "^1.1|^2.0|^3.0",
"symfony/console": "^5.4.3 || ^6.0.0",
"symfony/validator": "^5.4.3 || ^6.0",
"symfony/yaml": "^5.4.3 || ^6.0.0",
"tracy/tracy": "^2.6.0"
},
"conflict": {
"nette/di": "<=3.0.0-RC"
"nette/tester": "<2.4.1"
},
"provide": {
"psr/http-message-implementation": "1.0"
Expand All @@ -58,8 +58,7 @@
},
"autoload-dev": {
"psr-4": {
"Tests\\Fixtures\\": "tests/fixtures",
"Tests\\Toolkit\\": "tests/toolkit"
"Tests\\": "tests"
}
},
"minimum-stability": "dev",
Expand Down
13 changes: 11 additions & 2 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,15 @@ includes:
- vendor/phpstan/phpstan-strict-rules/rules.neon

parameters:
# @todo
level: 8
phpVersion: 70400

paths:
- src

reportMaybesInPropertyPhpDocTypes: false
checkGenericClassInNonGenericObjectType: false

ignoreErrors:
# deprecated with no replacement until 2.0.0
- '#^Call to deprecated method registerUniqueLoader\(\) of class Doctrine\\Common\\Annotations\\AnnotationRegistry.+#'
Expand All @@ -16,7 +23,6 @@ parameters:
- '#^Variable property access on Apitte\\Core\\Mapping\\Request\\BasicEntity\.$#'

# Phpstan bug
- '#^Parameter \#1 (.*?) of function call_user_func expects callable\(\)\: mixed, array\(Apitte\\Core\\UI\\Controller\\IController, string\) given\.$#'
- message: '#^Parameter \#1 \$argument of class ReflectionClass constructor expects class-string<T of object>|T of object, string given\.$#'
path: %currentWorkingDirectory%/src/Core/DI/Loader/DoctrineAnnotationLoader.php

Expand All @@ -43,3 +49,6 @@ parameters:
path: %currentWorkingDirectory%/src/OpenApi/SchemaDefinition/Entity/EntityAdapter.php

- '#Unable to resolve the template type T in call to method Nette\\DI\\Container::getByType\(\)#'

- '#Property Apitte\\OpenApi\\Schema\\(\w+)::\$(\w+) is never read, only written.#'
- '#Property Apitte\\OpenApi\\Schema\\(\w+)::\$(\w+) is unused.#'
24 changes: 8 additions & 16 deletions ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,39 +12,31 @@
<rule ref="SlevomatCodingStandard.Files.TypeNameMatchesFileName">
<properties>
<property name="rootNamespaces" type="array">
<element key=" src" value="Apitte"/>
<element key="tests/cases" value="Tests\Cases"/>
<element key="tests/fixtures" value="Tests\Fixtures"/>
<element key="tests/toolkit" value="Tests\Toolkit"/>
<element key="src" value="Apitte"/>
<element key="tests" value="Tests"/>
</property>
</properties>
</rule>

<rule ref="SlevomatCodingStandard.Exceptions.ReferenceThrowableOnly.ReferencedGeneralException">
<exclude-pattern>src/Exception/ExceptionExtra.php</exclude-pattern>
</rule>

<rule ref="SlevomatCodingStandard.Namespaces.UnusedUses.UnusedUse">
<!-- false positive; slevomat ignores attribute class names and removes their imports -->
<exclude-pattern>tests/fixtures/Controllers/AttributeFoobarController.php</exclude-pattern>
<exclude-pattern>tests/fixtures/Controllers/AttributeMultiController.php</exclude-pattern>
<exclude-pattern>tests/Fixtures/Controllers/AttributeFoobarController.php</exclude-pattern>
<exclude-pattern>tests/Fixtures/Controllers/AttributeMultiController.php</exclude-pattern>
</rule>

<rule ref="PSR1.Classes.ClassDeclaration.MissingNamespace">
<exclude-pattern>*tests/cases/*</exclude-pattern>
<exclude-pattern>*tests/Cases/*</exclude-pattern>
</rule>

<rule ref="Squiz.Classes.ClassFileName.NoMatch">
<exclude-pattern>*tests/cases/*</exclude-pattern>
<exclude-pattern>*tests/Cases/*</exclude-pattern>
</rule>

<rule ref="SlevomatCodingStandard.Files.TypeNameMatchesFileName.NoMatchBetweenTypeNameAndFileName">
<exclude-pattern>*tests/cases/*</exclude-pattern>
<exclude-pattern>*tests/Cases/*</exclude-pattern>
</rule>

<!-- Exclude folders -->
<exclude-pattern>/tests/tmp</exclude-pattern>

<!-- fails on PHP 7.4 because of attributes syntax collision (remove when on PHP 8) -->
<exclude-pattern>/tests/fixtures/Controllers/*.php</exclude-pattern>
</ruleset>

2 changes: 2 additions & 0 deletions src/Core/DI/Plugin/CoreSchemaPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Apitte\Core\Schema\Validation\FullpathValidation;
use Apitte\Core\Schema\Validation\GroupPathValidation;
use Apitte\Core\Schema\Validation\IdValidation;
use Apitte\Core\Schema\Validation\IValidation;
use Apitte\Core\Schema\Validation\NegotiationValidation;
use Apitte\Core\Schema\Validation\PathValidation;
use Apitte\Core\Schema\Validation\RequestBodyValidation;
Expand Down Expand Up @@ -146,6 +147,7 @@ protected function validateSchema(SchemaBuilder $builder): SchemaBuilder
$validator = new SchemaBuilderValidator();

// Add all validators at compile-time
/** @var class-string<IValidation> $validation */
foreach ($validations as $validation) {
$validator->add(new $validation());
}
Expand Down
3 changes: 2 additions & 1 deletion src/Core/Handler/ServiceCallback.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Apitte\Core\Handler;

use Apitte\Core\UI\Controller\IController;
use Apitte\Core\Utils\Helpers;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;

Expand Down Expand Up @@ -36,7 +37,7 @@ public function getMethod(): string
*/
public function __invoke(ServerRequestInterface $request, ResponseInterface $response)
{
return call_user_func([$this->service, $this->method], $request, $response);
return call_user_func(Helpers::callback([$this->service, $this->method]), $request, $response);
}

}
12 changes: 5 additions & 7 deletions src/Core/Mapping/RequestParameterMapping.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ class RequestParameterMapping
InvalidArgumentTypeException::TYPE_DATETIME => '%s request parameter "%s" should be of type datetime in format ISO 8601 (Y-m-d\TH:i:sP).',
];

/** @var string[]|ITypeMapper[] */
/** @var array<string, ITypeMapper|class-string<ITypeMapper>> */
protected $types = [];

/**
* @param string|ITypeMapper $mapper
* @param ITypeMapper|string $mapper
*/
public function addMapper(string $type, $mapper): void
{
Expand Down Expand Up @@ -223,14 +223,12 @@ protected function getMapper(string $type): ?ITypeMapper
return null;
}

$mapper = $this->types[$type];

// Initialize mapper
if (!is_object($mapper)) {
$this->types[$type] = $mapper = new $mapper();
if (!($this->types[$type] instanceof ITypeMapper)) {
$this->types[$type] = new $this->types[$type]();
}

return $mapper;
return $this->types[$type];
}

}
14 changes: 14 additions & 0 deletions src/Core/Utils/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Apitte\Core\Utils;

use Apitte\Core\Exception\Logical\InvalidArgumentException;

final class Helpers
{

Expand All @@ -10,4 +12,16 @@ public static function slashless(string $str): string
return Regex::replace($str, '#/{2,}#', '/');
}

/**
* @param array{object,string} $callback
*/
public static function callback(array $callback): callable
{
if (!is_callable($callback)) {
throw new InvalidArgumentException('Invalid callback given');
}

return $callback;
}

}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

require_once __DIR__ . '/../../../bootstrap.php';

use Apitte\Core\Exception\Logical\InvalidArgumentException;
use Apitte\Core\Utils\Helpers;
use Tester\Assert;

Expand All @@ -19,3 +20,12 @@ test(function (): void {
Assert::equal('/foo/', Helpers::slashless('/foo/'));
Assert::equal('/foo/', Helpers::slashless('//foo//'));
});

// Helpers::callback
test(function (): void {
Assert::type('callable', Helpers::callback([Helpers::class, 'callback']));

Assert::exception(static function (): void {
Assert::type('callable', Helpers::callback([Helpers::class, 'fake']));
}, InvalidArgumentException::class);
});
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
<?php

declare(strict_types=1);
<?php declare(strict_types = 1);

namespace Tests\Fixtures\Controllers\Mixed;

Expand All @@ -11,8 +9,8 @@
final class AttributesOnlyController extends ApiV1Controller
{

#[RequestParameter(name: "userId", type: "int", in: "path")]
#[RequestParameter(name: "photoId", type: "int", in: "path")]
#[RequestParameter(name: 'userId', type: 'int', in: 'path')]
#[RequestParameter(name: 'photoId', type: 'int', in: 'path')]
#[Path(path: '/user/{userId}/verification-photo/{photoId}')]
public function run()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
<?php

declare(strict_types=1);
<?php declare(strict_types = 1);

namespace Tests\Fixtures\Controllers\Mixed;

use Apitte\Core\Annotation\Controller\Path;
use Apitte\Core\Annotation\Controller\RequestParameters;
use Apitte\Core\Annotation\Controller\RequestParameter;
use Apitte\Core\Annotation\Controller\RequestParameters;
use Tests\Fixtures\Controllers\ApiV1Controller;

final class PathAndRequestParamsController extends ApiV1Controller
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit e25e03a

Please sign in to comment.