From 4d7db3097512b04923bef8d528646cf9413cb366 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Wed, 6 Apr 2022 15:41:19 +1200 Subject: [PATCH 1/2] Add `has` function to allow checking for existence without exception control flow --- Dockerfile | 14 ++++ docker-compose.yml | 10 +++ src/Registry/Registry.php | 22 +++++-- tests/Registry/RegistryTest.php | 112 +++++++++++++++++++++----------- 4 files changed, 114 insertions(+), 44 deletions(-) create mode 100644 Dockerfile create mode 100644 docker-compose.yml diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..c64f9d2 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,14 @@ +FROM composer:2.0 as deps +COPY composer.* /app/ +RUN composer install \ + --ignore-platform-reqs \ + --optimize-autoloader \ + --no-plugins \ + --no-scripts \ + --prefer-dist + +FROM php:8.0.14-alpine3.15 +COPY --from=deps /app/vendor /app/vendor +WORKDIR /app +COPY . . +CMD ["vendor/bin/phpunit", "--configuration", "phpunit.xml"] diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..d62abaa --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,10 @@ +version: '3' + +services: + test: + build: + context: . + volumes: + - ./:/app + working_dir: /app + command: vendor/bin/phpunit --configuration phpunit.xml \ No newline at end of file diff --git a/src/Registry/Registry.php b/src/Registry/Registry.php index 3b4b609..11204e9 100644 --- a/src/Registry/Registry.php +++ b/src/Registry/Registry.php @@ -11,21 +11,21 @@ class Registry * * @var callable[] */ - protected $callbacks = []; + protected array $callbacks = []; /** * List of all fresh resources * * @var array */ - protected $fresh = []; + protected array $fresh = []; /** * List of all connections * * @var array */ - protected $registry = [ + protected array $registry = [ 'default' => [], ]; @@ -34,7 +34,7 @@ class Registry * * @var string */ - protected $context = 'default'; + protected string $context = 'default'; /** * Set a new connection callback @@ -65,19 +65,29 @@ public function set(string $name, callable $callback, bool $fresh = false): self * @return mixed * @throws Exception */ - public function get(string $name, $fresh = false) + public function get(string $name, bool $fresh = false) { if (!\array_key_exists($name, $this->registry[$this->context]) || $fresh || $this->fresh[$name]) { if (!\array_key_exists($name, $this->callbacks)) { throw new Exception('No callback named "' . $name . '" found when trying to create connection'); } - $this->registry[$this->context][$name] = $this->callbacks[$name](); } return $this->registry[$this->context][$name]; } + /** + * Check if connection exists + * + * @param string $name + * @return bool + */ + public function has(string $name): bool + { + return \array_key_exists($name, $this->callbacks); + } + /** * Set the current context */ diff --git a/tests/Registry/RegistryTest.php b/tests/Registry/RegistryTest.php index e9ee7d1..392cdae 100755 --- a/tests/Registry/RegistryTest.php +++ b/tests/Registry/RegistryTest.php @@ -19,10 +19,7 @@ class RegistryTest extends TestCase { - /** - * @var Registry - */ - protected $registry = null; + protected ?Registry $registry = null; public function setUp(): void { @@ -34,65 +31,104 @@ public function tearDown(): void $this->registry = null; } - public function testTest() - { + /** + * @throws \Exception + */ + public function testGet() { $this->registry->set('array', function () { return new ArrayObject(['test']); }); + $this->registry->get('array')[] = 'Hello World'; + $this->assertCount(2, $this->registry->get('array')); + } + + /** + * @throws \Exception + */ + public function testSet() { + $this->registry->set('array', function () { + return new ArrayObject(['test']); + }); $this->assertCount(1, $this->registry->get('array')); + } - $this->registry->get('array')[] = 'Hello World'; + /** + * @throws \Exception + */ + public function testHas() + { + $this->registry->set('item', function () { + return ['test']; + }); + $this->assertTrue($this->registry->has('item')); - $this->assertCount(2, $this->registry->get('array')); + $this->registry->set('item', static fn() => null); + + $this->assertFalse($this->registry->has('item')); + } - // Fresh Copy + /** + * @throws \Exception + */ + public function testGetFresh() { + $this->registry->set('array', function () { + return new ArrayObject(['test']); + }); $this->assertCount(1, $this->registry->get('array', true)); + } - $this->registry->set('time', function () { + /** + * @throws \Exception + */ + public function testSetFresh() { + $this->registry->set('fresh', function () { return microtime(); - }); + }, true); - // Test for different contexts + // Added usleep because some runs were so fast that the microtime was the same + $copy1 = $this->registry->get('fresh'); + usleep(1); + $copy2 = $this->registry->get('fresh'); + usleep(1); + $copy3 = $this->registry->get('fresh'); - $timeX = $this->registry->get('time'); - $timeY = $this->registry->get('time'); + $this->assertNotEquals($copy1, $copy2); + $this->assertNotEquals($copy2, $copy3); + $this->assertNotEquals($copy1, $copy3); + } - $this->assertEquals($timeX, $timeY); - - // Test for cached instance + /** + * @throws \Exception + */ + public function testGetCaching() + { + $this->registry->set('time', function () { + return microtime(); + }); + $timeX = $this->registry->get('time'); $timeY = $this->registry->get('time'); $this->assertEquals($timeX, $timeY); + } - // Switch Context - - $this->registry->context('new'); + /** + * @throws \Exception + */ + public function testContextSwitching() + { + $this->registry->set('time', function () { + return microtime(); + }); + $timeX = $this->registry->get('time'); $timeY = $this->registry->get('time'); - $this->assertNotEquals($timeX, $timeY); - - // Test for cached instance + $this->registry->context('new'); $timeY = $this->registry->get('time'); $this->assertNotEquals($timeX, $timeY); - - - // Test fresh copies - - $this->registry->set('fresh', function () { - return microtime(); - }, true); - - $copy1 = $this->registry->get('fresh'); - $copy2 = $this->registry->get('fresh'); - $copy3 = $this->registry->get('fresh'); - - $this->assertNotEquals($copy1, $copy2); - $this->assertNotEquals($copy2, $copy3); - $this->assertNotEquals($copy1, $copy3); } } \ No newline at end of file From 3cd3c13ba906af820b02dd717d92e2a1bad3eb7b Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Wed, 6 Apr 2022 15:43:58 +1200 Subject: [PATCH 2/2] Remove invalid assert --- tests/Registry/RegistryTest.php | 4 ---- 1 file changed, 4 deletions(-) diff --git a/tests/Registry/RegistryTest.php b/tests/Registry/RegistryTest.php index 392cdae..9fc91e2 100755 --- a/tests/Registry/RegistryTest.php +++ b/tests/Registry/RegistryTest.php @@ -62,10 +62,6 @@ public function testHas() return ['test']; }); $this->assertTrue($this->registry->has('item')); - - $this->registry->set('item', static fn() => null); - - $this->assertFalse($this->registry->has('item')); } /**