Skip to content

Commit

Permalink
Fix errors/warnings flagged by PHPCS (#143)
Browse files Browse the repository at this point in the history
* Fix: phpcs errors

1. Blank line after function.
2. Ignore WordPress.NamingConventions.ValidHookName.UseUnderscores because of backward compatibility

* Fix: phpcs error for equal sign alignment

* Fix: phpcs errors

1. Spacing and equal line alignement issue.
2. Ignore object param and return type because assuming php greater than 7.1 also requrires php 7.4 as plugin description.
3. Change CamelCase to snake_case variables.

* Fix: phpcs error for file /tests/bootstrap.php for spacing issue

* Fix: phpcs errors

1. Spacing and alignment issue.
2. Naming convention
3. Multiline array comma issue

* Fix issue added by bot for undefined variable

* Exclude hook name validation rule instead ignoring

* Update unit tests workflow

* Add slah before wp_json_encode function

* Revert json encode change
  • Loading branch information
hbhalodia authored Feb 8, 2023
1 parent 4da9c8d commit ceb8772
Show file tree
Hide file tree
Showing 19 changed files with 495 additions and 377 deletions.
6 changes: 5 additions & 1 deletion .github/workflows/phpunit_on_pull_request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,13 @@ jobs:
if: steps.check_files.outputs.files_exists == 'true'
run: echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json"

- name: Update composer packages
if: steps.check_files.outputs.files_exists == 'true'
run: composer update

- name: Run PHPUnit
if: steps.check_files.outputs.files_exists == 'true'
run: composer update && composer tests:unit
run: vendor/bin/phpunit tests/php/Unit/ --verbose

- name: Archive code coverage results
uses: actions/upload-artifact@v2
Expand Down
5 changes: 5 additions & 0 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
<exclude name="Generic.Arrays.DisallowShortArraySyntax" />
<exclude name="Generic.Commenting.DocComment.MissingShort" />
<exclude name="WordPress.PHP.DisallowShortTernary" />
<!--
Below rule is added to ignore the hook name validation.
@todo: Remove once the hook name is updated to use underscores.
-->
<exclude name="WordPress.NamingConventions.ValidHookName.UseUnderscores" />
</rule>

<rule ref="WordPress-Extra" />
Expand Down
1 change: 1 addition & 0 deletions src/Interfaces/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
* @package WpGuruDev\OrderExport\Interfaces
*/
interface Container {

/**
* Define services in container.
*
Expand Down
1 change: 0 additions & 1 deletion src/Modules/Shortcode.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@ public function scan_shortcode( string $output, string $tag, $attrs ): string {
return $output;
}


/**
* Filter redirect URL as per shortcode param.
*
Expand Down
7 changes: 5 additions & 2 deletions src/Utils/GoogleClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -244,11 +244,14 @@ public function user(): \stdClass {
* @return string
*/
public function state(): string {
$state_data['nonce'] = wp_create_nonce( 'login_with_google' );

$state_data = [
'nonce' => wp_create_nonce( 'login_with_google' ),
];

$state_data = apply_filters( 'rtcamp.google_login_state', $state_data );
$state_data['provider'] = 'google';

return base64_encode( wp_json_encode( $state_data ) );
}

}
2 changes: 1 addition & 1 deletion templates/google-login-button.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

if ( is_user_logged_in() ) {
$button_text = __( 'Log out', 'login-with-google' );
$button_url = wp_logout_url( get_permalink() );
$button_url = wp_logout_url( get_permalink() );
}
?>
<div class="wp_google_login">
Expand Down
110 changes: 62 additions & 48 deletions tests/php/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@
*
* @author Guido Scialfa <[email protected]>
*/
class TestCase extends WPMockTestCase
{
class TestCase extends WPMockTestCase {

/**
* Sets up the fixture, for example, open a network connection.
* This method is called before a test is executed.
*/
public function setUp(): void
{
public function setUp(): void {

parent::setUp();
\WP_Mock::setUp();
}
Expand All @@ -35,8 +35,8 @@ public function setUp(): void
* Tears down the fixture, for example, close a network connection.
* This method is called after a test is executed.
*/
public function tearDown(): void
{
public function tearDown(): void {

parent::tearDown();
\WP_Mock::tearDown();
}
Expand All @@ -46,27 +46,28 @@ public function tearDown(): void
*
* Basic configuration available for all of the testee objects, call `getMock` to get the mock.
*
* @param string $className
* @param array $constructorArguments
* @param array $methods
* @param string $sutMethod
* @param string $class_name
* @param array $constructor_arguments
* @param array $methods
* @param string $sut_method
*
* @return PHPUnit_Framework_MockObject_MockBuilder
*/
protected function buildTesteeMock(
string $className,
array $constructorArguments,
string $class_name,
array $constructor_arguments,
array $methods,
string $sutMethod
): object {
string $sut_method
): object { // phpcs:ignore PHPCompatibility.FunctionDeclarations.NewReturnTypeDeclarations.objectFound -- Ignoring because assuming php application version greater than 7.1.

$testee = $this->getMockBuilder( $class_name );

$testee = $this->getMockBuilder($className);
$constructorArguments
? $testee->setConstructorArgs($constructorArguments)
$constructor_arguments
? $testee->setConstructorArgs( $constructor_arguments )
: $testee->disableOriginalConstructor();

$methods and $testee->setMethods($methods);
$sutMethod and $testee->setMethodsExcept([$sutMethod]);
$methods && $testee->setMethods( $methods );
$sut_method && $testee->setMethodsExcept( [ $sut_method ] );

return $testee;
}
Expand All @@ -75,31 +76,36 @@ protected function buildTesteeMock(
* Retrieve a Testee Mock to Test Protected Methods
*
* return MockBuilder
* @param string $className
* @param array $constructorArguments
*
* @param string $class_name
* @param array $constructor_arguments
* @param string $method
* @param array $methods
* @param array $methods
*
* @return array
*
* @throws ReflectionException
*/
protected function buildTesteeMethodMock(
string $className,
array $constructorArguments,
string $class_name,
array $constructor_arguments,
string $method,
array $methods
): array {

$testee = $this->buildTesteeMock(
$className,
$constructorArguments,
$class_name,
$constructor_arguments,
$methods,
''
)->getMock();
$reflectionMethod = new ReflectionMethod($className, $method);
$reflectionMethod->setAccessible(true);

$reflection_method = new ReflectionMethod( $class_name, $method );
$reflection_method->setAccessible( true );

return [
$testee,
$reflectionMethod,
$reflection_method,
];
}

Expand All @@ -112,13 +118,14 @@ protected function buildTesteeMethodMock(
//phpcs:disable Inpsyde.CodeQuality.ReturnTypeDeclaration.NoReturnType
protected function getTesteeProperty(
string $property,
object $object
object $object // phpcs:ignore PHPCompatibility.FunctionDeclarations.NewParamTypeDeclarations.objectFound -- Ignoring because assuming php application version greater than 7.1.
) {

$reflection = new \ReflectionClass($object);
$reflectionProperty = $reflection->getProperty($property);
$reflectionProperty->setAccessible(true);
return $reflectionProperty->getValue($object);
$reflection = new \ReflectionClass( $object );
$reflection_property = $reflection->getProperty( $property );

$reflection_property->setAccessible( true );
return $reflection_property->getValue( $object );
}

/**
Expand All @@ -131,12 +138,17 @@ protected function getTesteeProperty(
* @throws ReflectionException
*/
// phpcs:disable Inpsyde.CodeQuality.ArgumentTypeDeclaration.NoArgumentType
protected function setTesteeProperty(object $object, string $property, $value): void
{
$reflection = new \ReflectionClass($object);
$reflectionProperty = $reflection->getProperty($property);
$reflectionProperty->setAccessible(true);
$reflectionProperty->setValue($object, $value);
protected function setTesteeProperty(
object $object, // phpcs:ignore PHPCompatibility.FunctionDeclarations.NewParamTypeDeclarations.objectFound -- Ignoring because assuming php application version greater than 7.1.
string $property,
$value
): void {

$reflection = new \ReflectionClass( $object );
$reflection_property = $reflection->getProperty( $property );

$reflection_property->setAccessible( true );
$reflection_property->setValue( $object, $value );
}

/**
Expand All @@ -146,30 +158,32 @@ protected function setTesteeProperty(object $object, string $property, $value):
* @param string $function_name
* @param mixed $args
* @param int $times
*
* @param mixed $return
*/
protected function wpMockFunction(
string $functionName,
string $function_name,
$args = [],
int $times = 1,
$return = null
) {

$funcArgs = [
$func_args = [
'times' => $times,
];

if (!empty($args)) {
$funcArgs['args'] = $args;
if ( ! empty( $args ) ) {
$func_args['args'] = $args;
}

if (!empty($return)) {
$funcArgs['return'] = $return;
if ( ! empty( $return ) ) {
$func_args['return'] = $return;
}

\WP_Mock::userFunction(
$functionName,
$funcArgs
$function_name,
$func_args
);

}
}
36 changes: 21 additions & 15 deletions tests/php/Unit/ContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class ContainerTest extends TestCase {
/**
* @var PimpleContainer
*/
private $pimpleMock;
private $pimple_mock;

/**
* Object under test.
Expand All @@ -40,45 +40,51 @@ class ContainerTest extends TestCase {
* @return void
*/
public function setUp(): void {
$this->pimpleMock = $this->createMock( PimpleContainer::class );
$this->testee = new Testee( $this->pimpleMock );

$this->pimple_mock = $this->createMock( PimpleContainer::class );
$this->testee = new Testee( $this->pimple_mock );

}

public function testContainerImplementsInterface() {

$this->assertInstanceOf( ContainerInterface::class, $this->testee );
}

/**
* @covers ::get
*/
public function testGetThrowsExceptionForNonExistentService() {
$this->pimpleMock->expects( $this->once() )
->method( 'keys' )
->willReturn( [ 'example_service' ] );

$this->pimple_mock->expects( $this->once() )
->method( 'keys' )
->willReturn( [ 'example_service' ] );

$this->expectException( InvalidArgumentException::class );

$this->testee->get( 'non_existent_service' );
}

/**
* @covers ::get
*/
public function testGetReturnsServiceObject() {
$dummyService = (object) [

$dummy_service = (object) [
'some_key' => 'some_value',
'some_other_key' => 'some_other_value',
];

$this->testee->container['test_service'] = $dummyService;
$this->testee->container['test_service'] = $dummy_service;

$this->pimpleMock->expects( $this->once() )
->method( 'keys' )
->willReturn( [ 'test_service' ] );
$this->pimple_mock->expects( $this->once() )
->method( 'keys' )
->willReturn( [ 'test_service' ] );

$this->pimpleMock->expects( $this->once() )
->method( 'offsetGet' )
->with( 'test_service' )
->willReturn( $dummyService );
$this->pimple_mock->expects( $this->once() )
->method( 'offsetGet' )
->with( 'test_service' )
->willReturn( $dummy_service );

$this->testee->get( 'test_service' );
}
Expand Down
6 changes: 3 additions & 3 deletions tests/php/Unit/Modules/AssetsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function testInit() {
'login_enqueue_scripts',
[
$this->testee,
'enqueue_login_styles'
'enqueue_login_styles',
]
);

Expand Down Expand Up @@ -111,7 +111,7 @@ function () {
'login-with-google',
'https://example.com/assets/js/login.js',
[
'some-other-script'
'some-other-script',
],
false,
true,
Expand All @@ -124,7 +124,7 @@ function () {
'login-with-google',
'js/login.js',
[
'some-other-script'
'some-other-script',
]
);

Expand Down
Loading

0 comments on commit ceb8772

Please sign in to comment.