|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
require_once __DIR__ . "/../vendor/autoload.php"; |
|
4
|
|
|
require_once "Sample1.php"; |
|
5
|
|
|
require_once "Sample2.php"; |
|
6
|
|
|
|
|
7
|
|
|
class SingletonTest extends \PHPUnit\Framework\TestCase |
|
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
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @expectedException \ByJG\DesignPattern\SingletonException |
|
50
|
|
|
*/ |
|
51
|
|
|
public function testSerialize() |
|
52
|
|
|
{ |
|
53
|
|
|
$sample1 = Sample1::getInstance(); |
|
54
|
|
|
$serialize = serialize($sample1); |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|