1919
2020class 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