Skip to content

Commit

Permalink
fix(codestyle): fix some codestyle issues in code and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rtm-ctrlz committed Jun 6, 2022
1 parent 2c53289 commit 8d4390e
Show file tree
Hide file tree
Showing 12 changed files with 39 additions and 57 deletions.
7 changes: 0 additions & 7 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,3 @@ parameters:
- ./test
bootstrapFiles:
- ./test/phpstan/runkit7.stub.php
ignoreErrors:
- message: '/Parameter #2 \$code of class ReflectionException constructor expects int, mixed given\./'
paths:
- ./src/ClassMethod/UndefinedClassMethod.php
- ./src/Constant/MockedClassConstant.php
- ./src/Constant/UndefinedClassConstant.php
- ./src/Functions/UndefinedFunction.php
2 changes: 1 addition & 1 deletion src/Constant/MockedGlobalConstant.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class MockedGlobalConstant extends BaseConstant
public function __construct(string $constantName, $newValue)
{
if (!defined($constantName)) {
throw new ReflectionException("Constant {$constantName} is not defined");
throw new ReflectionException("Constant $constantName is not defined");
}

$this->name = $constantName;
Expand Down
2 changes: 0 additions & 2 deletions src/Phpunit/MockedFunction.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ class MockedFunction
* @param InvocationOrder|null $invocationRule
*
* @throws ReflectionException
*
* @noinspection UnusedConstructorDependenciesInspection
*/
public function __construct(
TestCase $testCase,
Expand Down
5 changes: 2 additions & 3 deletions test/phpunit/ClassMethod/MockedClassMethodTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use ReflectionException;
use RuntimeException;

use function PHPUnit\Framework\assertSame;
use function uniqid;

/**
Expand Down Expand Up @@ -188,7 +187,7 @@ public function testCallOriginalPrivate(): void
{
$object = new ClassWithMethods();
$method = new MockedClassMethod(ClassWithMethods::class, 'privateMethod');
assertSame('privateMethod', $method->callOriginal($object));
self::assertSame('privateMethod', $method->callOriginal($object));
}

/**
Expand All @@ -199,6 +198,6 @@ public function testCallOriginalProtected(): void
{
$object = new ClassWithMethods();
$method = new MockedClassMethod(ClassWithMethods::class, 'protectedMethod');
assertSame('protectedMethod', $method->callOriginal($object));
self::assertSame('protectedMethod', $method->callOriginal($object));
}
}
8 changes: 3 additions & 5 deletions test/phpunit/ClassMethod/UndefinedClassMethodTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
use ReflectionMethod;

use function array_reduce;
use function PHPUnit\Framework\assertContains;
use function PHPUnit\Framework\assertNotContains;

/**
* @internal
Expand All @@ -40,13 +38,13 @@ public function testMissingMethod(): void
public function testRemoveMethod(string $class, string $method): void
{
$classReflection = new ReflectionClass($class);
assertContains($method, self::helperGetReflectionMethodNames($classReflection));
self::assertContains($method, self::helperGetReflectionMethodNames($classReflection));

$undefinedMethod = new UndefinedClassMethod($class, $method);
assertNotContains($method, self::helperGetReflectionMethodNames($classReflection));
self::assertNotContains($method, self::helperGetReflectionMethodNames($classReflection));

unset($undefinedMethod);
assertContains($method, self::helperGetReflectionMethodNames($classReflection));
self::assertContains($method, self::helperGetReflectionMethodNames($classReflection));
}

/**
Expand Down
13 changes: 6 additions & 7 deletions test/phpunit/Constant/MockedClassConstantTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use ReflectionClassConstant;
use ReflectionException;

use function PHPUnit\Framework\assertSame;
use function uniqid;

/**
Expand All @@ -36,16 +35,16 @@ public function testUsagePublic(): void
$mockedConst = new MockedClassConstant(ClassWithConstants::class, 'CONST_PUBLIC', 222);

// check #1: value override (main target)
assertSame(222, ClassWithConstants::CONST_PUBLIC);
self::assertSame(222, ClassWithConstants::CONST_PUBLIC);

// check #1: stored value
assertSame($originalValue, $mockedConst->getValue());
self::assertSame($originalValue, $mockedConst->getValue());

// task #2: remove object - changes should be reverted
unset($mockedConst);

// check #3: constant now having original value
assertSame($originalValue, ClassWithConstants::CONST_PUBLIC);
self::assertSame($originalValue, ClassWithConstants::CONST_PUBLIC);
}

/**
Expand All @@ -70,14 +69,14 @@ public function testUsageComplex(string $class, string $constantName): void
$mockedConst = new MockedClassConstant($class, $constantName, $newValue);

// check #1: value override (main target)
assertSame($newValue, $classReflection->getConstants()[$constantName]);
self::assertSame($newValue, $classReflection->getConstants()[$constantName]);
// check #2: stored value
assertSame($originalValue, $mockedConst->getValue());
self::assertSame($originalValue, $mockedConst->getValue());

// task #2: destroy object (and revert changes)
unset($mockedConst);

// check #3: changes are revered
assertSame($originalValue, $classReflection->getConstants()[$constantName]);
self::assertSame($originalValue, $classReflection->getConstants()[$constantName]);
}
}
13 changes: 5 additions & 8 deletions test/phpunit/Constant/MockedGlobalConstantTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@

use function constant;
use function define;
use function PHPUnit\Framework\assertFalse;
use function PHPUnit\Framework\assertNotEquals;
use function PHPUnit\Framework\assertSame;
use function uniqid;

/**
Expand All @@ -27,18 +24,18 @@ public function testMinimal(): void
{
$constantName = uniqid('CONST_', false);
$originalValue = uniqid('VALUE_', true);
assertFalse(defined($constantName));
self::assertFalse(defined($constantName));

define($constantName, $originalValue);
assertSame($originalValue, constant($constantName));
self::assertSame($originalValue, constant($constantName));

$mockedValue = 'someValue';
assertNotEquals($mockedValue, $originalValue);
self::assertNotEquals($mockedValue, $originalValue);
$mockedConstant = new MockedGlobalConstant($constantName, $mockedValue);

assertSame($mockedValue, constant($constantName));
self::assertSame($mockedValue, constant($constantName));

unset($mockedConstant);
assertSame($originalValue, constant($constantName));
self::assertSame($originalValue, constant($constantName));
}
}
12 changes: 4 additions & 8 deletions test/phpunit/Constant/UndefinedClassConstantTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@
use ReflectionClassConstant;
use ReflectionException;

use function PHPUnit\Framework\assertArrayHasKey;
use function PHPUnit\Framework\assertArrayNotHasKey;
use function PHPUnit\Framework\assertSame;

/**
* @internal
*/
Expand Down Expand Up @@ -104,22 +100,22 @@ public function testRemoveComplex(string $class, string $constantName): void
unset($constantReflection);

// check #0: validate test data
assertArrayHasKey($constantName, $classReflection->getConstants());
self::assertArrayHasKey($constantName, $classReflection->getConstants());

// task #1: remove constant
$mockedConst = new UndefinedClassConstant($class, $constantName);

// check #1: constant is missing
assertArrayNotHasKey($constantName, $classReflection->getConstants());
self::assertArrayNotHasKey($constantName, $classReflection->getConstants());

// check #2: stored value is safe
assertSame($originalValue, $mockedConst->getValue());
self::assertSame($originalValue, $mockedConst->getValue());

// task #2: revert changes
unset($mockedConst);

$constantReflection = new ReflectionClassConstant($class, $constantName);
$revertedValue = $constantReflection->getValue();
assertSame($originalValue, $revertedValue);
self::assertSame($originalValue, $revertedValue);
}
}
11 changes: 4 additions & 7 deletions test/phpunit/Constant/UndefinedGlobalConstantTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@
use function constant;
use function define;
use function defined;
use function PHPUnit\Framework\assertFalse;
use function PHPUnit\Framework\assertSame;
use function PHPUnit\Framework\assertTrue;
use function uniqid;

/**
Expand Down Expand Up @@ -44,20 +41,20 @@ public function testUndefinedComplex(): void
$originalValue = constant($constantName);

// check #0: validate test data
assertTrue(defined($constantName));
self::assertTrue(defined($constantName));

// task #1: remove constant
$mockedConst = new UndefinedGlobalConstant($constantName);

// check #1: constant is missing
assertFalse(defined($constantName));
self::assertFalse(defined($constantName));

// check #2: stored value is safe
assertSame($originalValue, $mockedConst->getValue());
self::assertSame($originalValue, $mockedConst->getValue());

// task #2: revert changes
unset($mockedConst);

assertSame($originalValue, constant($constantName));
self::assertSame($originalValue, constant($constantName));
}
}
2 changes: 2 additions & 0 deletions test/phpunit/Helpers/ClassWithMethods.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,13 @@ public function publicMethod(): string
return __FUNCTION__;
}

/** @noinspection PhpUnused */
protected function protectedMethod(): string
{
return __FUNCTION__;
}

/** @noinspection PhpUnusedPrivateMethodInspection */
private function privateMethod(): string
{
return __FUNCTION__;
Expand Down
2 changes: 2 additions & 0 deletions test/phpunit/Helpers/ExtendedClassWithMethods.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

/** @noinspection EmptyClassInspection */

declare(strict_types=1);

namespace QratorLabs\Smocky\Test\PhpUnit\Helpers;
Expand Down
19 changes: 10 additions & 9 deletions test/phpunit/Smocky/MockedEntityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,10 @@
use QratorLabs\Smocky\Test\PhpUnit\Helpers\ClassWithConstants;
use QratorLabs\Smocky\Test\PhpUnit\Helpers\ClassWithMethods;
use ReflectionClassConstant;
use ReflectionException;
use ReflectionMethod;

use function constant;
use function PHPUnit\Framework\assertEquals;
use function PHPUnit\Framework\assertNotSame;
use function PHPUnit\Framework\assertStringContainsString;
use function strtoupper;

/**
Expand Down Expand Up @@ -45,35 +43,38 @@ public function getStashedNamePublic(string $basename, string $prefix = ''): str

// check #1: contains basename
$basename = 'someName';
assertStringContainsString($basename, $helper->getStashedNamePublic($basename));
self::assertStringContainsString($basename, $helper->getStashedNamePublic($basename));

// check #2: contains basename and prefix
$prefix = 'somePrefix';
$name = $helper->getStashedNamePublic($basename, $prefix);
assertStringContainsString($basename, $name);
assertStringContainsString($prefix, $name);
self::assertStringContainsString($basename, $name);
self::assertStringContainsString($prefix, $name);

// check #3: unique
// yep, I know that we can't call `uniqid()` in true parallel,
// but let's make it look like pretty close calls
assertNotSame($helper->getStashedNamePublic($basename), $helper->getStashedNamePublic($basename));
self::assertNotSame($helper->getStashedNamePublic($basename), $helper->getStashedNamePublic($basename));
}

/**
* @throws ReflectionException
*/
public function testGetVisibility(): void
{
$mock = $this->getMockForAbstractClass(MockedEntity::class, []);
$method = new ReflectionMethod(MockedEntity::class, 'getVisibility');
$method->setAccessible(true);

foreach (ClassWithConstants::getDataForTests() as $key => $item) {
assertEquals(
self::assertEquals(
constant('RUNKIT7_ACC_' . strtoupper($key)),
$method->invoke($mock, new ReflectionClassConstant($item[0], $item[1]))
);
}
foreach (ClassWithMethods::getDataForTests() as $key => $item) {
[$vis] = explode(' ', $key);
assertEquals(
self::assertEquals(
constant('RUNKIT7_ACC_' . strtoupper($vis)),
$method->invoke($mock, new ReflectionMethod($item[0], $item[1]))
);
Expand Down

0 comments on commit 8d4390e

Please sign in to comment.