diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index b5b9f8a..d6f7f5d 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -8,7 +8,7 @@ jobs: strategy: matrix: - php: [7.3, 7.4, 8.0] + php: [7.4, 8.0, 8.1] steps: - name: Checkout code @@ -26,4 +26,4 @@ jobs: run: composer update --prefer-dist --no-progress --no-interaction - name: Run test suite - run: php vendor/bin/phpunit + run: php vendor/bin/phpunit diff --git a/README.md b/README.md index 74c4d2a..25b3622 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ Inspired by MiniTest of Ruby now you combine BDD and classical TDD style in one ## Installation -*Requires PHP >= 7.3* +*Requires PHP >= 7.4* * Install with Composer: diff --git a/composer.json b/composer.json index 4b9e0b6..3ac90fb 100644 --- a/composer.json +++ b/composer.json @@ -14,7 +14,7 @@ } ], "require": { - "php": ">=7.3.0", + "php": ">=7.4.0", "myclabs/deep-copy": "^1.10", "phpunit/phpunit": "^8.0|^9.0" }, diff --git a/src/Codeception/Specify.php b/src/Codeception/Specify.php index 81cfc72..72d49f0 100644 --- a/src/Codeception/Specify.php +++ b/src/Codeception/Specify.php @@ -23,6 +23,7 @@ public function specify(string $thing, Closure $code = null, $examples = []): ?s $this->runSpec($thing, $code, $examples); return null; } + return $this; } @@ -32,6 +33,7 @@ public function describe(string $feature, Closure $code = null): ?self $this->runSpec($feature, $code); return null; } + return $this; } @@ -41,6 +43,7 @@ public function it(string $specification, Closure $code = null, $examples = []): $this->runSpec($specification, $code, $examples); return $this; } + TestCase::markTestIncomplete(); return $this; } @@ -56,6 +59,7 @@ public function should(string $behavior, Closure $code = null, $examples = []): $this->runSpec('should ' . $behavior, $code, $examples); return $this; } + TestCase::markTestIncomplete(); return $this; } @@ -66,6 +70,7 @@ public function shouldNot(string $behavior, Closure $code = null, $examples = [] $this->runSpec('should not ' . $behavior, $code, $examples); return $this; } + TestCase::markTestIncomplete(); return $this; } diff --git a/src/Codeception/Specify/ObjectProperty.php b/src/Codeception/Specify/ObjectProperty.php index 691d342..e334c77 100644 --- a/src/Codeception/Specify/ObjectProperty.php +++ b/src/Codeception/Specify/ObjectProperty.php @@ -46,7 +46,7 @@ public function __construct($owner, $property, $value = null) $this->property->setAccessible(true); - $this->initValue = ($value === null ? $this->getValue() : $value); + $this->initValue = ($value ?? $this->getValue()); } public function getName(): string @@ -57,7 +57,7 @@ public function getName(): string /** * Restores initial value */ - public function restoreValue() + public function restoreValue(): void { $this->setValue($this->initValue); } @@ -73,7 +73,7 @@ public function getValue() /** * @param mixed $value */ - public function setValue($value) + public function setValue($value): void { $this->property->setValue($this->owner, $value); } diff --git a/src/Codeception/Specify/ResultPrinter.php b/src/Codeception/Specify/ResultPrinter.php index 4ab43be..227dc4d 100644 --- a/src/Codeception/Specify/ResultPrinter.php +++ b/src/Codeception/Specify/ResultPrinter.php @@ -15,13 +15,11 @@ class ResultPrinter extends DefaultResultPrinter protected function writeProgress(string $progress): void { $this->write($progress); - $this->column++; - $this->numTestsRun++; + ++$this->column; + ++$this->numTestsRun; - if ($this->column == $this->maxColumn || $this->numTestsRun == $this->numTests) { - if ($this->column == $this->maxColumn) { - $this->writeNewLine(); - } + if ($this->column === $this->maxColumn) { + $this->writeNewLine(); } } } diff --git a/src/Codeception/Specify/SpecifyHooks.php b/src/Codeception/Specify/SpecifyHooks.php index 6543c97..d84d5c9 100644 --- a/src/Codeception/Specify/SpecifyHooks.php +++ b/src/Codeception/Specify/SpecifyHooks.php @@ -15,21 +15,15 @@ trait SpecifyHooks { - private $afterSpecify = []; + private array $afterSpecify = []; - private $beforeSpecify = []; + private array $beforeSpecify = []; - /** - * @var DeepCopy - */ - private $copier; + private ?DeepCopy $copier = null; - /** - * @var SpecifyTest - */ - private $currentSpecifyTest; + private ?SpecifyTest $currentSpecifyTest = null; - private $specifyName = ''; + private string $specifyName = ''; private function getCurrentSpecifyTest(): SpecifyTest { @@ -43,7 +37,7 @@ private function getCurrentSpecifyTest(): SpecifyTest */ private function runSpec(string $specification, Closure $callable = null, $params = []) { - if (!$callable) { + if ($callable === null) { return; } @@ -106,8 +100,10 @@ private function getSpecifyExamples($params): array if (!is_array($params['examples'])) { throw new RuntimeException("Examples should be an array"); } + return $params['examples']; } + return [[]]; } @@ -116,6 +112,7 @@ private function specifyGetPhpUnitReflection(): ?ReflectionClass if ($this instanceof TestCase) { return new ReflectionClass(TestCase::class); } + return null; } @@ -161,7 +158,8 @@ private function getSpecifyObjectProperties(): array if (!$docBlock) { continue; } - if (preg_match('~\*(\s+)?@specify\s?~', $docBlock)) { + + if (preg_match('#\*(\s+)?@specify\s?#', $docBlock)) { $property->setAccessible(true); $clonedProperties[] = new ObjectProperty($this, $property); } diff --git a/src/Codeception/Specify/SpecifyTest.php b/src/Codeception/Specify/SpecifyTest.php index 383d011..57522ba 100644 --- a/src/Codeception/Specify/SpecifyTest.php +++ b/src/Codeception/Specify/SpecifyTest.php @@ -12,25 +12,29 @@ class SpecifyTest implements Test, SelfDescribing { - protected $name; + protected string $name = ''; - protected $test; + /** @var callable */ + protected $test = null; - protected $example; + protected array $example = []; + /** + * @var mixed|null + */ protected $throws; - public function __construct($test) + public function __construct(callable $test) { $this->test = $test; } - public function setName($name) + public function setName(string $name): void { $this->name = $name; } - public function toString() : string + public function toString(): string { return $this->name; } @@ -49,8 +53,7 @@ public function getName($withDataSet = true): string * Count elements of an object * @link http://php.net/manual/en/countable.count.php * @return int The custom count as an integer. - *

- *

+ * * The return value is cast to an integer. * @since 5.1.0 */ @@ -61,25 +64,19 @@ public function count(): int /** * Runs a test and collects its result in a TestResult instance. - * - * @param TestResult|null $result - * - * @return TestResult */ public function run(TestResult $result = null): TestResult { try { call_user_func_array($this->test, $this->example); - } catch (AssertionFailedError $e) { - $result->addFailure(clone($this), $e, $result->time()); + } catch (AssertionFailedError $error) { + $result->addFailure(clone($this), $error, $result->time()); } + return $result; } - /** - * @param mixed $example - */ - public function setExample($example) + public function setExample(array $example): void { $this->example = $example; } @@ -87,7 +84,7 @@ public function setExample($example) /** * @param mixed $throws */ - public function setThrows($throws) + public function setThrows($throws): void { $this->throws = $throws; } diff --git a/tests/ObjectPropertyTest.php b/tests/ObjectPropertyTest.php index c7e07a6..fbfb39e 100644 --- a/tests/ObjectPropertyTest.php +++ b/tests/ObjectPropertyTest.php @@ -6,6 +6,8 @@ final class ObjectPropertyTest extends SpecifyUnitTest { + private ?string $prop = null; + private $private = 'private'; public function testConstruction() diff --git a/tests/SpecifyTest.php b/tests/SpecifyTest.php index 717a2df..d1c7ae2 100644 --- a/tests/SpecifyTest.php +++ b/tests/SpecifyTest.php @@ -14,7 +14,7 @@ class SpecifyTest extends SpecifyUnitTest /** * @specify */ - protected $a; + protected ?TestOne $a = null; /** * @specify @@ -24,7 +24,7 @@ class SpecifyTest extends SpecifyUnitTest /** * not cloned */ - protected $b; + protected ?string $b = null; public function testUserCanChangeName() { @@ -41,13 +41,14 @@ public function testUserCanChangeName() $this->specify('i can fail here but test goes on', function() { $this->markTestIncomplete(); }); - } catch (IncompleteTestError $e) { + } catch (IncompleteTestError $error) { $this->fail("should not be thrown"); } + $this->assertTrue(true); } - function testBeforeCallback() + public function testBeforeCallback() { $this->beforeSpecify(function() { $this->user = "davert"; @@ -57,7 +58,7 @@ function testBeforeCallback() }); } - function testMultiBeforeCallback() + public function testMultiBeforeCallback() { $this->beforeSpecify(function() { $this->user = "davert"; @@ -70,7 +71,7 @@ function testMultiBeforeCallback() }); } - function testAfterCallback() + public function testAfterCallback() { $this->afterSpecify(function() { $this->user = "davert"; @@ -81,7 +82,7 @@ function testAfterCallback() $this->assertEquals('davert', $this->user); } - function testMultiAfterCallback() + public function testMultiAfterCallback() { $this->afterSpecify(function() { $this->user = "davert"; @@ -95,7 +96,7 @@ function testMultiAfterCallback() $this->assertEquals('davertjon', $this->user); } - function testCleanSpecifyCallbacks() + public function testCleanSpecifyCallbacks() { $this->afterSpecify(function() { $this->user = "davert"; @@ -117,7 +118,7 @@ public function testExamples() ]]); } - function testOnlySpecifications() + public function testOnlySpecifications() { $this->specify('should be valid'); $this->assertTrue(true); @@ -205,9 +206,9 @@ public function testCloneOnlySpecified() /** * @Issue https://github.com/Codeception/Specify/issues/6 */ - function testPropertyRestore() + public function testPropertyRestore() { - $this->a = new testOne(); + $this->a = new TestOne(); $this->a->prop = ['hello', 'world']; $this->specify('array contains hello+world', function ($testData) { @@ -223,9 +224,9 @@ function testPropertyRestore() $this->assertTrue($this->getPrivateProperty()); $this->specify('property $private should be restored properly', function() { - $this->private = 'i\'m protected'; - $this->setPrivateProperty('i\'m private'); - $this->assertEquals('i\'m private', $this->getPrivateProperty()); + $this->private = "i'm protected"; + $this->setPrivateProperty("i'm private"); + $this->assertEquals("i'm private", $this->getPrivateProperty()); }); $this->assertFalse($this->private); @@ -310,7 +311,7 @@ public function testMockObjectsIsolation() /** * @dataProvider someData */ - public function testSpecifyAndDataProvider($param) + public function testSpecifyAndDataProvider(int $param) { $this->specify('should assert data provider', function () use ($param) { $this->assertGreaterThan(0, $param); @@ -320,14 +321,14 @@ public function testSpecifyAndDataProvider($param) /** * @dataProvider someData */ - public function testExamplesAndDataProvider($param) + public function testExamplesAndDataProvider(int $param) { $this->specify('should assert data provider', function ($example) use ($param) { $this->assertGreaterThanOrEqual(5, $param + $example); }, ['examples' => [[4], [7], [5]]]); } - public function someData() + public function someData(): array { return [[1], [2]]; } diff --git a/tests/_support/SpecifyUnitTest.php b/tests/_support/SpecifyUnitTest.php index 3a4ff93..060c2a8 100644 --- a/tests/_support/SpecifyUnitTest.php +++ b/tests/_support/SpecifyUnitTest.php @@ -20,6 +20,7 @@ protected function setPrivateProperty($private) { $this->private = $private; } + /** * @return mixed */