Completed
Push — master ( 38025f...2e259d )
by Damian
19s
created

ChangeSetItemTest::testGetForObject()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 20
rs 9.4285
cc 1
eloc 13
nc 1
nop 0
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' => ClassInfo::baseDataClass($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
	function testGetForObject() {
78
		$object = new ChangeSetItemTest_Versioned(['Foo' => 1]);
79
		$object->write();
80
81
		$item = new ChangeSetItem([
82
			'ObjectID' => $object->ID,
83
			'ObjectClass' => ClassInfo::baseDataClass($object)
84
		]);
85
		$item->write();
86
87
		$this->assertEquals(
88
			ChangeSetItemTest_Versioned::get()->byID($object->ID)->toMap(),
89
			ChangeSetItem::get_for_object($object)->first()->Object()->toMap()
0 ignored issues
show
Bug introduced by
The method Object() does not exist on DataObject. Did you maybe mean dbObject()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
90
		);
91
92
		$this->assertEquals(
93
			ChangeSetItemTest_Versioned::get()->byID($object->ID)->toMap(),
94
			ChangeSetItem::get_for_object_by_id($object->ID, $object->ClassName)->first()->Object()->toMap()
0 ignored issues
show
Bug introduced by
The method Object() does not exist on DataObject. Did you maybe mean dbObject()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
95
		);
96
	}
97
}
98