-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSingletonTest.php
More file actions
50 lines (38 loc) · 1.25 KB
/
SingletonTest.php
File metadata and controls
50 lines (38 loc) · 1.25 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
<?php
namespace Tests\Traits;
use PHPUnit\Framework\TestCase;
use DesiredPatterns\Traits\Singleton;
class SingletonTest extends TestCase
{
public function testOnlyOneInstanceIsCreated()
{
$instance1 = TestSingleton::getInstance();
$instance2 = TestSingleton::getInstance();
$this->assertSame($instance1, $instance2);
}
public function testInstanceAccessibility()
{
$instance = TestSingleton::getInstance();
$instance->someProperty = 'test value';
$this->assertEquals('test value', $instance->someProperty);
}
public function testCloneThrowsException()
{
$this->expectException(\Error::class);
$this->expectExceptionMessage('Call to private Tests\Traits\TestSingleton::__clone() from scope Tests\Traits\SingletonTest');
$instance = TestSingleton::getInstance();
clone $instance;
}
public function testSerializationThrowsException()
{
$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage('Singleton instance cannot be serialized');
$instance = TestSingleton::getInstance();
serialize($instance);
}
}
class TestSingleton
{
use Singleton;
public $someProperty;
}