SingletonTest::testInstanceCreation()   A
last analyzed

Complexity

Conditions 2
Paths 5

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 16
rs 9.4285
cc 2
eloc 10
nc 5
nop 1
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 5 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
    namespace CloudFrameworkTest\Patterns;
3
    require_once __DIR__ . '/../../src/autoload.php';
4
5
    class SingletonTest extends \PHPUnit_Framework_TestCase
6
    {
7
        /**
8
         * Test creation of instance
9
         *
10
         * @param string $instanceClass
11
         *
12
         * @return \CloudFramework\Patterns\Singleton
13
         */
14
        public function testInstanceCreation($instanceClass = '\CloudFramework\CloudFrameworkApp')
15
        {
16
            /** @var \CloudFramework\Patterns\Singleton $instanceClass */
17
            /** @var \CloudFramework\Patterns\Singleton $instance */
18
            $instance = NULL;
19
            try {
20
                $instance = $instanceClass::getInstance();
21
                $this->assertNotNull($instance, 'Create an instance of ' . $instanceClass);
22
                $this->assertInstanceOf('\CloudFramework\Patterns\Singleton', $instance, 'CloudFramework have to extend of Singleton');
23
                $this->assertInstanceOf($instanceClass, $instance, 'Created object must be as creation definition class');
24
            } catch (\Exception $e) {
25
                $this->fail('Can not create ' . $instanceClass . ' instance: ' . $e->getMessage());
26
            }
27
28
            return $instance;
29
        }
30
31
        /**
32
         * Test singleton instance
33
         *
34
         * @param string $instanceClass
35
         *
36
         * @return boolean
37
         */
38
        public function checkSingletonInstance($instanceClass = '\CloudFramework\CloudFrameworkApp')
39
        {
40
            $instance1 = $this->testInstanceCreation($instanceClass);
41
            $instance2 = $this->testInstanceCreation($instanceClass);
42
            $this->assertNotNull($instance1);
43
            $instance1->app_name = 'myName';
0 ignored issues
show
Documentation introduced by
The property app_name does not exist on object<CloudFramework\Patterns\Singleton>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
44
            $this->assertNotNull($instance2);
45
            $this->assertEquals($instance1, $instance2);
46
        }
47
48
    }
49