Completed
Push — master ( ebbf9a...c78942 )
by Damian
35:58
created

UserFormsVersionedTest::testPublishing()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 35
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 35
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 20
nc 1
nop 0
1
<?php
2
3
4
class UserFormsVersionedTest extends SapphireTest
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...
5
{
6
    protected static $fixture_file = 'UserDefinedFormTest.yml';
7
8
    public function setUp()
9
    {
10
        parent::setUp();
11
        Versioned::reading_stage('Stage');
12
    }
13
14
    public function testPublishing()
15
    {
16
        /** @var UserDefinedForm $form */
17
        $form = $this->objFromFixture('UserDefinedForm', 'filtered-form-page');
18
19
        // Get id of options
20
        $optionID = $this->idFromFixture('EditableOption', 'option-3');
21
        $this->assertEmpty(Versioned::get_one_by_stage('EditableOption', 'Live', array('"ID" = ?' => $optionID)));
0 ignored issues
show
Documentation introduced by
array('"ID" = ?' => $optionID) is of type array<string,integer,{"\"ID\" = ?":"integer"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Bug introduced by
The method assertEmpty() does not seem to exist on object<UserFormsVersionedTest>.

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...
22
23
        // Publishing writes this to live
24
        $form->doPublish();
25
        $liveVersion = Versioned::get_versionnumber_by_stage('EditableOption', 'Live', $optionID, false);
26
        $this->assertNotEmpty($liveVersion);
0 ignored issues
show
Bug introduced by
The method assertNotEmpty() does not seem to exist on object<UserFormsVersionedTest>.

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
        // Add new option, and repeat publish process
29
        /** @var EditableCheckboxGroupField $list */
30
        $list = $this->objFromFixture('EditableCheckboxGroupField', 'checkbox-group');
31
        $newOption = new EditableOption();
32
        $newOption->Title = 'New option';
0 ignored issues
show
Documentation introduced by
The property Title does not exist on object<EditableOption>. 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...
33
        $newOption->Value = 'ok';
0 ignored issues
show
Documentation introduced by
The property Value does not exist on object<EditableOption>. 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...
34
        $newOption->write();
35
        $newOptionID = $newOption->ID;
36
        $list->Options()->add($newOption);
0 ignored issues
show
Bug introduced by
The method Options() does not exist on EditableCheckboxGroupField. Did you maybe mean publishOptions()?

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...
37
38
        $form->doPublish();
39
40
        // Un-modified option should not create a new version
41
        $newLiveVersion = Versioned::get_versionnumber_by_stage('EditableOption', 'Live', $optionID, false);
42
        $this->assertNotEmpty($newLiveVersion);
0 ignored issues
show
Bug introduced by
The method assertNotEmpty() does not seem to exist on object<UserFormsVersionedTest>.

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...
43
        $this->assertEquals($liveVersion, $newLiveVersion);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<UserFormsVersionedTest>.

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...
44
45
        // New option is successfully published
46
        $newOptionLiveVersion = Versioned::get_versionnumber_by_stage('EditableOption', 'Live', $newOptionID, false);
47
        $this->assertNotEmpty($newOptionLiveVersion);
0 ignored issues
show
Bug introduced by
The method assertNotEmpty() does not seem to exist on object<UserFormsVersionedTest>.

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...
48
    }
49
}
50