Skip to content

Commit

Permalink
Merge pull request #47 from madapaja/php84
Browse files Browse the repository at this point in the history
Enable PHP 8.4, Drop Twig 2.x and Windows test support
  • Loading branch information
koriym authored Nov 23, 2024
2 parents 40050fc + fbc23b1 commit 0df8a84
Show file tree
Hide file tree
Showing 17 changed files with 2,893 additions and 41 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@
/.php_cs.cache
/.phpunit.result.cache
/.phpcs-cache
/vendor-bin/tools/vendor/

38 changes: 20 additions & 18 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,19 @@
],
"require": {
"php": "^8.1",
"bear/resource": "^1.20",
"bear/sunday": "^1.2",
"bear/resource": "^1.23",
"bear/sunday": "^1.7",
"bear/app-meta": "^1.8",
"twig/twig": "^2.15.3 || ^v3.4.3",
"mobiledetect/mobiledetectlib": "^2.7 || ^3.74",
"ray/di": "^2.7",
"ray/aop": "^2.13",
"psr/log": "^3.0"
"twig/twig": "^v3.15",
"mobiledetect/mobiledetectlib": "^3.74.3",
"ray/di": "^2.17",
"ray/aop": "^2.17",
"psr/log": "^3.0.2"
},
"require-dev": {
"doctrine/annotations": "^1.13.3 || ^2.0",
"phpunit/phpunit": "^9.5.21",
"squizlabs/php_codesniffer": "^3.7",
"phpmd/phpmd": "^2.6",
"rector/rector": "^0.15.3",
"ray/rector-ray": "^1.0",
"doctrine/coding-standard": "^12.0",
"vimeo/psalm": "^5.4",
"phpstan/phpstan": "^1.10"
"phpunit/phpunit": "^9.6.21",
"doctrine/annotations": "^1.14.4 || ^2.0",
"bamarni/composer-bin-plugin": "^1.8.2"
},
"autoload":{
"psr-4":{
Expand All @@ -46,7 +40,8 @@
"tests",
"tests/Fake/src"
]
}
},
"files": ["tests/test_path.php"]
},
"scripts": {
"test": ["phpunit"],
Expand All @@ -62,7 +57,14 @@
},
"config": {
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true
"dealerdirect/phpcodesniffer-composer-installer": true,
"bamarni/composer-bin-plugin": true
}
},
"extra": {
"bamarni-bin": {
"bin-links": true,
"forward-command": true
}
}
}
10 changes: 6 additions & 4 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
parameters:
level: max
paths:
level: max
paths:
- src
- tests
excludePaths:
excludePaths:
- */tests/tmp/*
- */tests/Fake/*
ignoreErrors:
ignoreErrors:
- '#PHPDoc tag @SuppressWarnings has invalid value#'
- '#Cannot cast mixed to string#'
6 changes: 4 additions & 2 deletions src/AppPathProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use BEAR\AppMeta\AbstractAppMeta;
use Ray\Di\ProviderInterface;

use const DIRECTORY_SEPARATOR;

/** @implements ProviderInterface<array<string>> */
class AppPathProvider implements ProviderInterface
{
Expand All @@ -23,8 +25,8 @@ public function get(): array
$appDir = $this->appMeta->appDir;

return [
$appDir . '/src/Resource',
$appDir . '/var/templates',
$appDir . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'Resource',
$appDir . DIRECTORY_SEPARATOR . 'var' . DIRECTORY_SEPARATOR . 'templates',
];
}
}
4 changes: 3 additions & 1 deletion src/TemplateFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@
use function strpos;
use function substr;

use const DIRECTORY_SEPARATOR;

class TemplateFinder implements TemplateFinderInterface
{
/**
* {@inheritDoc}
*/
public function __invoke(string $name): string
{
$pos = strpos($name, '/Resource/');
$pos = strpos($name, DIRECTORY_SEPARATOR . 'Resource' . DIRECTORY_SEPARATOR);
assert(is_int($pos));
$relativePath = substr($name, $pos + 10);

Expand Down
2 changes: 1 addition & 1 deletion src/TwigRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ private function getReflection(ResourceObject $ro): ReflectionClass
return new ReflectionClass($ro);
}

/** @return array<string, mixed> */
/** @return array<mixed> */
private function buildBody(ResourceObject $ro): array
{
$body = is_array($ro->body) ? $ro->body : [];
Expand Down
6 changes: 3 additions & 3 deletions tests/AppPathProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@

use function is_dir;
use function mkdir;
use function test_path;

class AppPathProviderTest extends TestCase
{
public function testAppPathProvider(): void
{
$appDir = __DIR__ . '/Fake';
$paths = [
$appDir . '/src/Resource',
$appDir . '/var/templates',
test_path(__DIR__ . '/Fake/src/Resource'),
test_path(__DIR__ . '/Fake/var/templates'),
];

foreach ($paths as $path) {
Expand Down
6 changes: 3 additions & 3 deletions tests/ArrayLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ public function testIndex(): void
{
$ro = $this->injector->getInstance(Index::class);

$this->assertSame('Hello, BEAR.Sunday!', (string) $ro->onGet());
$this->assertEquals('Hello, BEAR.Sunday!', $ro->onGet());
}

public function testIndexWithArg(): void
{
$ro = $this->injector->getInstance(Index::class);

$this->assertSame('Hello, Madapaja!', (string) $ro->onGet('Madapaja'));
$this->assertEquals('Hello, Madapaja!', (string) $ro->onGet('Madapaja'));
}

public function testTemplateNotFoundException(): void
Expand All @@ -61,6 +61,6 @@ public function testPage(): void
{
$ro = $this->injector->getInstance(Page::class);

$this->assertSame('<!DOCTYPE html><html><head><title>Page</title><body>Hello, BEAR.Sunday!</body></html>', (string) $ro->onGet());
$this->assertEquals('<!DOCTYPE html><html><head><title>Page</title><body>Hello, BEAR.Sunday!</body></html>', (string) $ro->onGet());
}
}
2 changes: 2 additions & 0 deletions tests/Fake/src/FakeHttpResponder.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@
class FakeHttpResponder implements TransferInterface
{
public static $code;
/** @var array<string, string> */
public static $headers = [];
public static $content;

public static function reset()
{
/** @var array<string, string> */
static::$headers = [];
static::$content = null;
}
Expand Down
3 changes: 2 additions & 1 deletion tests/Fake/src/Resource/Page/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
namespace Madapaja\TwigModule\Resource\Page;

use BEAR\Resource\ResourceObject;
use function test_path;

class Index extends ResourceObject
{
Expand All @@ -21,7 +22,7 @@ public function onGet($name = 'BEAR.Sunday')

public function onPost($name = 'BEAR.Sunday')
{
$this->templatePath = __DIR__ . '/IndexPost.html.twig';
$this->templatePath = test_path(__DIR__ . '/IndexPost.html.twig');
$this['name'] = $name;
$this['isPost'] = true;

Expand Down
14 changes: 8 additions & 6 deletions tests/MobileTemplateFinderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
use Ray\Di\AbstractModule;
use Ray\Di\Injector;

use function test_path;

class MobileTemplateFinderTest extends TestCase
{
private Injector $injector;
Expand All @@ -29,20 +31,20 @@ protected function configure(): void
public function testMobileTemplate(): void
{
$_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (iPhone; CPU iPhone OS 6_0_1 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A523 Safari/8536.25';
$paths = [__DIR__ . '/Fake/src/Resource'];
$paths = [test_path(__DIR__ . '/Fake/src/Resource')];
$templateFinder = new MobileTemplateFinder($paths, new TemplateFinder());
$file = ($templateFinder)(__DIR__ . '/Resource/Page/Index.php');
$expected = 'Page/Index.mobile.twig';
$file = ($templateFinder)(test_path(__DIR__ . '/Resource/Page/Index.php'));
$expected = test_path('Page/Index.mobile.twig');
$this->assertSame($expected, $file);
}

public function testPcTemplate(): void
{
$_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)';
$paths = [__DIR__ . '/Fake/src/Resource'];
$paths = [test_path(__DIR__ . '/Fake/src/Resource')];
$templateFinder = new MobileTemplateFinder($paths, new TemplateFinder());
$file = ($templateFinder)(__DIR__ . '/Resource/Page/Index.php');
$expected = 'Page/Index.html.twig';
$file = ($templateFinder)(test_path(__DIR__ . '/Resource/Page/Index.php'));
$expected = test_path('Page/Index.html.twig');
$this->assertSame($expected, $file);
}

Expand Down
3 changes: 2 additions & 1 deletion tests/OptionProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@

use function is_dir;
use function mkdir;
use function test_path;

class OptionProviderTest extends TestCase
{
public string $tmpDir;

public function setUp(): void
{
$this->tmpDir = __DIR__ . '/tmp/optionProvider/tmp';
$this->tmpDir = test_path(__DIR__ . '/tmp/optionProvider/tmp');
if (is_dir($this->tmpDir)) {
return;
}
Expand Down
1 change: 0 additions & 1 deletion tests/TwigErrorPageModuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ protected function configure(): void
});
$injector = (new Injector($module));
$error = $injector->getInstance(ErrorInterface::class);
assert($error instanceof ErrorInterface);
$error->handle(new RuntimeException(), new RouterMatch())->transfer();
assert(is_array(FakeTransfer::$ro->body));
$this->assertArrayHasKey('status', FakeTransfer::$ro->body);
Expand Down
8 changes: 8 additions & 0 deletions tests/test_path.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

declare(strict_types=1);

function test_path(string $path): string
{
return str_replace('/', DIRECTORY_SEPARATOR, $path);
}
Empty file removed tests/tmp/.placeholder
Empty file.
15 changes: 15 additions & 0 deletions vendor-bin/tools/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"require-dev": {
"doctrine/coding-standard": "^12.0",
"phpmd/phpmd": "^2.15",
"phpstan/phpstan": "^2.0.2",
"squizlabs/php_codesniffer": "^3.11.1",
"vimeo/psalm": "^5.26.1"
},
"config": {
"sort-packages": true,
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true
}
}
}
Loading

0 comments on commit 0df8a84

Please sign in to comment.