-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
1,149 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
vendor | ||
wordpress | ||
composer.lock | ||
coverage-report | ||
coverage.xml | ||
clover.xml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
{ | ||
"name": "gin0115/wpunit-helpers", | ||
"type": "library", | ||
"description": "A series of helper classes, functions and traits for testing with WPUnit for WordPress", | ||
"keywords": [], | ||
"homepage": "https://pinkcrab.co.uk", | ||
"license": "MIT", | ||
"authors": [{ | ||
"name": "Glynn Quelch", | ||
"email": "[email protected]", | ||
"homepage": "http://clappo.co.uk", | ||
"role": "Developer" | ||
}], | ||
"autoload": { | ||
"psr-4": { | ||
"Gin0115\\WPUnit_Helpers\\": "src/" | ||
}, | ||
"files": [] | ||
}, | ||
"autoload-dev": { | ||
"psr-4": { | ||
"Gin0115\\WPUnit_Helpers\\Tests\\": "tests/" | ||
} | ||
}, | ||
"require-dev": { | ||
"phpunit/phpunit": "^7.0", | ||
"roots/wordpress": "^5.5", | ||
"wp-phpunit/wp-phpunit": "^5.0", | ||
"symfony/var-dumper": "4.*", | ||
"phpstan/phpstan": "^0.12.6", | ||
"szepeviktor/phpstan-wordpress": "^0.7.2", | ||
"php-stubs/wordpress-stubs": "^5.6.0", | ||
"dealerdirect/phpcodesniffer-composer-installer": "*", | ||
"wp-coding-standards/wpcs": "*", | ||
"object-calisthenics/phpcs-calisthenics-rules": "*" | ||
}, | ||
"require": { | ||
"php": ">=7.1.0", | ||
"pinkcrab/function-constructors": "~0.1.0" | ||
}, | ||
"scripts": { | ||
"test": "phpunit --coverage-clover clover.xml", | ||
"coverage": "phpunit --coverage-html coverage-report", | ||
"analyse": "vendor/bin/phpstan analyse src -l8", | ||
"sniff": "./vendor/bin/phpcs src/ -v", | ||
"all": "composer test && composer analyse && composer sniff" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
# OBJECTS | ||
|
||
The objects helper class provides 3 methods for making using Reflection a little cleaner and shorter. | ||
|
||
Can be used with public/protected/private, methods & properties, including static's. | ||
|
||
## How to use. | ||
|
||
All methods in this helper class are self contained and called in via static methods. | ||
|
||
### PROPERTIES | ||
|
||
``` php | ||
|
||
use Gin0115\WPUnit_Helpers\Objects; | ||
|
||
class Foo { | ||
|
||
public $public_property = 'public FOO'; | ||
protected $protected_property = 'protected FOO'; | ||
private $private_property = 'private FOO'; | ||
|
||
public static $public_static_property = 'public static FOO'; | ||
protected static $protected_static_property = 'protected static FOO'; | ||
private static $private_static_property = 'private static FOO'; | ||
|
||
} | ||
|
||
// Accessing. | ||
$instance = new Foo(); | ||
Objects::get_property($instance, 'public_property'); // public FOO | ||
Objects::get_property($instance, 'protected_property'); // protected FOO | ||
Objects::get_property($instance, 'private_property'); // private FOO | ||
|
||
Objects::get_property($instance, 'public_static_property'); // public static FOO | ||
Objects::get_property($instance, 'protected_static_property'); // protected static FOO | ||
Objects::get_property($instance, 'private_static_property'); // private static FOO | ||
|
||
// Setting | ||
Objects::set_property($instance, 'public_property', 'new public foo'); // new public foo | ||
Objects::set_property($instance, 'protected_property', 'new protected foo'); // new protected foo | ||
Objects::set_property($instance, 'private_property', 'new private foo'); // new private foo | ||
|
||
Objects::set_property($instance, 'public_static_property', 'new static public foo'); // new static public foo | ||
Objects::set_property($instance, 'protected_static_property', 'new static protected foo'); // new static protected foo | ||
Objects::set_property($instance, 'private_static_property', 'new static private foo'); // new static private foo | ||
``` | ||
|
||
### METHODS | ||
|
||
``` php | ||
|
||
use Gin0115\WPUnit_Helpers\Objects; | ||
|
||
class Foo { | ||
|
||
public function public_method(string $value): string { | ||
return 'from public_method ' . $value; | ||
} | ||
protected function protected_method(string $value): string { | ||
return 'from protected_method ' . $value; | ||
} | ||
private function private_method(string $value): string { | ||
return 'from private_method ' . $value; | ||
} | ||
|
||
public static function public_static_method(string $value): string { | ||
return 'from public_static_method ' . $value; | ||
} | ||
protected static function protected_static_method(string $value): string { | ||
return 'from protected_static_method ' . $value; | ||
} | ||
private static function private_static_method(string $value): string { | ||
return 'from private_static_method ' . $value; | ||
} | ||
|
||
} | ||
|
||
|
||
$instance = new Foo(); | ||
Objects::invoke_method($instance, 'invoked'); // from public_method invoked | ||
Objects::invoke_method($instance, 'invoked'); // from protected_method invoked | ||
Objects::invoke_method($instance, 'invoked'); // from private_method invoked | ||
|
||
Objects::invoke_method($instance, 'invoked'); // from public_static_method invoked | ||
Objects::invoke_method($instance, 'invoked'); // from protected_static_method invoked | ||
Objects::invoke_method($instance, 'invoked'); // from private_static_method invoked | ||
|
||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?xml version="1.0"?> | ||
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="Example Project" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/squizlabs/PHP_CodeSniffer/master/phpcs.xsd"> | ||
|
||
<description>PinkCrab general PHPCS rules for modules</description> | ||
|
||
<exclude-pattern>/vendor/*</exclude-pattern> | ||
<exclude-pattern>/wordpress/*</exclude-pattern> | ||
<exclude-pattern>/tests/*</exclude-pattern> | ||
|
||
<!-- Include the WordPress-Extra standard. --> | ||
<rule ref="WordPress-Extra"> | ||
<exclude name="WordPress.Files.FileName.NotHyphenatedLowercase"/> | ||
<exclude name="WordPress.Files.FileName.InvalidClassFileName"/> | ||
<exclude name="WordPress.PHP.YodaConditions.NotYoda"/> | ||
<exclude name="WordPress.WP.AlternativeFunctions.curl_curl_getinfo"/> | ||
<exclude name="WordPress.WP.AlternativeFunctions.curl_curl_setopt"/> | ||
<exclude name="WordPress.WP.AlternativeFunctions.curl_curl_exec"/> | ||
<exclude name="WordPress.WP.AlternativeFunctions.curl_curl_init"/> | ||
<exclude name="WordPress.WP.AlternativeFunctions.curl_curl_close"/> | ||
<exclude name="WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents"/> | ||
<exclude name="WordPress.PHP.DisallowShortTernary.Found"/> | ||
</rule> | ||
|
||
<!-- Add in some extra rules from other standards. --> | ||
<rule ref="Generic.CodeAnalysis.UnusedFunctionParameter"/> | ||
<rule ref="Generic.Commenting.Todo"/> | ||
|
||
|
||
<config name="minimum_supported_wp_version" value="4.9"/> | ||
|
||
|
||
|
||
</ruleset> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# Start command: composer update --classmap-authoritative && vendor/bin/phpstan analyze | ||
|
||
includes: | ||
- vendor/phpstan/phpstan/conf/bleedingEdge.neon | ||
- vendor/szepeviktor/phpstan-wordpress/extension.neon | ||
parameters: | ||
level: max | ||
inferPrivatePropertyTypeFromConstructor: true | ||
paths: | ||
- %currentWorkingDirectory%/src/ | ||
excludes_analyse: | ||
- %currentWorkingDirectory%/tests/* | ||
bootstrapFiles: | ||
- vendor/php-stubs/wordpress-stubs/wordpress-stubs.php |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?xml version="1.0"?> | ||
<phpunit | ||
bootstrap="tests/bootstrap.php" | ||
backupGlobals="false" | ||
colors="true" | ||
convertErrorsToExceptions="true" | ||
convertNoticesToExceptions="true" | ||
convertWarningsToExceptions="true" | ||
executionOrder="random" | ||
> | ||
<testsuites> | ||
<testsuite name="wpunit_helpers"> | ||
<directory prefix="Test_" suffix=".php">./tests/</directory> | ||
</testsuite> | ||
</testsuites> | ||
|
||
<filter> | ||
<whitelist processUncoveredFilesFromWhitelist="true"> | ||
<directory suffix=".php">./src/</directory> | ||
</whitelist> | ||
</filter> | ||
|
||
<php> | ||
<env name="WP_PHPUNIT__TESTS_CONFIG" value="tests/wp-config.php" /> | ||
</php> | ||
|
||
<logging> | ||
<log type="coverage-clover" target="clover.xml"/> | ||
</logging> | ||
</phpunit> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<?php | ||
|
||
declare (strict_types=1); | ||
|
||
/** | ||
* Objects helper functions for working private and protected properties/methods | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
* | ||
* @author Glynn Quelch <[email protected]> | ||
* @license http://www.opensource.org/licenses/mit-license.html MIT License | ||
* @package Gin0115\WPUnit_Helpers | ||
* @since 1.0.0 | ||
* */ | ||
|
||
namespace Gin0115\WPUnit_Helpers; | ||
|
||
use stdClass; | ||
|
||
class Objects { | ||
|
||
/** | ||
* Gets private or protected propertoes from an object. | ||
* | ||
* @param object $object | ||
* @param string $property | ||
* @return mixed | ||
*/ | ||
public static function get_property( &$object, string $property ) { | ||
$reflection = new \ReflectionClass( get_class( $object ) ); | ||
$property_value = $reflection->getProperty( $property ); | ||
$property_value->setAccessible( true ); | ||
return $property_value->getValue( $object ); | ||
} | ||
|
||
/** | ||
* Call protected/private method of a class. | ||
* | ||
* @param object $object Instantiated object that we will run method on. | ||
* @param string $method_name Method name to call. | ||
* @param array<string, mixed> $parameters Array of parameters to pass into method. | ||
* @return mixed Method return. | ||
*/ | ||
public static function invoke_method( &$object, string $method_name, array $parameters = array() ) { | ||
$reflection = new \ReflectionClass( get_class( $object ) ); | ||
$method = $reflection->getMethod( $method_name ); | ||
$method->setAccessible( true ); | ||
|
||
return $method->invokeArgs( $object, $parameters ); | ||
} | ||
|
||
/** | ||
* Allows the setting of a private/protected property | ||
* | ||
* @param object $object | ||
* @param string $property | ||
* @param mixed $value | ||
* @return void | ||
*/ | ||
public static function set_property( &$object, string $property, $value ): void { | ||
$reflection = new \ReflectionClass( get_class( $object ) ); | ||
$property_value = $reflection->getProperty( $property ); | ||
$property_value->setAccessible( true ); | ||
$property_value->setValue( $object, $value ); | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
/** | ||
* Collection of helper methods for working with output. | ||
* | ||
* @author Glynn Quelch <[email protected]> | ||
* @since 1.0.0 | ||
* @package Gin0115/WPUnit_Helpers | ||
*/ | ||
|
||
declare( strict_types=1 ); | ||
|
||
namespace Gin0115\WPUnit_Helpers; | ||
|
||
class Output { | ||
|
||
/** | ||
* Prints a printable and strarts a new line. | ||
* | ||
* @param mixed $arg | ||
* @return void | ||
*/ | ||
public static function println( $arg ): void { | ||
print $arg . PHP_EOL; // phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped | ||
} | ||
|
||
/** | ||
* Basic buffer for catching output of a callable. | ||
* | ||
* @param callable $action | ||
* @return string | ||
*/ | ||
public static function buffer( callable $action ): string { | ||
$output = ''; | ||
ob_start(); | ||
$action(); | ||
$output = ob_get_contents(); | ||
ob_end_clean(); | ||
return $output ?: ''; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
/** | ||
* Functions which have no real place to live. | ||
* | ||
* @author Glynn Quelch <[email protected]> | ||
* @since 1.0.0 | ||
* @package Gin0115/WPUnit_Helpers | ||
*/ | ||
|
||
declare( strict_types=1 ); | ||
|
||
namespace Gin0115\WPUnit_Helpers; | ||
|
||
class Utils { | ||
|
||
/** | ||
* Array map, which gives access to array key and a selection of static | ||
* values. | ||
* | ||
* @param callable $function | ||
* @param iterable<mixed> $data | ||
* @param mixed ...$with | ||
* @return array<int, mixed> | ||
*/ | ||
public static function array_map_with( callable $function, iterable $data, ...$with ): array { | ||
$return = array(); | ||
foreach ( $data as $key => $value ) { | ||
$return[] = $function( $key, $value, ...$with ); | ||
} | ||
return $return; | ||
} | ||
} |
Empty file.
Oops, something went wrong.