Skip to content

Commit 9c18331

Browse files
authored
Merge pull request #7 from utopia-php/feat-allow-querying
Add `has` function to allow checking for existence without throwing
2 parents ade9de2 + 3cd3c13 commit 9c18331

4 files changed

Lines changed: 111 additions & 45 deletions

File tree

Dockerfile

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
FROM composer:2.0 as deps
2+
COPY composer.* /app/
3+
RUN composer install \
4+
--ignore-platform-reqs \
5+
--optimize-autoloader \
6+
--no-plugins \
7+
--no-scripts \
8+
--prefer-dist
9+
10+
FROM php:8.0.14-alpine3.15
11+
COPY --from=deps /app/vendor /app/vendor
12+
WORKDIR /app
13+
COPY . .
14+
CMD ["vendor/bin/phpunit", "--configuration", "phpunit.xml"]

docker-compose.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
version: '3'
2+
3+
services:
4+
test:
5+
build:
6+
context: .
7+
volumes:
8+
- ./:/app
9+
working_dir: /app
10+
command: vendor/bin/phpunit --configuration phpunit.xml

src/Registry/Registry.php

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,21 @@ class Registry
1111
*
1212
* @var callable[]
1313
*/
14-
protected $callbacks = [];
14+
protected array $callbacks = [];
1515

1616
/**
1717
* List of all fresh resources
1818
*
1919
* @var array
2020
*/
21-
protected $fresh = [];
21+
protected array $fresh = [];
2222

2323
/**
2424
* List of all connections
2525
*
2626
* @var array
2727
*/
28-
protected $registry = [
28+
protected array $registry = [
2929
'default' => [],
3030
];
3131

@@ -34,7 +34,7 @@ class Registry
3434
*
3535
* @var string
3636
*/
37-
protected $context = 'default';
37+
protected string $context = 'default';
3838

3939
/**
4040
* Set a new connection callback
@@ -65,19 +65,29 @@ public function set(string $name, callable $callback, bool $fresh = false): self
6565
* @return mixed
6666
* @throws Exception
6767
*/
68-
public function get(string $name, $fresh = false)
68+
public function get(string $name, bool $fresh = false)
6969
{
7070
if (!\array_key_exists($name, $this->registry[$this->context]) || $fresh || $this->fresh[$name]) {
7171
if (!\array_key_exists($name, $this->callbacks)) {
7272
throw new Exception('No callback named "' . $name . '" found when trying to create connection');
7373
}
74-
7574
$this->registry[$this->context][$name] = $this->callbacks[$name]();
7675
}
7776

7877
return $this->registry[$this->context][$name];
7978
}
8079

80+
/**
81+
* Check if connection exists
82+
*
83+
* @param string $name
84+
* @return bool
85+
*/
86+
public function has(string $name): bool
87+
{
88+
return \array_key_exists($name, $this->callbacks);
89+
}
90+
8191
/**
8292
* Set the current context
8393
*/

tests/Registry/RegistryTest.php

Lines changed: 71 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,7 @@
1919

2020
class RegistryTest extends TestCase
2121
{
22-
/**
23-
* @var Registry
24-
*/
25-
protected $registry = null;
22+
protected ?Registry $registry = null;
2623

2724
public function setUp(): void
2825
{
@@ -34,65 +31,100 @@ public function tearDown(): void
3431
$this->registry = null;
3532
}
3633

37-
public function testTest()
38-
{
34+
/**
35+
* @throws \Exception
36+
*/
37+
public function testGet() {
3938
$this->registry->set('array', function () {
4039
return new ArrayObject(['test']);
4140
});
42-
43-
$this->assertCount(1, $this->registry->get('array'));
44-
4541
$this->registry->get('array')[] = 'Hello World';
4642

4743
$this->assertCount(2, $this->registry->get('array'));
44+
}
45+
46+
/**
47+
* @throws \Exception
48+
*/
49+
public function testSet() {
50+
$this->registry->set('array', function () {
51+
return new ArrayObject(['test']);
52+
});
53+
$this->assertCount(1, $this->registry->get('array'));
54+
}
55+
56+
/**
57+
* @throws \Exception
58+
*/
59+
public function testHas()
60+
{
61+
$this->registry->set('item', function () {
62+
return ['test'];
63+
});
64+
$this->assertTrue($this->registry->has('item'));
65+
}
4866

49-
// Fresh Copy
67+
/**
68+
* @throws \Exception
69+
*/
70+
public function testGetFresh() {
71+
$this->registry->set('array', function () {
72+
return new ArrayObject(['test']);
73+
});
5074
$this->assertCount(1, $this->registry->get('array', true));
75+
}
5176

52-
$this->registry->set('time', function () {
77+
/**
78+
* @throws \Exception
79+
*/
80+
public function testSetFresh() {
81+
$this->registry->set('fresh', function () {
5382
return microtime();
54-
});
83+
}, true);
5584

56-
// Test for different contexts
85+
// Added usleep because some runs were so fast that the microtime was the same
86+
$copy1 = $this->registry->get('fresh');
87+
usleep(1);
88+
$copy2 = $this->registry->get('fresh');
89+
usleep(1);
90+
$copy3 = $this->registry->get('fresh');
5791

58-
$timeX = $this->registry->get('time');
59-
$timeY = $this->registry->get('time');
92+
$this->assertNotEquals($copy1, $copy2);
93+
$this->assertNotEquals($copy2, $copy3);
94+
$this->assertNotEquals($copy1, $copy3);
95+
}
6096

61-
$this->assertEquals($timeX, $timeY);
62-
63-
// Test for cached instance
97+
/**
98+
* @throws \Exception
99+
*/
100+
public function testGetCaching()
101+
{
102+
$this->registry->set('time', function () {
103+
return microtime();
104+
});
64105

106+
$timeX = $this->registry->get('time');
65107
$timeY = $this->registry->get('time');
66108

67109
$this->assertEquals($timeX, $timeY);
110+
}
68111

69-
// Switch Context
70-
71-
$this->registry->context('new');
112+
/**
113+
* @throws \Exception
114+
*/
115+
public function testContextSwitching()
116+
{
117+
$this->registry->set('time', function () {
118+
return microtime();
119+
});
72120

121+
$timeX = $this->registry->get('time');
73122
$timeY = $this->registry->get('time');
74123

75-
$this->assertNotEquals($timeX, $timeY);
76-
77-
// Test for cached instance
124+
$this->registry->context('new');
78125

79126
$timeY = $this->registry->get('time');
80127

81128
$this->assertNotEquals($timeX, $timeY);
82-
83-
84-
// Test fresh copies
85-
86-
$this->registry->set('fresh', function () {
87-
return microtime();
88-
}, true);
89-
90-
$copy1 = $this->registry->get('fresh');
91-
$copy2 = $this->registry->get('fresh');
92-
$copy3 = $this->registry->get('fresh');
93-
94-
$this->assertNotEquals($copy1, $copy2);
95-
$this->assertNotEquals($copy2, $copy3);
96-
$this->assertNotEquals($copy1, $copy3);
97129
}
98130
}

0 commit comments

Comments
 (0)