Completed
Pull Request — master (#5247)
by Damian
11:17
created

ChangeSetItemTest_Versioned   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 11
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
A canEdit() 0 1 1
1
<?php
2
3
class ChangeSetItemTest_Versioned extends DataObject {
4
	private static $db = [
5
		'Foo' => 'Int'
6
	];
7
8
	private static $extensions = [
9
		"Versioned"
10
	];
11
12
	function canEdit($member = null) { return true; }
13
}
14
15
/**
16
 * @package framework
17
 * @subpackage tests
18
 */
19
class ChangeSetItemTest extends SapphireTest {
20
21
	protected $extraDataObjects = [
22
		'ChangeSetItemTest_Versioned'
23
	];
24
25
	function testChangeType() {
26
		$object = new ChangeSetItemTest_Versioned(['Foo' => 1]);
27
		$object->write();
28
29
		$item = new ChangeSetItem([
30
			'ObjectID' => $object->ID,
31
			'ObjectClass' => $object->ClassName
32
		]);
33
34
		$this->assertEquals(
35
			ChangeSetItem::CHANGE_CREATED, $item->ChangeType,
36
			'New objects that aren\'t yet published should return created'
37
		);
38
39
		$object->publishRecursive();
40
41
		$this->assertEquals(
42
			ChangeSetItem::CHANGE_NONE, $item->ChangeType,
43
			'Objects that have just been published should return no change'
44
		);
45
46
		$object->Foo += 1;
0 ignored issues
show
Documentation introduced by
The property Foo does not exist on object<ChangeSetItemTest_Versioned>. 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...
47
		$object->write();
48
49
		$this->assertEquals(
50
			ChangeSetItem::CHANGE_MODIFIED, $item->ChangeType,
51
			'Object that have unpublished changes written to draft should show as modified'
52
		);
53
54
		$object->publishRecursive();
55
56
		$this->assertEquals(
57
			ChangeSetItem::CHANGE_NONE, $item->ChangeType,
58
			'Objects that have just been published should return no change'
59
		);
60
61
		// We need to use a copy, because ID is set to 0 by delete, causing the following unpublish to fail
62
		$objectCopy = clone $object; $objectCopy->delete();
63
64
		$this->assertEquals(
65
			ChangeSetItem::CHANGE_DELETED, $item->ChangeType,
66
			'Objects that have been deleted from draft (but not yet unpublished) should show as deleted'
67
		);
68
69
		$object->doUnpublish();
70
71
		$this->assertEquals(
72
			ChangeSetItem::CHANGE_NONE, $item->ChangeType,
73
			'Objects that have been deleted and then unpublished should return no change'
74
		);
75
	}
76
}
77