Skip to content

Commit b0bfe90

Browse files
authored
Merge pull request #38 from davidyell/endpoint-registry-flush
Resolves #37
2 parents 1f4af91 + 7c4d5c3 commit b0bfe90

File tree

2 files changed

+117
-0
lines changed

2 files changed

+117
-0
lines changed

src/Model/EndpointRegistry.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,31 @@ class EndpointRegistry
2323
*/
2424
protected static $_options = [];
2525

26+
/**
27+
* Clear the endpoint registry of all instances
28+
*
29+
* @return void
30+
*/
31+
public static function clear()
32+
{
33+
self::$_instances = [];
34+
self::$_options = [];
35+
}
36+
37+
/**
38+
* Remove a specific endpoint instance from the registry by alias
39+
*
40+
* @param string $alias String alias of the endpoint
41+
* @return void
42+
*/
43+
public static function remove($alias)
44+
{
45+
unset(
46+
self::$_instances[$alias],
47+
self::$_options[$alias]
48+
);
49+
}
50+
2651
/**
2752
* Get a endpoint instance from the registry.
2853
*
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?php
2+
3+
namespace Muffin\Webservice\Model;
4+
5+
use Cake\TestSuite\TestCase;
6+
use Muffin\Webservice\Connection;
7+
8+
class EndpointRegistryTest extends TestCase
9+
{
10+
public function tearDown()
11+
{
12+
EndpointRegistry::clear();
13+
}
14+
15+
/**
16+
* Test to ensure that an endpoint can be added/retrieved from the registry
17+
*
18+
* @return void
19+
*/
20+
public function testAddingEndpoint()
21+
{
22+
$result = EndpointRegistry::get('Test', [
23+
'connection' => new Connection([
24+
'name' => 'test',
25+
'service' => 'test'
26+
])
27+
]);
28+
29+
$this->assertInstanceOf(\Muffin\Webservice\Model\Endpoint::class, $result);
30+
}
31+
32+
/**
33+
* Ensure that if you try and set the options for an already configured Endpoint instance an
34+
* exception is thrown
35+
*
36+
* @expectedException \RuntimeException
37+
*/
38+
public function testAddingSameEndpoint()
39+
{
40+
$result = EndpointRegistry::get('Test', [
41+
'connection' => new Connection([
42+
'name' => 'test',
43+
'service' => 'test'
44+
])
45+
]);
46+
47+
$this->assertInstanceOf(\Muffin\Webservice\Model\Endpoint::class, $result);
48+
49+
$result = EndpointRegistry::get('Test', [
50+
'connection' => new Connection([
51+
'name' => 'exception',
52+
'service' => 'exception'
53+
])
54+
]);
55+
}
56+
57+
public function testRemovingInstance()
58+
{
59+
$result = EndpointRegistry::get('Test', [
60+
'connection' => new Connection([
61+
'name' => 'test',
62+
'service' => 'test'
63+
])
64+
]);
65+
66+
$this->assertInstanceOf(\Muffin\Webservice\Model\Endpoint::class, $result);
67+
68+
EndpointRegistry::remove('Test');
69+
70+
$ref = new \ReflectionClass(EndpointRegistry::class);
71+
$this->assertEmpty($ref->getStaticProperties()['_instances']);
72+
$this->assertEmpty($ref->getStaticProperties()['_options']);
73+
}
74+
75+
public function testClearing()
76+
{
77+
$result = EndpointRegistry::get('Test', [
78+
'connection' => new Connection([
79+
'name' => 'test',
80+
'service' => 'test'
81+
])
82+
]);
83+
84+
$this->assertInstanceOf(\Muffin\Webservice\Model\Endpoint::class, $result);
85+
86+
EndpointRegistry::clear();
87+
88+
$ref = new \ReflectionClass(EndpointRegistry::class);
89+
$this->assertEmpty($ref->getStaticProperties()['_instances']);
90+
$this->assertEmpty($ref->getStaticProperties()['_options']);
91+
}
92+
}

0 commit comments

Comments
 (0)