From 95740436ff69986f3d8520b7c95ea6d8255a91e2 Mon Sep 17 00:00:00 2001 From: Anton Shevchuk Date: Tue, 10 Sep 2024 12:39:00 +0200 Subject: [PATCH] Migrate to PHP 8.2 Migrate to PHPUnit 11 Migrate to usage trait in trait (to simplify usage) --- .scrutinizer.yml | 2 +- composer.json | 4 +- src/Container/ArrayAccess.php | 7 +- src/Container/Container.php | 4 +- src/Container/JsonSerialize.php | 4 +- src/Container/MagicAccess.php | 7 +- src/Container/RegularAccess.php | 7 +- tests/Container/ArrayContainerTest.php | 3 +- tests/Container/ComplexContainerTest.php | 87 +++++++++++++++++++ tests/Container/Fixtures/ArrayContainer.php | 7 +- ...reteContainer.php => ComplexContainer.php} | 9 +- tests/Container/Fixtures/JsonContainer.php | 26 ++++++ tests/Container/Fixtures/MagicContainer.php | 7 +- tests/Container/Fixtures/RegularContainer.php | 7 +- tests/Container/Fixtures/SimpleContainer.php | 26 ++++++ tests/Container/JsonContainerTest.php | 50 +++++++++++ tests/Container/MagicContainerTest.php | 3 +- tests/Container/RegularContainerTest.php | 4 +- ...tainerTest.php => SimpleContainerTest.php} | 27 ++---- tests/_bootstrap.php | 2 +- 20 files changed, 224 insertions(+), 69 deletions(-) create mode 100644 tests/Container/ComplexContainerTest.php rename tests/Container/Fixtures/{ConcreteContainer.php => ComplexContainer.php} (70%) create mode 100644 tests/Container/Fixtures/JsonContainer.php create mode 100644 tests/Container/Fixtures/SimpleContainer.php create mode 100644 tests/Container/JsonContainerTest.php rename tests/Container/{ContainerTest.php => SimpleContainerTest.php} (64%) diff --git a/.scrutinizer.yml b/.scrutinizer.yml index 496d12b..0c65618 100644 --- a/.scrutinizer.yml +++ b/.scrutinizer.yml @@ -1,6 +1,6 @@ build: environment: - php: 8.1 + php: 8.2 nodes: analysis: tests: diff --git a/composer.json b/composer.json index 52d9700..9c27b00 100644 --- a/composer.json +++ b/composer.json @@ -3,10 +3,10 @@ "description": "Container package", "type": "library", "require": { - "php": ">=8.1" + "php": ">=8.2" }, "require-dev": { - "phpunit/phpunit": "~10.4" + "phpunit/phpunit": "~11.3" }, "autoload": { "psr-4": { diff --git a/src/Container/ArrayAccess.php b/src/Container/ArrayAccess.php index 0e82ea4..1b5ae82 100644 --- a/src/Container/ArrayAccess.php +++ b/src/Container/ArrayAccess.php @@ -19,14 +19,11 @@ * @package Bluz\Common * @author Anton Shevchuk * @see ArrayAccess - * - * @method void doSetContainer(string $key, mixed $value) - * @method mixed doGetContainer(string $key) - * @method bool doContainsContainer(string $key) - * @method void doDeleteContainer(string $key) */ trait ArrayAccess { + use Container; + /** * Offset to set * diff --git a/src/Container/Container.php b/src/Container/Container.php index 3e68c6c..f6987b5 100644 --- a/src/Container/Container.php +++ b/src/Container/Container.php @@ -12,7 +12,7 @@ namespace Bluz\Container; /** - * Container of data for object + * Container of data for an object * * @package Bluz\Common * @author Anton Shevchuk @@ -102,7 +102,7 @@ public function toArray(): array } /** - * Reset container array + * Reset the container array * * @return void */ diff --git a/src/Container/JsonSerialize.php b/src/Container/JsonSerialize.php index 070f1b6..cecfbae 100644 --- a/src/Container/JsonSerialize.php +++ b/src/Container/JsonSerialize.php @@ -17,11 +17,11 @@ * @package Bluz\Common * @author Anton Shevchuk * @see JsonSerializable - * - * @method array toArray() */ trait JsonSerialize { + use Container; + /** * Specify data which should be serialized to JSON * diff --git a/src/Container/MagicAccess.php b/src/Container/MagicAccess.php index a44e05c..88d0393 100644 --- a/src/Container/MagicAccess.php +++ b/src/Container/MagicAccess.php @@ -16,14 +16,11 @@ * * @package Bluz\Common * @author Anton Shevchuk - * - * @method void doSetContainer(string $key, mixed $value) - * @method mixed doGetContainer(string $key) - * @method bool doContainsContainer(string $key) - * @method void doDeleteContainer(string $key) */ trait MagicAccess { + use Container; + /** * Magic alias for set() regular method * diff --git a/src/Container/RegularAccess.php b/src/Container/RegularAccess.php index 1c20548..de25d78 100644 --- a/src/Container/RegularAccess.php +++ b/src/Container/RegularAccess.php @@ -16,14 +16,11 @@ * * @package Bluz\Common * @author Anton Shevchuk - * - * @method void doSetContainer(string $key, mixed $value) - * @method mixed doGetContainer(string $key) - * @method bool doContainsContainer(string $key) - * @method void doDeleteContainer(string $key) */ trait RegularAccess { + use Container; + /** * Set key/value pair * diff --git a/tests/Container/ArrayContainerTest.php b/tests/Container/ArrayContainerTest.php index f455291..613bfe6 100644 --- a/tests/Container/ArrayContainerTest.php +++ b/tests/Container/ArrayContainerTest.php @@ -9,10 +9,9 @@ use Bluz\Tests\Container\Fixtures\ArrayContainer; use PHPUnit\Framework\TestCase; -use Bluz\Tests\Container\Fixtures\ConcreteContainer; /** - * Tests for Container traits + * Tests for ArrayContainer * * @package Bluz\Tests\Common * diff --git a/tests/Container/ComplexContainerTest.php b/tests/Container/ComplexContainerTest.php new file mode 100644 index 0000000..7dd72e4 --- /dev/null +++ b/tests/Container/ComplexContainerTest.php @@ -0,0 +1,87 @@ + 'bar', + 'quz' => 'qux' + ]; + + /** + * Sets up the fixture, for example, opens a network connection. + * This method is called before a test is executed. + */ + protected function setUp(): void + { + $this->class = new ComplexContainer(); + } + + /** + * Test ArrayAccess trait + */ + public function testArrayAccess() + { + $this->class['foo'] = 'bar'; + $this->class['quz'] = 'qux'; + + unset($this->class['quz']); + + self::assertEquals('bar', $this->class['foo']); + self::assertFalse(isset($this->class['quz'])); + self::assertFalse(isset($this->class['some'])); + self::assertEmpty($this->class['quz']); + self::assertEmpty($this->class['some']); + self::assertNull($this->class['quz']); + } + + /** + * Test MagicAccess trait + */ + public function testMagicAccess() + { + $this->class->foo = 'bar'; + $this->class->quz = 'qux'; + + unset($this->class->quz); + + self::assertEquals('bar', $this->class->foo); + self::assertFalse(isset($this->class->quz)); + self::assertFalse(isset($this->class->some)); + self::assertEmpty($this->class->quz); + self::assertEmpty($this->class->some); + self::assertNull($this->class->quz); + } + + /** + * Test JsonSerialize implementation + */ + public function testJsonSerialize() + { + $this->class->setFromArray($this->example); + + self::assertJsonStringEqualsJsonString(json_encode($this->example), json_encode($this->class)); + } +} diff --git a/tests/Container/Fixtures/ArrayContainer.php b/tests/Container/Fixtures/ArrayContainer.php index 2678523..bf5c30c 100644 --- a/tests/Container/Fixtures/ArrayContainer.php +++ b/tests/Container/Fixtures/ArrayContainer.php @@ -12,13 +12,9 @@ namespace Bluz\Tests\Container\Fixtures; use Bluz\Container\ArrayAccess; -use Bluz\Container\Container; -use Bluz\Container\JsonSerialize; -use Bluz\Container\MagicAccess; -use Bluz\Container\RegularAccess; /** - * Concrete class with Container trait + * Concrete class with ArrayAccess trait * * @package Bluz\Tests\Common * @@ -26,6 +22,5 @@ */ class ArrayContainer implements \ArrayAccess { - use Container; use ArrayAccess; } diff --git a/tests/Container/Fixtures/ConcreteContainer.php b/tests/Container/Fixtures/ComplexContainer.php similarity index 70% rename from tests/Container/Fixtures/ConcreteContainer.php rename to tests/Container/Fixtures/ComplexContainer.php index 9211a0e..005ec1c 100644 --- a/tests/Container/Fixtures/ConcreteContainer.php +++ b/tests/Container/Fixtures/ComplexContainer.php @@ -12,20 +12,21 @@ namespace Bluz\Tests\Container\Fixtures; use Bluz\Container\ArrayAccess; -use Bluz\Container\Container; use Bluz\Container\JsonSerialize; use Bluz\Container\MagicAccess; use Bluz\Container\RegularAccess; /** - * Concrete class with Container trait + * Concrete class with all traits * * @package Bluz\Tests\Common * * @author Anton Shevchuk */ -class ConcreteContainer implements \JsonSerializable +class ComplexContainer implements \ArrayAccess, \JsonSerializable { - use Container; + use ArrayAccess; use JsonSerialize; + use MagicAccess; + use RegularAccess; } diff --git a/tests/Container/Fixtures/JsonContainer.php b/tests/Container/Fixtures/JsonContainer.php new file mode 100644 index 0000000..0f27d13 --- /dev/null +++ b/tests/Container/Fixtures/JsonContainer.php @@ -0,0 +1,26 @@ + 'bar', + 'quz' => 'qux' + ]; + + /** + * Sets up the fixture, for example, opens a network connection. + * This method is called before a test is executed. + */ + protected function setUp(): void + { + $this->class = new JsonContainer(); + } + + /** + * Test JsonSerialize implementation + */ + public function testJsonSerialize() + { + $this->class->setFromArray($this->example); + + self::assertJsonStringEqualsJsonString(json_encode($this->example), json_encode($this->class)); + } +} diff --git a/tests/Container/MagicContainerTest.php b/tests/Container/MagicContainerTest.php index a9d3683..504af11 100644 --- a/tests/Container/MagicContainerTest.php +++ b/tests/Container/MagicContainerTest.php @@ -9,10 +9,9 @@ use Bluz\Tests\Container\Fixtures\MagicContainer; use PHPUnit\Framework\TestCase; -use Bluz\Tests\Container\Fixtures\ConcreteContainer; /** - * Tests for Container traits + * Tests for MagicContainer * * @package Bluz\Tests\Common * diff --git a/tests/Container/RegularContainerTest.php b/tests/Container/RegularContainerTest.php index 2ba2942..f3c44c1 100644 --- a/tests/Container/RegularContainerTest.php +++ b/tests/Container/RegularContainerTest.php @@ -9,10 +9,10 @@ use Bluz\Tests\Container\Fixtures\RegularContainer; use PHPUnit\Framework\TestCase; -use Bluz\Tests\Container\Fixtures\ConcreteContainer; +use Bluz\Tests\Container\Fixtures\SimpleContainer; /** - * Tests for Container traits + * Tests for RegularContainer * * @package Bluz\Tests\Common * diff --git a/tests/Container/ContainerTest.php b/tests/Container/SimpleContainerTest.php similarity index 64% rename from tests/Container/ContainerTest.php rename to tests/Container/SimpleContainerTest.php index bf6d07e..8d6528d 100644 --- a/tests/Container/ContainerTest.php +++ b/tests/Container/SimpleContainerTest.php @@ -8,21 +8,21 @@ namespace Bluz\Tests\Container; use PHPUnit\Framework\TestCase; -use Bluz\Tests\Container\Fixtures\ConcreteContainer; +use Bluz\Tests\Container\Fixtures\SimpleContainer; /** - * Tests for Container traits + * Tests for SimpleContainer * * @package Bluz\Tests\Common * * @author Anton Shevchuk */ -class ContainerTest extends TestCase +class SimpleContainerTest extends TestCase { /** - * @var ConcreteContainer + * @var SimpleContainer */ - protected ConcreteContainer $class; + protected SimpleContainer $class; protected array $example = [ 'foo' => 'bar', @@ -35,11 +35,11 @@ class ContainerTest extends TestCase */ protected function setUp(): void { - $this->class = new ConcreteContainer(); + $this->class = new SimpleContainer(); } /** - * Test setup Container from array and get as array + * Test setup SimpleContainer from the array and get as the array */ public function testSetFromArray() { @@ -49,11 +49,12 @@ public function testSetFromArray() } /** - * Test Reset Container data to null + * Test Reset SimpleContainer data to null */ public function testResetContainer() { $this->class->setFromArray($this->example); + $this->class->resetArray(); $result = $this->class->toArray(); @@ -63,14 +64,4 @@ public function testResetContainer() self::assertNull($result['foo']); self::assertNull($result['quz']); } - - /** - * Test JsonSerialize implementation - */ - public function testJsonSerialize() - { - $this->class->setFromArray($this->example); - - self::assertJsonStringEqualsJsonString(json_encode($this->example), json_encode($this->class)); - } } diff --git a/tests/_bootstrap.php b/tests/_bootstrap.php index 44cc33e..1b32193 100644 --- a/tests/_bootstrap.php +++ b/tests/_bootstrap.php @@ -1,6 +1,6 @@