Skip to content

Commit a319b9e

Browse files
committed
Added Unit Test
1 parent 2fcc59b commit a319b9e

File tree

6 files changed

+105
-17
lines changed

6 files changed

+105
-17
lines changed

.travis.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
language: php
2+
php:
3+
- "7.2"
4+
- "7.1"
5+
- "7.0"
6+
- "5.6"
7+
8+
install:
9+
- composer install
10+
11+
script:
12+
- vendor/bin/phpunit
13+

composer.json

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
{
2-
"name": "byjg/singleton-pattern",
3-
"version": "1.0.1",
4-
"description": "A lightweight PHP implementation of the Design Pattern Singleton using trait.",
5-
"authors": [
6-
{
7-
"name": "João Gilberto Magalhães",
8-
"email": "joao@byjg.com.br"
9-
}
10-
],
11-
"autoload": {
12-
"psr-4": {
13-
"ByJG\\DesignPattern\\": "src/"
14-
}
15-
},
16-
"require": {},
17-
"license": "MIT"
2+
"name": "byjg/singleton-pattern",
3+
"description": "A lightweight PHP implementation of the Design Pattern Singleton using trait.",
4+
"authors": [
5+
{
6+
"name": "João Gilberto Magalhães",
7+
"email": "joao@byjg.com.br"
8+
}
9+
],
10+
"autoload": {
11+
"psr-4": {
12+
"ByJG\\DesignPattern\\": "src/"
13+
}
14+
},
15+
"require": {},
16+
"require-dev": {
17+
"phpunit/phpunit": ">5.7"
18+
},
19+
"license": "MIT"
1820
}

src/Singleton.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ protected function __construct()
1111
/**
1212
* @throws SingletonException
1313
*/
14-
final private function __clone()
14+
final public function __clone()
1515
{
1616
throw new SingletonException('You can not clone a singleton.');
1717
}

tests/Sample1.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
require_once __DIR__ . "/../vendor/autoload.php";
4+
5+
class Sample1
6+
{
7+
use \ByJG\DesignPattern\Singleton;
8+
9+
public $property;
10+
11+
private function __construct()
12+
{
13+
$this->property = 10;
14+
}
15+
}

tests/Sample2.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
require_once __DIR__ . "/../vendor/autoload.php";
4+
5+
class Sample2
6+
{
7+
use \ByJG\DesignPattern\Singleton;
8+
9+
public $property2;
10+
}

tests/SingletonTest.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
}

0 commit comments

Comments
 (0)