@@ -4,31 +4,31 @@ |
||
4 | 4 | |
5 | 5 | trait Singleton |
6 | 6 | { |
7 | - protected function __construct() |
|
8 | - { |
|
9 | - } |
|
7 | + protected function __construct() |
|
8 | + { |
|
9 | + } |
|
10 | 10 | |
11 | - /** |
|
12 | - * @throws SingletonException |
|
13 | - */ |
|
14 | - final public function __clone() |
|
15 | - { |
|
16 | - throw new SingletonException('You can not clone a singleton.'); |
|
17 | - } |
|
11 | + /** |
|
12 | + * @throws SingletonException |
|
13 | + */ |
|
14 | + final public function __clone() |
|
15 | + { |
|
16 | + throw new SingletonException('You can not clone a singleton.'); |
|
17 | + } |
|
18 | 18 | |
19 | - /** |
|
20 | - * @return static |
|
21 | - */ |
|
22 | - public static function getInstance() |
|
23 | - { |
|
24 | - static $instances; |
|
19 | + /** |
|
20 | + * @return static |
|
21 | + */ |
|
22 | + public static function getInstance() |
|
23 | + { |
|
24 | + static $instances; |
|
25 | 25 | |
26 | - $calledClass = get_called_class(); |
|
26 | + $calledClass = get_called_class(); |
|
27 | 27 | |
28 | - if (!isset($instances[$calledClass])) { |
|
29 | - $instances[$calledClass] = new $calledClass(); |
|
30 | - } |
|
31 | - return $instances[$calledClass]; |
|
32 | - } |
|
28 | + if (!isset($instances[$calledClass])) { |
|
29 | + $instances[$calledClass] = new $calledClass(); |
|
30 | + } |
|
31 | + return $instances[$calledClass]; |
|
32 | + } |
|
33 | 33 | |
34 | 34 | } |
@@ -6,43 +6,43 @@ |
||
6 | 6 | |
7 | 7 | class SingletonTest extends \PHPUnit\Framework\TestCase |
8 | 8 | { |
9 | - public function testSingleton() |
|
10 | - { |
|
11 | - $sample1 = Sample1::getInstance(); |
|
12 | - $this->assertEquals(10, $sample1->property); |
|
13 | - $sample1->property = 20; |
|
14 | - $this->assertEquals(20, $sample1->property); |
|
15 | - |
|
16 | - $sample1Other = Sample1::getInstance(); |
|
17 | - $this->assertEquals(20, $sample1Other->property); |
|
18 | - $sample1->property = 40; |
|
19 | - $this->assertEquals(40, $sample1Other->property); |
|
20 | - $this->assertEquals(40, $sample1->property); |
|
21 | - |
|
22 | - // |
|
23 | - |
|
24 | - $sample2 = Sample2::getInstance(); |
|
25 | - $sample2->property2 = 50; |
|
26 | - $this->assertEquals(50, $sample2->property2); |
|
27 | - |
|
28 | - $sample2Other = Sample2::getInstance(); |
|
29 | - $this->assertEquals(50, $sample2Other->property2); |
|
30 | - $sample2->property2 = 90; |
|
31 | - $this->assertEquals(90, $sample2Other->property2); |
|
32 | - $this->assertEquals(90, $sample2->property2); |
|
33 | - |
|
34 | - // |
|
35 | - |
|
36 | - $this->assertEquals(40, $sample1->property); |
|
37 | - } |
|
38 | - |
|
39 | - /** |
|
40 | - * @expectedException \ByJG\DesignPattern\SingletonException |
|
41 | - */ |
|
42 | - public function testClone() |
|
43 | - { |
|
44 | - $sample1 = Sample1::getInstance(); |
|
45 | - $sample2 = clone $sample1; |
|
46 | - } |
|
9 | + public function testSingleton() |
|
10 | + { |
|
11 | + $sample1 = Sample1::getInstance(); |
|
12 | + $this->assertEquals(10, $sample1->property); |
|
13 | + $sample1->property = 20; |
|
14 | + $this->assertEquals(20, $sample1->property); |
|
15 | + |
|
16 | + $sample1Other = Sample1::getInstance(); |
|
17 | + $this->assertEquals(20, $sample1Other->property); |
|
18 | + $sample1->property = 40; |
|
19 | + $this->assertEquals(40, $sample1Other->property); |
|
20 | + $this->assertEquals(40, $sample1->property); |
|
21 | + |
|
22 | + // |
|
23 | + |
|
24 | + $sample2 = Sample2::getInstance(); |
|
25 | + $sample2->property2 = 50; |
|
26 | + $this->assertEquals(50, $sample2->property2); |
|
27 | + |
|
28 | + $sample2Other = Sample2::getInstance(); |
|
29 | + $this->assertEquals(50, $sample2Other->property2); |
|
30 | + $sample2->property2 = 90; |
|
31 | + $this->assertEquals(90, $sample2Other->property2); |
|
32 | + $this->assertEquals(90, $sample2->property2); |
|
33 | + |
|
34 | + // |
|
35 | + |
|
36 | + $this->assertEquals(40, $sample1->property); |
|
37 | + } |
|
38 | + |
|
39 | + /** |
|
40 | + * @expectedException \ByJG\DesignPattern\SingletonException |
|
41 | + */ |
|
42 | + public function testClone() |
|
43 | + { |
|
44 | + $sample1 = Sample1::getInstance(); |
|
45 | + $sample2 = clone $sample1; |
|
46 | + } |
|
47 | 47 | |
48 | 48 | } |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -require_once __DIR__ . "/../vendor/autoload.php"; |
|
3 | +require_once __DIR__."/../vendor/autoload.php"; |
|
4 | 4 | require_once "Sample1.php"; |
5 | 5 | require_once "Sample2.php"; |
6 | 6 |
@@ -4,12 +4,12 @@ |
||
4 | 4 | |
5 | 5 | class Sample1 |
6 | 6 | { |
7 | - use \ByJG\DesignPattern\Singleton; |
|
7 | + use \ByJG\DesignPattern\Singleton; |
|
8 | 8 | |
9 | - public $property; |
|
9 | + public $property; |
|
10 | 10 | |
11 | - private function __construct() |
|
12 | - { |
|
13 | - $this->property = 10; |
|
14 | - } |
|
11 | + private function __construct() |
|
12 | + { |
|
13 | + $this->property = 10; |
|
14 | + } |
|
15 | 15 | } |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -require_once __DIR__ . "/../vendor/autoload.php"; |
|
3 | +require_once __DIR__."/../vendor/autoload.php"; |
|
4 | 4 | |
5 | 5 | class Sample1 |
6 | 6 | { |
@@ -4,7 +4,7 @@ |
||
4 | 4 | |
5 | 5 | class Sample2 |
6 | 6 | { |
7 | - use \ByJG\DesignPattern\Singleton; |
|
7 | + use \ByJG\DesignPattern\Singleton; |
|
8 | 8 | |
9 | - public $property2; |
|
9 | + public $property2; |
|
10 | 10 | } |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -require_once __DIR__ . "/../vendor/autoload.php"; |
|
3 | +require_once __DIR__."/../vendor/autoload.php"; |
|
4 | 4 | |
5 | 5 | class Sample2 |
6 | 6 | { |