File tree Expand file tree Collapse file tree 3 files changed +82
-0
lines changed
tests/Creational/Singleton Expand file tree Collapse file tree 3 files changed +82
-0
lines changed Original file line number Diff line number Diff line change 14
14
"Growthdev\\ DesignPatterns\\ " : " src/"
15
15
}
16
16
},
17
+ "autoload-dev" : {
18
+ "psr-4" : {
19
+ "Growthdev\\ DesignPatterns\\ Tests\\ " : " tests/"
20
+ }
21
+ },
17
22
"authors" : [
18
23
{
19
24
"name" : " Walmir Silva" ,
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ declare (strict_types=1 );
4
+
5
+ namespace DesignPatterns \Creational \Singleton ;
6
+
7
+ final class Singleton
8
+ {
9
+ private static ?Singleton $ instance = null ;
10
+
11
+ public static function getInstance (): Singleton
12
+ {
13
+ if (null === static ::$ instance ) {
14
+ static ::$ instance = new static ();
15
+ }
16
+
17
+ return static ::$ instance ;
18
+ }
19
+
20
+ protected function __construct ()
21
+ {
22
+ }
23
+
24
+ private function __clone ()
25
+ {
26
+ }
27
+
28
+ private function __wakeup ()
29
+ {
30
+ }
31
+
32
+ private function __sleep ()
33
+ {
34
+ }
35
+ }
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ declare (strict_types=1 );
4
+
5
+ namespace Growthdev \DesignPatterns \Creational \Singleton ;
6
+
7
+ use DesignPatterns \Creational \Singleton \Singleton ;
8
+ use PHPUnit \Framework \TestCase ;
9
+
10
+ final class SingletonTest extends TestCase
11
+ {
12
+ public function testShoudBeTheSameInstanceForTwoObjets (): void
13
+ {
14
+ $ firstInstance = Singleton::getInstance ();
15
+ $ secondInstance = SingleTon::getInstance ();
16
+
17
+ $ this ->assertSame ($ firstInstance , $ secondInstance );
18
+ }
19
+
20
+ public function testThrowErrorWhenTryToCreateInstance (): void
21
+ {
22
+ $ this ->expectException (Error::class);
23
+
24
+ $ instance = new Singleton ();
25
+ }
26
+
27
+ public function testThrowExceptionWhenTryToClone (): void
28
+ {
29
+ $ this ->expectException (Error::class);
30
+
31
+ $ instance = Singleton::getInstance ();
32
+ $ clone = clone $ instance ;
33
+ }
34
+
35
+ public function testThrowExceptionWhenTryToSerialize (): void
36
+ {
37
+ $ this ->expectException (Error::class);
38
+
39
+ $ instance = Singleton::getInstance ();
40
+ $ serialize = serialize ($ instance );
41
+ }
42
+ }
You can’t perform that action at this time.
0 commit comments