Skip to content

Commit

Permalink
Merge pull request #9 from FriendsOfCake/cake-4.x
Browse files Browse the repository at this point in the history
Cake 4.x
  • Loading branch information
ADmad authored Dec 24, 2019
2 parents f377fa7 + fca9a99 commit 81bce9c
Show file tree
Hide file tree
Showing 12 changed files with 228 additions and 213 deletions.
12 changes: 3 additions & 9 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,10 @@ root = true
[*]
charset = utf-8
end_of_line = lf
indent_size = 2
indent_size = 4
indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true

[*.html]
indent_size = 4

[*.php]
indent_size = 4

[*.xml]
indent_size = 4
[*.yml]
indent_size = 2
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
composer.lock
phpunit.xml
vendor
.phpunit.result.cache
14 changes: 7 additions & 7 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
language: php

php:
- 7.0
- 7.1
- 7.2
- 7.3
- 7.4

env:
global:
Expand All @@ -13,21 +13,21 @@ matrix:
fast_finish: true

include:
- php: 7.0
- php: 7.2
env: PHPCS=1 DEFAULT=0

- php: 7.0
- php: 7.2
env: PHPSTAN=1 DEFAULT=0

before_script:
- composer install --prefer-dist --no-interaction
- if [[ $PHPCS = 1 ]]; then composer require cakephp/cakephp-codesniffer:dev-master; fi
- if [[ $PHPSTAN = 1 ]]; then composer require phpstan/phpstan:^0.8; fi
- if [[ $PHPCS = 1 ]]; then composer require cakephp/cakephp-codesniffer:^4.0; fi
- if [[ $PHPSTAN = 1 ]]; then composer require phpstan/phpstan:^0.12; fi

script:
- if [[ $DEFAULT = 1 ]]; then vendor/bin/phpunit; fi
- if [[ $PHPCS = 1 ]]; then vendor/bin/phpcs -p --extensions=php --standard=vendor/cakephp/cakephp-codesniffer/CakePHP src/; fi
- if [[ $PHPSTAN = 1 ]]; then vendor/bin/phpstan analyse -l 5 src; fi
- if [[ $PHPSTAN = 1 ]]; then vendor/bin/phpstan analyse src; fi

notifications:
email: false
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ composer require friendsofcake/cakephp-test-utilities

## Traits

The usage of these traits requires at least PHP 5.4. At this point there are
two traits:
At this point there are two traits:

1. [`AccessibilityHelperTrait`](#accessibilityhelpertrait) : Gain access protected properties and methods.
2. [`CounterHelperTrait`](#counterhelpertrait) : Uses counters to help with the order of expectations.
Expand Down Expand Up @@ -84,9 +83,10 @@ class MyTest extends TestCase
{
use CompareTrait;

public function setUp()
public function setUp(): void
{
parent::setUp();

$this->_compareBasePath = 'comparisons/MyTest/';
}
}
Expand All @@ -109,7 +109,7 @@ public function testExample()
}
```

See [Cake's docs](https://book.cakephp.org/3.0/en/development/testing.html#comparing-test-results-to-a-file)
See [Cake's docs](https://book.cakephp.org/4/en/development/testing.html#comparing-test-results-to-a-file)
for more details on usage of `assertSameAsFile` on which these methods are
based.

Expand Down
15 changes: 7 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,19 @@
"role": "Polisher"
}
],
"config": {
"preferred-install": "source"
"require": {
"cakephp/cakephp": "^4.0"
},
"require-dev": {
"phpunit/phpunit": "~8.5.0"
},
"autoload": {
"psr-4": {
"FriendsOfCake\\TestUtilities\\": "src",
"FriendsOfCake\\TestUtilities\\Test\\": "tests"
}
},
"require-dev": {
"phpunit/phpunit": "^5.7.14|^6.0"
},
"require": {
"php": "^5.6 || ^7.0",
"cakephp/cakephp": "^3.4"
"config": {
"preferred-install": "source"
}
}
2 changes: 2 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
parameters:
level: 6
42 changes: 25 additions & 17 deletions src/AccessibilityHelperTrait.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
declare(strict_types=1);

namespace FriendsOfCake\TestUtilities;

Expand All @@ -17,7 +18,7 @@ trait AccessibilityHelperTrait
/**
* Default target to use for reflection.
*
* @var Object|string
* @var object|string
*/
public $defaultReflectionTarget = null;

Expand Down Expand Up @@ -47,7 +48,7 @@ trait AccessibilityHelperTrait
*
* @return void
*/
public function resetReflectionCache()
public function resetReflectionCache(): void
{
$this->_reflectionPropertyCache = [];
$this->_reflectionMethodCache = [];
Expand All @@ -58,10 +59,10 @@ public function resetReflectionCache()
* Map an instance of an object to its class name.
*
* @param object $instance an instance of a class
* @param string $class the key used in the reflection instance class
* @param string|null $class the key used in the reflection instance class
* @return void
*/
public function setReflectionClassInstance($instance, $class = null)
public function setReflectionClassInstance(object $instance, ?string $class = null): void
{
$class = $class ?: get_class($instance);
$this->_reflectionInstanceCache[$class] = $instance;
Expand All @@ -70,15 +71,19 @@ public function setReflectionClassInstance($instance, $class = null)
/**
* Get working instance of "$class".
*
* @param string $class the class name
* @param string|null $class the class name
* @return object
* @throws \Exception
*/
public function getReflectionInstance($class)
public function getReflectionInstance(?string $class = null): object
{
$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));
throw new Exception(sprintf(
'Unable to find instance of %s in the reflection cache. '
. 'Have you added it using "setReflectionClassInstance"?',
$class
));
}

return $this->_reflectionInstanceCache[$class];
Expand All @@ -92,7 +97,7 @@ public function getReflectionInstance($class)
* @param string $class Target reflection class
* @return mixed
*/
public function callProtectedMethod($method, $args = [], $class = null)
public function callProtectedMethod(string $method, array $args = [], ?string $class = null)
{
$class = $this->_getReflectionTargetClass($class);
$cacheKey = $class . '_' . $method;
Expand All @@ -112,7 +117,7 @@ public function callProtectedMethod($method, $args = [], $class = null)
* @param string $class Target reflection class
* @return mixed
*/
public function getProtectedProperty($property, $class = null)
public function getProtectedProperty(string $property, ?string $class = null)
{
$Instance = $this->_getReflectionPropertyInstance($property, $class);

Expand All @@ -127,7 +132,7 @@ public function getProtectedProperty($property, $class = null)
* @param string $class Target reflection class
* @return mixed
*/
public function setProtectedProperty($property, $value, $class = null)
public function setProtectedProperty(string $property, $value, ?string $class = null)
{
$Instance = $this->_getReflectionPropertyInstance($property, $class);

Expand All @@ -138,10 +143,10 @@ public function setProtectedProperty($property, $value, $class = null)
* Get a reflection property object.
*
* @param string $property the property to access/manipulate
* @param string $class the class name
* @param string|null $class the class name
* @return \ReflectionProperty
*/
protected function _getReflectionPropertyInstance($property, $class)
protected function _getReflectionPropertyInstance(string $property, ?string $class = null): ReflectionProperty
{
$class = $this->_getReflectionTargetClass($class);
$cacheKey = $class . '_' . $property;
Expand All @@ -157,16 +162,19 @@ protected function _getReflectionPropertyInstance($property, $class)
/**
* Get the reflection class name.
*
* @param string $class the class name
* @param string|object|null $class the class name
* @return string
* @throws \Exception
*/
protected function _getReflectionTargetClass($class)
protected function _getReflectionTargetClass($class = null): string
{
$class = $class ?: $this->defaultReflectionTarget;

if (!$class) {
throw new Exception('Unable to find reflection target; have you set $defaultReflectionTarget or passed in a class name?');
throw new Exception(
'Unable to find reflection target. '
. 'Have you set $defaultReflectionTarget or passed in a class name?'
);
}

if (!is_object($class)) {
Expand All @@ -183,7 +191,7 @@ protected function _getReflectionTargetClass($class)
* @param string $method the method name
* @return \ReflectionMethod
*/
protected function _getNewReflectionMethod($class, $method)
protected function _getNewReflectionMethod(string $class, string $method): ReflectionMethod
{
return new ReflectionMethod($class, $method);
}
Expand All @@ -195,7 +203,7 @@ protected function _getNewReflectionMethod($class, $method)
* @param string $property the property to access/manipulate
* @return \ReflectionProperty
*/
protected function _getNewReflectionProperty($class, $property)
protected function _getNewReflectionProperty(string $class, string $property): ReflectionProperty
{
return new ReflectionProperty($class, $property);
}
Expand Down
28 changes: 16 additions & 12 deletions src/CompareTrait.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php
declare(strict_types=1);

namespace FriendsOfCake\TestUtilities;

use Cake\Filesystem\File;
use Cake\TestSuite\StringCompareTrait;
use \ReflectionClass;
use ReflectionClass;

/**
* Assert methods, comparing to files for:
Expand All @@ -26,7 +26,7 @@ trait CompareTrait
* @param string $result test result as a string
* @return void
*/
public function assertHtmlSameAsFile($path, $result)
public function assertHtmlSameAsFile(string $path, string $result): void
{
$indented = $this->indentHtml($result);
$this->assertSameAsFile($path, $indented);
Expand All @@ -38,13 +38,18 @@ public function assertHtmlSameAsFile($path, $result)
* Compares the array representation
*
* @param string $path partial path to test comparison file
* @param array $result test result as an array
* @param mixed $result test result as an array
* @return void
*/
public function assertJsonSameAsFile($path, $result)
public function assertJsonSameAsFile(string $path, $result): void
{
if (!file_exists($path)) {
$path = $this->_compareBasePath . $path;

$dir = dirname($path);
if (!is_dir($dir)) {
mkdir($dir, 0777, true);
}
}
if ($this->_updateComparisons === null) {
$this->_updateComparisons = env('UPDATE_TEST_COMPARISON_FILES');
Expand All @@ -54,8 +59,7 @@ public function assertJsonSameAsFile($path, $result)
$result,
JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE
) . "\n";
$file = new File($path, true);
$file->write($indented);
file_put_contents($path, $indented);
}

$expected = json_decode(file_get_contents($path), true);
Expand All @@ -69,7 +73,7 @@ public function assertJsonSameAsFile($path, $result)
* @param string $result test result as a string
* @return void
*/
public function assertXmlSameAsFile($path, $result)
public function assertXmlSameAsFile(string $path, string $result): void
{
$indented = $this->indentXml($result);
$this->assertSameAsFile($path, $indented);
Expand All @@ -80,7 +84,7 @@ public function assertXmlSameAsFile($path, $result)
*
* @return void
*/
protected function initComparePath()
protected function initComparePath(): void
{
if ($this->_compareBasePath) {
return;
Expand All @@ -91,7 +95,7 @@ protected function initComparePath()
'TestCase',
'comparisons',
substr($reflector->getFileName(), 0, -8)
) . DS;
) . DIRECTORY_SEPARATOR;
}

/**
Expand All @@ -104,7 +108,7 @@ protected function initComparePath()
* @param string $html the html string
* @return string
*/
protected function indentHtml($html)
protected function indentHtml(string $html): string
{
$html = trim(preg_replace("/\s+/", ' ', $html));

Expand Down Expand Up @@ -146,7 +150,7 @@ protected function indentHtml($html)
* @param string $xml the xml string
* @return string
*/
protected function indentXml($xml)
protected function indentXml(string $xml): string
{
$header = '';
$headerPos = strpos($xml, '?>');
Expand Down
8 changes: 5 additions & 3 deletions src/CounterHelperTrait.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
<?php
declare(strict_types=1);

namespace FriendsOfCake\TestUtilities;

use PHPUnit\Framework\MockObject\Rule\InvokedAtIndex;

/**
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
Expand All @@ -10,7 +13,6 @@
*/
trait CounterHelperTrait
{

/**
* List of counters used by this test case
*
Expand All @@ -25,9 +27,9 @@ trait CounterHelperTrait
* Permits using multiple named counters
*
* @param mixed $name string or object
* @return \PHPUnit_Framework_MockObject_Matcher_InvokedAtIndex
* @return \PHPUnit\Framework\MockObject\Rule\InvokedAtIndex
*/
public function nextCounter($name = '')
public function nextCounter($name = ''): InvokedAtIndex
{
if (is_object($name)) {
$name = get_class($name);
Expand Down
Loading

0 comments on commit 81bce9c

Please sign in to comment.