-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSingletonTest.php
More file actions
56 lines (47 loc) · 1.27 KB
/
SingletonTest.php
File metadata and controls
56 lines (47 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?php
/**
* Created by PhpStorm.
* User: sockstack
* Date: 2018/11/6
* Time: 23:36.
*/
namespace Sockstack\DesignPattern\Test;
use PHPUnit\Framework\TestCase;
use Sockstack\DesignPattern\Singleton\Sample\Singleton;
class SingletonTest extends TestCase
{
/**
* @expectedException \Error
*/
public function testSingletonByNew()
{
$object = new Singleton();
$this->fail('create fail');
}
/**
* @expectedException \PHPUnit_Framework_Error
* @expectedExceptionMessage 对象不能被复制
*/
public function testSingletonClone()
{
$object = Singleton::getInstance();
$clone_objecdt = clone $object;
$this->fail('clone fail');
}
/**
* @expectedException \PHPUnit_Framework_Error
* @expectedExceptionMessage 对象不能被反序列化
*/
public function testSingletonUnserialize()
{
$object = Singleton::getInstance();
$unserialize_object = unserialize(serialize($object));
$this->fail('unserialize fail');
}
public function testSingleton()
{
$object1 = Singleton::getInstance();
$object2 = Singleton::getInstance();
$this->assertTrue($object1 === $object2, '两个对象不是同一个对象');
}
}