Completed
Push — master ( 86c958...d453af )
by Simon
02:17
created

ShortlistTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 6
Bugs 0 Features 1
Metric Value
wmc 4
c 6
b 0
f 1
lcom 1
cbo 6
dl 0
loc 66
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A testShortlistCreation() 0 8 1
B testAddItemtoShortList() 0 27 1
A testAddPageToShortlist() 0 11 1
A testRemovePageFromShortlist() 0 12 1
1
<?php
2
3
class ShortlistTest extends FunctionalTest
4
{
5
    protected static $fixture_file = 'fixtures.yml';
6
7
	public function testShortlistCreation()
8
	{
9
        $shortlists = ShortList::get();
10
11
        $this->get('shortlist');
12
13
        $this->assertNotEquals($shortlists->Count(), 0);
1 ignored issue
show
Bug introduced by
The method assertNotEquals() does not seem to exist on object<ShortlistTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
14
    }
15
16
    public function testAddItemtoShortList()
17
    {
18
        $testpage = $this->objFromFixture('Page', 'page1');
19
        Session::start();
20
        $sessionID = session_id();
0 ignored issues
show
Unused Code introduced by
$sessionID 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...
21
22
        // create the shortlist
23
        $shortlist = new ShortList();
24
        $shortlist->write();
25
26
        $this->assertEquals($shortlist->ShortListItems()->Count(), 0);
2 ignored issues
show
Documentation Bug introduced by
The method ShortListItems does not exist on object<ShortList>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
Bug introduced by
The method assertEquals() does not seem to exist on object<ShortlistTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
27
28
        // create the item.
29
        $item = new ShortListItem();
30
        $item->ItemType = $testpage->ClassName;
1 ignored issue
show
Documentation introduced by
The property ItemType does not exist on object<ShortListItem>. 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...
31
        $item->ItemID = $testpage->ID;
0 ignored issues
show
Documentation introduced by
The property ItemID does not exist on object<ShortListItem>. 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...
32
        $item->write();
33
34
        // add to the shortlist.
35
        $shortlist->ShortListItems()->add($item);
1 ignored issue
show
Documentation Bug introduced by
The method ShortListItems does not exist on object<ShortList>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
36
        $shortlist->write();
37
38
        // do we have an item added?
39
        $this->assertNotEquals($shortlist->ShortListItems()->Count(), 0);
2 ignored issues
show
Documentation Bug introduced by
The method ShortListItems does not exist on object<ShortList>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
Bug introduced by
The method assertNotEquals() does not seem to exist on object<ShortlistTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
40
        // what about the reverse side?
41
        $this->assertNotEquals($item->ShortList()->ID, 0);
2 ignored issues
show
Documentation Bug introduced by
The method ShortList does not exist on object<ShortListItem>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
Bug introduced by
The method assertNotEquals() does not seem to exist on object<ShortlistTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
42
    }
43
44
    public function testAddPageToShortlist() {
45
        $testpage = $this->objFromFixture('Page', 'page1');
46
        Session::start();
47
        $sessionID = session_id();
48
49
        $response = $this->get('shortlist/add?id=' . $testpage->ID . '&type=Page&s=' . $sessionID);
0 ignored issues
show
Unused Code introduced by
$response 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...
50
51
        $shortlist = DataObject::get_one('ShortList', array('SessionID' => $sessionID));
52
53
        $this->assertNotEquals($shortlist->ShortListItems()->Count(), 0);
1 ignored issue
show
Bug introduced by
The method assertNotEquals() does not seem to exist on object<ShortlistTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
54
    }
55
56
    public function testRemovePageFromShortlist() {
57
        $shortlist = $this->objFromFixture('ShortList', 'test');
0 ignored issues
show
Unused Code introduced by
$shortlist 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...
58
        $testpage = $this->objFromFixture('Page', 'page1');
59
60
        $this->get('shortlist/add?id=' . $testpage->ID . '&type=Page&s=' . session_id() . '&output=0');
61
62
        $shortlist = DataObject::get_one('ShortList', array('SessionID' => session_id()));
63
64
        $this->get('shortlist/remove?id=' . $testpage->ID . '&type=Page&s=' . session_id() . '&output=0');
65
66
        $this->assertEquals($shortlist->ShortListItems()->Count(), 0);
1 ignored issue
show
Bug introduced by
The method assertEquals() does not seem to exist on object<ShortlistTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
67
    }
68
}
69