Skip to content

Commit

Permalink
Upgrade CI matrix build and add QA tests (#292)
Browse files Browse the repository at this point in the history
* ci: upgrade workflow

* build: upgrade phpunit

* build: static data providers

* test: introduce new qa for #288

* ci: upgrade ci

* ci: phpstan level 6

* test: pass tests after master merge
  • Loading branch information
g105b authored May 2, 2023
1 parent 7dbc7a0 commit 7e82009
Show file tree
Hide file tree
Showing 19 changed files with 123 additions and 173 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,6 @@ jobs:
with:
php_version: ${{ matrix.php }}
php_extensions: xdebug
configuration: test/phpunit/phpunit.xml
bootstrap: vendor/autoload.php
coverage_text: _coverage/coverage.txt
coverage_clover: _coverage/clover.xml

Expand Down Expand Up @@ -108,6 +106,7 @@ jobs:
with:
php_version: ${{ matrix.php }}
path: src/
level: 6

phpmd:
runs-on: ubuntu-latest
Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@

"require": {
"php": ">=8.1",
"phpgt/http": "^1.1"
"phpgt/http": "^v1.1"
},

"require-dev": {
"phpstan/phpstan": "^1.10",
"phpunit/phpunit": "^10.0",
"phpstan/phpstan": "^v1.10",
"phpunit/phpunit": "^10.1",
"phpmd/phpmd": "^2.13",
"squizlabs/php_codesniffer": "^3.7"
},
Expand Down
2 changes: 1 addition & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 1 addition & 6 deletions phpcs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,7 @@
<rule ref="Generic.Files.EndFileNewline" />
<rule ref="Generic.Files.InlineHTML" />
<rule ref="Generic.Files.LineEndings" />
<rule ref="Generic.Files.LineLength">
<properties>
<property name="lineLimit" value="120"/>
<property name="absoluteLineLimit" value="0"/>
</properties>
</rule>
<rule ref="Generic.Files.LineLength" />
<rule ref="Generic.Files.OneClassPerFile" />
<rule ref="Generic.Files.OneInterfacePerFile" />
<rule ref="Generic.Files.OneObjectStructurePerFile" />
Expand Down
29 changes: 6 additions & 23 deletions phpmd.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,19 @@
<description>Custom ruleset</description>

<rule ref="rulesets/codesize.xml">
<exclude name="TooManyPublicMethods" />
</rule>

<rule ref="rulesets/cleancode.xml">
<exclude name="StaticAccess" />
<exclude name="BooleanArgumentFlag" />
<exclude name="ElseExpression" />
<exclude name="IfStatementAssignment" />
</rule>

<rule ref="rulesets/controversial.xml">
</rule>

<rule ref="rulesets/design.xml">
<exclude name="NumberOfChildren" />
</rule>

<rule ref="rulesets/naming.xml">
Expand All @@ -30,32 +31,14 @@
<rule ref="rulesets/unusedcode.xml">
</rule>

<rule ref="rulesets/codesize.xml/TooManyPublicMethods">
<priority>1</priority>
<properties>
<property name="maxmethods" value="20" />
</properties>
</rule>

<rule ref="rulesets/cleancode.xml/StaticAccess">
<properties>
<property name="exceptions" value="DateTime,DateTimeInterface,Gt\Curl\CurlObjectLookup" />
</properties>
</rule>

<rule ref="rulesets/naming.xml/ShortMethodName">
<properties>
<property name="exceptions" value="go,do" />
</properties>
</rule>
<rule ref="rulesets/naming.xml/ShortVariable">
<properties>
<property name="exceptions" value="ch,mh,sh" />
<property name="exceptions" value="a,b,i" />
</properties>
</rule>
<rule ref="rulesets/naming.xml/LongVariable">
<rule ref="rulesets/naming.xml/ShortMethodName">
<properties>
<property name="maximum" value="24" />
<property name="exceptions" value="or,do" />
</properties>
</rule>
</ruleset>
27 changes: 16 additions & 11 deletions test/phpunit/phpunit.xml → phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
<?xml version="1.0"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.0/phpunit.xsd"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.1/phpunit.xsd"
colors="true"
cacheResult="false"
cacheDirectory="test/phpunit/.phpunit.cache"
bootstrap="vendor/autoload.php"
>
<coverage>
<include>
<directory suffix=".php">
../../src
</directory>
</include>
</coverage>
<coverage />

<testsuites>
<testsuite name="main">
<directory>
./
./test/phpunit/
</directory>
</testsuite>
</testsuites>
<logging/>

<logging />

<source>
<include>
<directory>
src/
</directory>
</include>
</source>
</phpunit>
85 changes: 51 additions & 34 deletions src/Input.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

use ArrayAccess;
use Countable;
use Gt\Input\InputData\Datum\MultipleInputDatum;
use Iterator;
use Psr\Http\Message\StreamInterface;
use Gt\Input\Trigger\Trigger;
Expand Down Expand Up @@ -37,8 +36,8 @@ class Input implements ArrayAccess, Countable, Iterator {
protected BodyInputData $bodyParameters;

/**
* @param array<string, string|array<string>> $get
* @param array<string, string|array<string>> $post
* @param array<string, string> $get
* @param array<string, string> $post
* @param array<string, array<int|string, string|array<int|string>>> $files
* @param string $bodyPath
*/
Expand Down Expand Up @@ -106,16 +105,16 @@ public function add(string $key, InputDatum $datum, string $method):void {
}

/**
* Get a particular input value by its key. To specify either GET or POST variables, pass
* Input::METHOD_GET or Input::METHOD_POST as the second parameter (defaults to
* Input::METHOD_BOTH).
* Get a particular input value by its key. To specify either GET or
* POST variables, pass Input::METHOD_GET or Input::METHOD_POST as the
* second parameter (defaults to Input::METHOD_BOTH).
*/
public function get(string $key, string $method = null):null|InputDatum|MultipleInputDatum|string {
public function get(string $key, string $method = null):mixed {
if(is_null($method)) {
$method = self::DATA_COMBINED;
}

$data = match ($method) {
$data = match($method) {
self::DATA_QUERYSTRING => $this->queryStringParameters->get($key),
self::DATA_BODY => $this->bodyParameters->get($key),
self::DATA_FILES => $this->fileUploadParameters->getFile($key),
Expand All @@ -134,13 +133,28 @@ public function contains(string $key, string $method = null):bool {
$method = self::DATA_COMBINED;
}

return match ($method) {
self::DATA_QUERYSTRING => $this->containsQueryStringParameter($key),
self::DATA_BODY => $this->containsBodyParameter($key),
self::DATA_FILES => $this->containsFile($key),
self::DATA_COMBINED => isset($this->parameters[$key]),
default => throw new InvalidInputMethodException($method),
};
switch($method) {
case self::DATA_QUERYSTRING:
$isset = $this->containsQueryStringParameter($key);
break;

case self::DATA_BODY:
$isset =$this->containsBodyParameter($key);
break;

case self::DATA_FILES:
$isset =$this->containsFile($key);
break;

case self::DATA_COMBINED:
$isset = isset($this->parameters[$key]);
break;

default:
throw new InvalidInputMethodException($method);
}

return $isset;
}

public function containsQueryStringParameter(string $key):bool {
Expand All @@ -156,21 +170,27 @@ public function containsFile(string $key):bool {
}

/**
* Get an InputData object containing all request variables. To specify only GET or POST
* variables, pass Input::METHOD_GET or Input::METHOD_POST.
* Get an InputData object containing all request variables. To specify
* only GET or POST variables, pass Input::METHOD_GET
* or Input::METHOD_POST.
*/
public function getAll(string $method = null):InputData {
if(is_null($method)) {
$method = self::DATA_COMBINED;
}

return match ($method) {
self::DATA_QUERYSTRING => $this->queryStringParameters,
self::DATA_BODY => $this->bodyParameters,
self::DATA_FILES => $this->fileUploadParameters,
self::DATA_COMBINED => $this->parameters,
default => throw new InvalidInputMethodException($method),
};
switch($method) {
case self::DATA_QUERYSTRING:
return $this->queryStringParameters;
case self::DATA_BODY:
return $this->bodyParameters;
case self::DATA_FILES:
return $this->fileUploadParameters;
case self::DATA_COMBINED:
return $this->parameters;
default:
throw new InvalidInputMethodException($method);
}
}

/**
Expand All @@ -182,11 +202,12 @@ public function do(string $match):Trigger {
}

/**
* Return a Trigger, firing when one or more request variables are present with
* the provided key value pair(s) are present.
* Return a Trigger, firing when one or more request variables are
* present with the provided key value pair(s) are present.
*
* $matches is an associative array, where the key is a request variable's name and the
* value is the request variable's value to match.
* $matches is an associative array, where the key is a request
* variable's name and the value is the request variable's value
* to match.
*
* @param array<string, string>|string $matches
*/
Expand All @@ -210,7 +231,8 @@ public function with(string...$keys):Trigger {
}

/**
* Return a Trigger that will pass all keys apart from the provided keys to its callback.
* Return a Trigger that will pass all keys apart from the provided
* keys to its callback.
*/
public function without(string...$keys):Trigger {
return $this->newTrigger("without", ...$keys);
Expand All @@ -227,9 +249,4 @@ protected function newTrigger(string $functionName, string...$args):Trigger {
$trigger = new Trigger($this);
return $trigger->$functionName(...$args);
}

/** @return array<string, string> */
public function asArray():array {
return $this->getAll()->asArray();
}
}
2 changes: 1 addition & 1 deletion src/InputData/AbstractInputData.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ abstract class AbstractInputData implements ArrayAccess, Countable, Iterator {
protected QueryStringInputData $queryStringParameters;
protected BodyInputData $bodyParameters;

public function get(string $key):null|InputDatum|MultipleInputDatum {
public function get(string $key):mixed {
return $this->parameters[$key] ?? null;
}

Expand Down
21 changes: 5 additions & 16 deletions src/InputData/Datum/FailedFileUpload.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,7 @@ public function __construct(
);
}

/**
* Retrieve the error associated with the uploaded file.
*
* The return value MUST be one of PHP's UPLOAD_ERR_XXX constants.
*
* If the file was uploaded successfully, this method MUST return
* UPLOAD_ERR_OK.
*
* Implementations SHOULD return the value stored in the "error" key of
* the file in the $_FILES array.
*
* @see http://php.net/manual/en/features.file-upload.errors.php
* @return int One of PHP's UPLOAD_ERR_XXX constants.
*/
/** @inheritDoc */
public function getError():int {
return $this->errorCode;
}
Expand All @@ -44,10 +31,12 @@ public function getErrorMessage():string {

switch($this->errorCode) {
case UPLOAD_ERR_INI_SIZE:
$msg = "The uploaded file exceeds the upload_max_filesize directive in php.ini.";
$msg = "The uploaded file exceeds the "
. "upload_max_filesize directive in php.ini.";
break;
case UPLOAD_ERR_FORM_SIZE:
$msg = "The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.";
$msg = "The uploaded file exceeds the MAX_FILE_SIZE "
. "directive that was specified in the HTML form.";
break;
case UPLOAD_ERR_PARTIAL:
$msg = "The uploaded file was only partially uploaded.";
Expand Down
6 changes: 3 additions & 3 deletions src/InputData/Datum/FileUpload.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use Gt\Http\Stream;
use Gt\Input\UploadedFileMoveException;
use Gt\Input\UploadedFileSecurityException;
use InvalidArgumentException;
use Psr\Http\Message\StreamInterface;
use Psr\Http\Message\UploadedFileInterface;
use SplFileInfo;
Expand Down Expand Up @@ -47,7 +48,6 @@ public function getOriginalExtension():string {
return pathinfo($this->originalFileName, PATHINFO_EXTENSION);
}

/** @inheritDoc */
public function getSize():int {
return $this->fileSize;
}
Expand Down Expand Up @@ -94,8 +94,8 @@ public function moveTo(string $targetPath):void {

/** @inheritDoc */
public function getError():int {
// Note that this class ALWAYS returns UPLOAD_ERR_OK, due to failed uploads being
// represented by another class, FailedFileUpload.
// Note that this class ALWAYS returns UPLOAD_ERR_OK, due to failed uploads
// being represented by another class, FailedFileUpload.
return UPLOAD_ERR_OK;
}

Expand Down
Loading

0 comments on commit 7e82009

Please sign in to comment.