Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -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"]
10 changes: 10 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
version: '3'

services:
test:
build:
context: .
volumes:
- ./:/app
working_dir: /app
command: vendor/bin/phpunit --configuration phpunit.xml
22 changes: 16 additions & 6 deletions src/Registry/Registry.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => [],
];

Expand All @@ -34,7 +34,7 @@ class Registry
*
* @var string
*/
protected $context = 'default';
protected string $context = 'default';

/**
* Set a new connection callback
Expand Down Expand Up @@ -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
*/
Expand Down
110 changes: 71 additions & 39 deletions tests/Registry/RegistryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@

class RegistryTest extends TestCase
{
/**
* @var Registry
*/
protected $registry = null;
protected ?Registry $registry = null;

public function setUp(): void
{
Expand All @@ -34,65 +31,100 @@ 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->assertCount(1, $this->registry->get('array'));

$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'));
}

/**
* @throws \Exception
*/
public function testHas()
{
$this->registry->set('item', function () {
return ['test'];
});
$this->assertTrue($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);
}
}