Completed
Push — master ( 2fcc59...a319b9 )
by Joao
02:16
created

SingletonTest::testSingleton()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 29
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 29
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 19
nc 1
nop 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 7 and the first side effect is on line 3.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
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
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
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;
0 ignored issues
show
Unused Code introduced by
$sample2 is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
46
    }
47
48
}
49