Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 8 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Remove files for archives generated using `git archive`
.editorconfig export-ignore
.gitattributes export-ignore
.gitignore export-ignore
.github export-ignore
phpunit.xml.dist export-ignore
tests export-ignore
phpstan.neon export-ignore
40 changes: 23 additions & 17 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,18 @@ on:

jobs:
testsuite:
runs-on: ubuntu-22.04
runs-on: ubuntu-24.04
strategy:
fail-fast: false
matrix:
php-version: ['8.2']
prefer-lowest: ['']
php-version: ['8.5']
dependencies: [highest]
include:
- php-version: '8.1'
prefer-lowest: 'prefer-lowest'
- php-version: '8.5'
dependencies: 'lowest'
- php-version: '8.6'
dependencies: 'highest'
composer-options: '--ignore-platform-req=php+'

steps:
- uses: actions/checkout@v4
Expand All @@ -28,39 +31,42 @@ jobs:
with:
php-version: ${{ matrix.php-version }}
extensions: mbstring, intl
coverage: none
coverage: pcov

- name: Composer install
uses: ramsey/composer-install@v3
with:
dependency-versions: ${{ matrix.dependencies }}
composer-options: ${{ matrix.composer-options }}

- name: Run PHPUnit
run: |
if ${{ matrix.prefer-lowest == 'prefer-lowest' }}; then
composer update --prefer-lowest --prefer-stable
if [[ ${{ matrix.php-version }} == '8.5' && ${{ matrix.dependencies }} == 'highest' ]]; then
export CODECOVERAGE=1 && vendor/bin/phpunit --display-all-issues --fail-on-all-issues --do-not-fail-on-skipped --do-not-fail-on-incomplete --coverage-clover=coverage.xml
else
composer update
vendor/bin/phpunit --display-phpunit-deprecations --display-warnings --display-deprecations
fi

- name: Run PHPUnit
run: vendor/bin/phpunit

cs-stan:
name: Coding Standard & Static Analysis
runs-on: ubuntu-22.04
runs-on: ubuntu-24.04

steps:
- uses: actions/checkout@v4

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.1'
php-version: '8.5'
extensions: mbstring, intl
coverage: none
tools: phpstan:1.10,cs2pr
tools: phpstan:2,cs2pr

- name: Composer Install
run: composer require cakephp/cakephp-codesniffer
run: composer require --dev cakephp/cakephp-codesniffer

- name: Run phpcs
run: vendor/bin/phpcs --report=checkstyle --standard=vendor/cakephp/cakephp-codesniffer/CakePHP src/ tests/ | cs2pr
run: vendor/bin/phpcs --report=checkstyle | cs2pr

- name: Run phpstan
if: always()
Expand Down
8 changes: 5 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
}
],
"require": {
"cakephp/cakephp": "^5.0"
"cakephp/cakephp": "^6.0@dev"
},
"require-dev": {
"phpunit/phpunit": "^10.1"
"phpunit/phpunit": "^13.0"
},
"autoload": {
"psr-4": {
Expand All @@ -35,5 +35,7 @@
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true
}
}
},
"minimum-stability": "dev",
"prefer-stable": true
}
9 changes: 9 additions & 0 deletions phpcs.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0"?>
<ruleset name="CakePHP Test Utilities">
<rule ref="CakePHP" />

<arg value="ps"/>

<file>src</file>
<file>tests</file>
</ruleset>
4 changes: 3 additions & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
parameters:
level: 7
level: 8
paths:
- src/
ignoreErrors:
- identifier: trait.unused
21 changes: 15 additions & 6 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
colors="true"
processIsolation="false"
stopOnFailure="false"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/13.0/phpunit.xsd">
<testsuites>
<testsuite name="Test Utility Test Cases">
<directory>./tests/</directory>
</testsuite>
</testsuites>

<phpunit colors="true">
<testsuites>
<testsuite name="Test Utility Test Cases">
<directory>./tests/</directory>
</testsuite>
</testsuites>
<source>
<include>
<directory suffix=".php">src/</directory>
</include>
</source>
</phpunit>
56 changes: 27 additions & 29 deletions src/AccessibilityHelperTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,21 @@ trait AccessibilityHelperTrait
*
* @var array
*/
protected array $_reflectionPropertyCache = [];
protected array $reflectionPropertyCache = [];

/**
* List of Reflection methods made public.
*
* @var array
*/
protected array $_reflectionMethodCache = [];
protected array $reflectionMethodCache = [];

/**
* List of class names <=> instances used for invocation.
*
* @var array
*/
protected array $_reflectionInstanceCache = [];
protected array $reflectionInstanceCache = [];

/**
* Reset the internal reflection caches.
Expand All @@ -50,9 +50,9 @@ trait AccessibilityHelperTrait
*/
public function resetReflectionCache(): void
{
$this->_reflectionPropertyCache = [];
$this->_reflectionMethodCache = [];
$this->_reflectionInstanceCache = [];
$this->reflectionPropertyCache = [];
$this->reflectionMethodCache = [];
$this->reflectionInstanceCache = [];
}

/**
Expand All @@ -65,7 +65,7 @@ public function resetReflectionCache(): void
public function setReflectionClassInstance(object $instance, ?string $class = null): void
{
$class = $class ?: get_class($instance);
$this->_reflectionInstanceCache[$class] = $instance;
$this->reflectionInstanceCache[$class] = $instance;
}

/**
Expand All @@ -77,16 +77,16 @@ public function setReflectionClassInstance(object $instance, ?string $class = nu
*/
public function getReflectionInstance(?string $class = null): object
{
$class = $this->_getReflectionTargetClass($class);
if (empty($this->_reflectionInstanceCache[$class])) {
$class = $this->getReflectionTargetClass($class);
if (empty($this->reflectionInstanceCache[$class])) {
throw new Exception(sprintf(
'Unable to find instance of %s in the reflection cache. '
. 'Have you added it using "setReflectionClassInstance"?',
$class
$class,
));
}

return $this->_reflectionInstanceCache[$class];
return $this->reflectionInstanceCache[$class];
}

/**
Expand All @@ -99,15 +99,14 @@ public function getReflectionInstance(?string $class = null): object
*/
public function callProtectedMethod(string $method, array $args = [], object|string|null $class = null): mixed
{
$class = $this->_getReflectionTargetClass($class);
$class = $this->getReflectionTargetClass($class);
$cacheKey = $class . '_' . $method;

if (!isset($this->_reflectionMethodCache[$cacheKey])) {
$this->_reflectionMethodCache[$cacheKey] = $this->_getNewReflectionMethod($class, $method);
$this->_reflectionMethodCache[$cacheKey]->setAccessible(true);
if (!isset($this->reflectionMethodCache[$cacheKey])) {
$this->reflectionMethodCache[$cacheKey] = $this->getNewReflectionMethod($class, $method);
}

return $this->_reflectionMethodCache[$cacheKey]->invokeArgs($this->getReflectionInstance($class), $args);
return $this->reflectionMethodCache[$cacheKey]->invokeArgs($this->getReflectionInstance($class), $args);
}

/**
Expand All @@ -119,7 +118,7 @@ public function callProtectedMethod(string $method, array $args = [], object|str
*/
public function getProtectedProperty(string $property, ?string $class = null): mixed
{
$Instance = $this->_getReflectionPropertyInstance($property, $class);
$Instance = $this->getReflectionPropertyInstance($property, $class);

return $Instance->getValue($this->getReflectionInstance($class));
}
Expand All @@ -134,7 +133,7 @@ public function getProtectedProperty(string $property, ?string $class = null): m
*/
public function setProtectedProperty(string $property, mixed $value, ?string $class = null): void
{
$this->_getReflectionPropertyInstance($property, $class)
$this->getReflectionPropertyInstance($property, $class)
->setValue($this->getReflectionInstance($class), $value);
}

Expand All @@ -145,17 +144,16 @@ public function setProtectedProperty(string $property, mixed $value, ?string $cl
* @param string|null $class the class name
* @return \ReflectionProperty
*/
protected function _getReflectionPropertyInstance(string $property, ?string $class = null): ReflectionProperty
protected function getReflectionPropertyInstance(string $property, ?string $class = null): ReflectionProperty
{
$class = $this->_getReflectionTargetClass($class);
$class = $this->getReflectionTargetClass($class);
$cacheKey = $class . '_' . $property;

if (!isset($this->_reflectionPropertyCache[$cacheKey])) {
$this->_reflectionPropertyCache[$cacheKey] = $this->_getNewReflectionProperty($class, $property);
$this->_reflectionPropertyCache[$cacheKey]->setAccessible(true);
if (!isset($this->reflectionPropertyCache[$cacheKey])) {
$this->reflectionPropertyCache[$cacheKey] = $this->getNewReflectionProperty($class, $property);
}

return $this->_reflectionPropertyCache[$cacheKey];
return $this->reflectionPropertyCache[$cacheKey];
}

/**
Expand All @@ -165,13 +163,13 @@ protected function _getReflectionPropertyInstance(string $property, ?string $cla
* @return string
* @throws \Exception
*/
protected function _getReflectionTargetClass(object|string|null $class = null): string
protected function getReflectionTargetClass(object|string|null $class = null): string
{
if ($class === null) {
if (!isset($this->defaultReflectionTarget)) {
throw new Exception(
'Unable to find reflection target. '
. 'Have you set $defaultReflectionTarget or passed in a class name?'
. 'Have you set $defaultReflectionTarget or passed in a class name?',
);
}

Expand All @@ -192,19 +190,19 @@ protected function _getReflectionTargetClass(object|string|null $class = null):
* @param string $method the method name
* @return \ReflectionMethod
*/
protected function _getNewReflectionMethod(string $class, string $method): ReflectionMethod
protected function getNewReflectionMethod(string $class, string $method): ReflectionMethod
{
return new ReflectionMethod($class, $method);
}

/**
* Gets a new ReflectionProperty instance. Extracted for testing purposes.
*
* @param string $class the class name
* @param class-string $class the class name
* @param string $property the property to access/manipulate
* @return \ReflectionProperty
*/
protected function _getNewReflectionProperty(string $class, string $property): ReflectionProperty
protected function getNewReflectionProperty(string $class, string $property): ReflectionProperty
{
return new ReflectionProperty($class, $property);
}
Expand Down
16 changes: 8 additions & 8 deletions src/CompareTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,18 @@ public function assertHtmlSameAsFile(string $path, string $result): void
public function assertJsonSameAsFile(string $path, mixed $result): void
{
if (!file_exists($path)) {
$path = $this->_compareBasePath . $path;
$path = $this->compareBasePath . $path;

$dir = dirname($path);
if (!is_dir($dir)) {
mkdir($dir, 0777, true);
}
}
$this->_updateComparisons ??= getenv('UPDATE_TEST_COMPARISON_FILES') ?: false;
if ($this->_updateComparisons) {
$this->updateComparisons ??= (bool)getenv('UPDATE_TEST_COMPARISON_FILES');
if ($this->updateComparisons) {
$indented = json_encode(
$result,
JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE
JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE,
) . "\n";
file_put_contents($path, $indented);
}
Expand Down Expand Up @@ -84,15 +84,15 @@ public function assertXmlSameAsFile(string $path, string $result): void
*/
protected function initComparePath(): void
{
if ($this->_compareBasePath) {
if ($this->compareBasePath) {
return;
}

$reflector = new ReflectionClass($this);
$this->_compareBasePath = str_replace(
$this->compareBasePath = str_replace(
'TestCase',
'comparisons',
substr($reflector->getFileName(), 0, -8)
substr($reflector->getFileName(), 0, -8),
) . DIRECTORY_SEPARATOR;
}

Expand Down Expand Up @@ -120,7 +120,7 @@ protected function indentHtml(string $html): string
$match[1] = preg_replace(
'@ ([\w-]+="[^"]*")@',
"\n $indent\\1",
$match[1]
$match[1],
);
$isClosingTag = (bool)$match[2];
$isSelfClosingTag = (bool)$match[3];
Expand Down
Loading
Loading