Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 85 additions & 0 deletions .cs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

use PhpCsFixer\Config;

return (new Config())
->setUsingCache(false)
->setRiskyAllowed(true)
->setRules(
[
'@PSR1' => true,
'@PSR2' => true,
'align_multiline_comment' => ['comment_type' => 'phpdocs_only'],
'array_indentation' => true,
'array_syntax' => ['syntax' => 'short'],
'blank_line_between_import_groups' => true,
'braces_position' => [
'allow_single_line_empty_anonymous_classes' => true,
],
'cast_spaces' => ['space' => 'none'],
'class_definition' => [
'space_before_parenthesis' => true,
],
'compact_nullable_type_declaration' => true,
'concat_space' => ['spacing' => 'one'],
'declare_equal_normalize' => ['space' => 'none'],
'declare_strict_types' => false,
'echo_tag_syntax' => ['format' => 'long'],
'fully_qualified_strict_types' => true,
'function_declaration' => [
'closure_fn_spacing' => 'none',
],
'general_phpdoc_annotation_remove' => [
'annotations' => [
'author',
'package',
],
],
'global_namespace_import' => [
'import_classes' => true,
'import_constants' => null,
'import_functions' => null,
],
'increment_style' => ['style' => 'post'],
'list_syntax' => ['syntax' => 'short'],
'method_argument_space' => ['on_multiline' => 'ensure_fully_multiline'],
'no_null_property_initialization' => false,
'no_superfluous_phpdoc_tags' => false,
'no_unused_imports' => true,
'nullable_type_declaration_for_default_null_value' => false,
'operator_linebreak' => [
'only_booleans' => true,
'position' => 'beginning',
],
'ordered_imports' => [
'sort_algorithm' => 'alpha',
'imports_order' => ['class', 'function', 'const'],
],
'phpdoc_add_missing_param_annotation' => ['only_untyped' => true],
'phpdoc_align' => false,
'phpdoc_no_empty_return' => false,
'phpdoc_no_useless_inheritdoc' => false,
'phpdoc_order' => true,
'phpdoc_to_comment' => false,
'protected_to_private' => false,
'psr_autoloading' => true,
'single_line_throw' => false,
'trailing_comma_in_multiline' => [
'after_heredoc' => true,
'elements' => ['array_destructuring', 'arrays', 'match'],
],
'yoda_style' => [
'equal' => false,
'identical' => false,
'less_and_greater' => false,
],
],
)
->setFinder(
PhpCsFixer\Finder::create()
->in(__DIR__ . '/src')
->in(__DIR__ . '/tests')
->name('*.php')
->ignoreDotFiles(true)
->ignoreVCS(true),
);
33 changes: 17 additions & 16 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
# Enforce Unix newlines
* text=lf

# Exclude unused files
# see: https://redd.it/2jzp6k
/.coveralls.yml export-ignore
/.editorconfig export-ignore
/.gitattributes export-ignore
/.github export-ignore
/.gitignore export-ignore
/CONTRIBUTING.md export-ignore
/README.md export-ignore
/phpcs.xml.dist export-ignore
/phpstan.neon.dist export-ignore
/phpunit.xml.dist export-ignore
/tests export-ignore
# Enforce Unix newlines
* text=lf

# Exclude unused files
# see: https://redd.it/2jzp6k
/.coveralls.yml export-ignore
/.editorconfig export-ignore
/.gitattributes export-ignore
/.github export-ignore
/.gitignore export-ignore
/CONTRIBUTING.md export-ignore
/README.md export-ignore
/phpcs.xml.dist export-ignore
/phpstan.neon.dist export-ignore
/phpunit.xml.dist export-ignore
/tests export-ignore
/.cs export-ignore
7 changes: 3 additions & 4 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ jobs:
strategy:
fail-fast: false
matrix:
php: [ 7.4, 8.0, 8.1, 8.2, 8.3, 8.4, 8.5 ]
php: [ 8.1, 8.2, 8.3, 8.4, 8.5 ]
include:
- php: 8.4
analysis: true

steps:
- name: Checkout
uses: actions/checkout@v4
uses: actions/checkout@v6

- name: Set up PHP ${{ matrix.php }}
uses: shivammathur/setup-php@v2
Expand All @@ -28,8 +28,7 @@ jobs:
run: composer install --prefer-dist --no-progress --no-suggest

- name: Coding standards
if: matrix.analysis
run: composer sniffer:check
run: composer cs:check

- name: Static analysis
if: matrix.analysis
Expand Down
19 changes: 10 additions & 9 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,16 @@
],
"homepage": "https://www.slimframework.com",
"require": {
"php": "^7.4 || ^8.0",
"php": "^7.4 || ~8.1.0 || ~8.2.0 || ~8.3.0 || ~8.4.0 || ~8.5.0",
"psr/http-message": "^1.1 || ^2.0",
"slim/slim": "^4.12",
"symfony/polyfill-php81": "^1.29",
"twig/twig": "^3.11"
},
"require-dev": {
"phpstan/phpstan": "^1.10.59",
"phpunit/phpunit": "^9.6 || ^10",
"friendsofphp/php-cs-fixer": "^3.60",
"phpstan/phpstan": "^1 || ^2",
"phpunit/phpunit": "^9.6 || ^10 || ^11 || ^12 || ^13",
"psr/http-factory": "^1.0",
"squizlabs/php_codesniffer": "^3.9"
},
Expand All @@ -47,18 +48,18 @@
}
},
"scripts": {
"sniffer:check": "phpcs --standard=phpcs.xml",
"sniffer:fix": "phpcbf --standard=phpcs.xml",
"cs:check": "php-cs-fixer fix --dry-run --format=txt --verbose --diff --config=.cs.php --ansi --allow-unsupported-php-version=yes",
"cs:fix": "php-cs-fixer fix --config=.cs.php --ansi --verbose --allow-unsupported-php-version=yes",
"stan": "phpstan analyse -c phpstan.neon --no-progress --ansi",
"test": "phpunit --configuration phpunit.xml --do-not-cache-result --colors=always",
"test": "phpunit --configuration phpunit.xml --do-not-cache-result --colors=always --display-warnings --display-deprecations --no-coverage",
"test:all": [
"@sniffer:check",
"@cs:check",
"@stan",
"@test:coverage"
"@test"
],
"test:coverage": [
"@putenv XDEBUG_MODE=coverage",
"phpunit --configuration phpunit.xml --do-not-cache-result --colors=always --coverage-clover build/coverage/clover.xml --coverage-html build/coverage --coverage-text"
"phpunit --configuration phpunit.xml --do-not-cache-result --colors=always --display-warnings --display-deprecations --coverage-clover build/coverage/clover.xml --coverage-html build/coverage --coverage-text"
]
}
}
22 changes: 10 additions & 12 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.5/phpunit.xsd"
beStrictAboutChangesToGlobalState="true"
beStrictAboutOutputDuringTests="true"
colors="true"
bootstrap="tests/bootstrap.php"
>
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/11.2/phpunit.xsd"
beStrictAboutChangesToGlobalState="true" beStrictAboutOutputDuringTests="true" colors="true"
bootstrap="tests/bootstrap.php" executionOrder="random" cacheDirectory=".phpunit.cache">
<testsuites>
<testsuite name="Twig-View Test Suite">
<testsuite name="Slim Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>

<coverage processUncoveredFiles="true">
<include>
<directory>src</directory>
</include>
<coverage>
<report>
<html outputDirectory="coverage" lowUpperBound="20" highLowerBound="50"/>
</report>
</coverage>
<source>
<include>
<directory>src</directory>
</include>
</source>
</phpunit>
2 changes: 0 additions & 2 deletions src/TwigMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,12 @@

namespace Slim\Views;

use Psr\Container\ContainerInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use RuntimeException;
use Slim\App;
use Slim\Interfaces\RouteCollectorProxyInterface;
use Slim\Interfaces\RouteParserInterface;

class TwigMiddleware implements MiddlewareInterface
Expand Down
6 changes: 3 additions & 3 deletions src/TwigRuntimeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ public function fullUrlFor(string $routeName, array $data = [], array $queryPara
*/
public function isCurrentUrl(string $routeName, array $data = []): bool
{
$currentUrl = $this->basePath . $this->uri->getPath();
$result = $this->routeParser->urlFor($routeName, $data);
$currentUrl = $this->uri->getPath();
$result = $this->basePath . $this->routeParser->urlFor($routeName, $data);

return $result === $currentUrl;
}
Expand All @@ -84,7 +84,7 @@ public function isCurrentUrl(string $routeName, array $data = []): bool
*/
public function getCurrentUrl(bool $withQueryString = false): string
{
$currentUrl = $this->basePath . $this->uri->getPath();
$currentUrl = $this->uri->getPath();
$query = $this->uri->getQuery();

if ($withQueryString && !empty($query)) {
Expand Down
2 changes: 1 addition & 1 deletion src/TwigRuntimeLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function __construct(RouteParserInterface $routeParser, UriInterface $uri
*/
public function load(string $class)
{
if (TwigRuntimeExtension::class === $class) {
if ($class === TwigRuntimeExtension::class) {
return new $class($this->routeParser, $this->uri, $this->basePath);
}

Expand Down
15 changes: 11 additions & 4 deletions tests/TwigRuntimeExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace Slim\Tests;

use PHPUnit\Framework\Attributes\DataProvider;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\UriInterface;
use Slim\Interfaces\CallableResolverInterface;
Expand Down Expand Up @@ -69,10 +70,12 @@ protected function mapRouteCollectorRoute(
public static function isCurrentUrlProvider(): array
{
return [
['/hello/{name}', ['name' => 'world'], '/hello/world', '/base-path', true],
['/hello/{name}', ['name' => 'world'], '/hello/world', '', true],
['/hello/{name}', ['name' => 'world'], '/hello/john', '/base-path', false],
['/hello/{name}', ['name' => 'world'], '/base-path/hello/world', '/base-path', true],
['/hello/{name}', ['name' => 'world'], '/hello/john', '', false],
['/hello/{name}', ['name' => 'world'], '/hello/world', '/base-path', false],
['/hello/{name}', ['name' => 'world'], '/base-path/hello/john', '/base-path', false],
['/hello/{name}', ['name' => 'world'], '/other/path', '/base-path', false],
];
}

Expand All @@ -85,9 +88,10 @@ public static function isCurrentUrlProvider(): array
* @param string|null $basePath
* @param bool $expected
*/
#[DataProvider('isCurrentUrlProvider')]
public function testIsCurrentUrl(string $pattern, array $data, string $path, ?string $basePath, bool $expected)
{
$routeCollector = $this->createRouteCollector($basePath);
$routeCollector = $this->createRouteCollector('');
$routeParser = $routeCollector->getRouteParser();

$routeName = 'route';
Expand Down Expand Up @@ -122,6 +126,7 @@ public static function currentUrlProvider(): array
* @param string $basePath
* @param bool $withQueryString
*/
#[DataProvider('currentUrlProvider')]
public function testCurrentUrl(string $pattern, string $url, string $basePath, bool $withQueryString)
{
$routeCollector = $this->createRouteCollector($basePath);
Expand All @@ -143,7 +148,7 @@ public function testCurrentUrl(string $pattern, string $url, string $basePath, b
->method('getQuery')
->willReturn($query);

$expected = $basePath . $path;
$expected = $path;
if ($withQueryString) {
$expected .= '?' . $query;
}
Expand Down Expand Up @@ -173,6 +178,7 @@ public static function urlForProvider(): array
* @param string $basePath
* @param string $expectedUrl
*/
#[DataProvider('urlForProvider')]
public function testUrlFor(
string $pattern,
array $routeData,
Expand Down Expand Up @@ -216,6 +222,7 @@ public static function fullUrlForProvider(): array
* @param string $basePath
* @param string $expectedFullUrl
*/
#[DataProvider('fullUrlForProvider')]
public function testFullUrlFor(
string $pattern,
array $routeData,
Expand Down
Loading