Completed
Push — master ( 3ef0b1...766c4a )
by Łukasz
22:35
created

testToStorageFieldDefinitionDefaultCurrentUser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21

Duplication

Lines 21
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 21
loc 21
rs 9.584
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * File containing the AuthorTest class.
5
 *
6
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
7
 * @license For full copyright and license information view LICENSE file distributed with this source code.
8
 */
9
namespace eZ\Publish\Core\Persistence\Legacy\Tests\Content\FieldValue\Converter;
10
11
use eZ\Publish\Core\FieldType\Author\Type as AuthorType;
12
use eZ\Publish\Core\FieldType\FieldSettings;
13
use eZ\Publish\SPI\Persistence\Content\FieldTypeConstraints;
14
use eZ\Publish\SPI\Persistence\Content\FieldValue;
15
use eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldValue;
16
use eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldDefinition;
17
use eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter\AuthorConverter;
18
use eZ\Publish\SPI\Persistence\Content\Type\FieldDefinition as SPIFieldDefinition;
19
use PHPUnit\Framework\TestCase;
20
use DOMDocument;
21
22
/**
23
 * Test case for Author converter in Legacy storage.
24
 *
25
 * @group fieldType
26
 * @group ezauthor
27
 */
28
class AuthorTest extends TestCase
29
{
30
    /**
31
     * @var \eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter\AuthorConverter
32
     */
33
    protected $converter;
34
35
    /**
36
     * @var \eZ\Publish\Core\FieldType\Author\Author[]
37
     */
38
    private $authors;
39
40
    protected function setUp()
41
    {
42
        parent::setUp();
43
        $this->converter = new AuthorConverter();
44
        $this->authors = array(
0 ignored issues
show
Documentation Bug introduced by
It seems like array(array('id' => 21, ...> '[email protected]')) of type array<integer,array<stri...email\":\"string\"}>"}> is incompatible with the declared type array<integer,object<eZ\...eldType\Author\Author>> of property $authors.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
45
            array('id' => 21, 'name' => 'Boba Fett', 'email' => '[email protected]'),
46
            array('id' => 42, 'name' => 'Darth Vader', 'email' => '[email protected]'),
47
            array('id' => 63, 'name' => 'Luke Skywalker', 'email' => '[email protected]'),
48
        );
49
    }
50
51
    protected function tearDown()
52
    {
53
        unset($this->authors);
54
        parent::tearDown();
55
    }
56
57
    /**
58
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter\AuthorConverter::toStorageValue
59
     */
60
    public function testToStorageValue()
61
    {
62
        $value = new FieldValue();
63
        $value->data = $this->authors;
64
        $storageFieldValue = new StorageFieldValue();
65
66
        $this->converter->toStorageValue($value, $storageFieldValue);
67
        $doc = new DOMDocument('1.0', 'utf-8');
68
        self::assertTrue($doc->loadXML($storageFieldValue->dataText));
69
70
        $authorsXml = $doc->getElementsByTagName('author');
71
        self::assertSame(count($this->authors), $authorsXml->length);
72
73
        // Loop against XML nodes and compare them to the real Author objects.
74
        // Then remove Author from $this->authors
75
        // This way, we can check if all authors have been converted in XML
76 View Code Duplication
        foreach ($authorsXml as $authorXml) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
77
            foreach ($this->authors as $i => $author) {
78
                if ($authorXml->getAttribute('id') == $author['id']) {
79
                    self::assertSame($author['name'], $authorXml->getAttribute('name'));
80
                    self::assertSame($author['email'], $authorXml->getAttribute('email'));
81
                    unset($this->authors[$i]);
82
                    break;
83
                }
84
            }
85
        }
86
87
        self::assertEmpty($this->authors, 'All authors have not been converted as expected');
88
    }
89
90
    /**
91
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter\AuthorConverter::toFieldValue
92
     */
93
    public function testToFieldValue()
94
    {
95
        $storageFieldValue = new StorageFieldValue();
96
        $storageFieldValue->dataText = <<<EOT
97
<?xml version="1.0" encoding="utf-8"?>
98
<ezauthor>
99
    <authors>
100
        <author id="1" name="Boba Fett" email="[email protected]"/>
101
        <author id="2" name="Darth Vader" email="[email protected]"/>
102
        <author id="3" name="Luke Skywalker" email="[email protected]"/>
103
    </authors>
104
</ezauthor>
105
EOT;
106
        $doc = new DOMDocument('1.0', 'utf-8');
107
        self::assertTrue($doc->loadXML($storageFieldValue->dataText));
108
        $authorsXml = $doc->getElementsByTagName('author');
0 ignored issues
show
Unused Code introduced by
$authorsXml 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...
109
        $fieldValue = new FieldValue();
110
111
        $this->converter->toFieldValue($storageFieldValue, $fieldValue);
112
        self::assertInternalType('array', $fieldValue->data);
113
114
        $authorsXml = $doc->getElementsByTagName('author');
115
        self::assertSame($authorsXml->length, count($fieldValue->data));
116
117
        $aAuthors = $fieldValue->data;
118 View Code Duplication
        foreach ($fieldValue->data as $i => $author) {
0 ignored issues
show
Bug introduced by
The expression $fieldValue->data of type integer|double|boolean|string|null|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
119
            foreach ($authorsXml as $authorXml) {
120
                if ($authorXml->getAttribute('id') == $author['id']) {
121
                    self::assertSame($authorXml->getAttribute('name'), $author['name']);
122
                    self::assertSame($authorXml->getAttribute('email'), $author['email']);
123
                    unset($aAuthors[$i]);
124
                    break;
125
                }
126
            }
127
        }
128
        self::assertEmpty($aAuthors, 'All authors have not been converted as expected from storage');
129
    }
130
131
    /**
132
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter\AuthorConverter::toStorageFieldDefinition
133
     */
134 View Code Duplication
    public function testToStorageFieldDefinitionDefaultCurrentUser()
135
    {
136
        $storageFieldDef = new StorageFieldDefinition();
137
        $fieldTypeConstraints = new FieldTypeConstraints();
138
        $fieldTypeConstraints->fieldSettings = new FieldSettings(
139
            array(
140
                'defaultAuthor' => AuthorType::DEFAULT_CURRENT_USER,
141
            )
142
        );
143
        $fieldDef = new SPIFieldDefinition(
144
            array(
145
                'fieldTypeConstraints' => $fieldTypeConstraints,
146
            )
147
        );
148
149
        $this->converter->toStorageFieldDefinition($fieldDef, $storageFieldDef);
150
        self::assertSame(
151
            AuthorType::DEFAULT_CURRENT_USER,
152
            $storageFieldDef->dataInt1
153
        );
154
    }
155
156
    /**
157
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter\AuthorConverter::toStorageFieldDefinition
158
     */
159 View Code Duplication
    public function testToStorageFieldDefinitionDefaultEmpty()
160
    {
161
        $storageFieldDef = new StorageFieldDefinition();
162
        $fieldTypeConstraints = new FieldTypeConstraints();
163
        $fieldTypeConstraints->fieldSettings = new FieldSettings(
164
            array(
165
                'defaultAuthor' => AuthorType::DEFAULT_EMPTY,
166
            )
167
        );
168
        $fieldDef = new SPIFieldDefinition(
169
            array(
170
                'fieldTypeConstraints' => $fieldTypeConstraints,
171
            )
172
        );
173
174
        $this->converter->toStorageFieldDefinition($fieldDef, $storageFieldDef);
175
        self::assertSame(
176
            AuthorType::DEFAULT_EMPTY,
177
            $storageFieldDef->dataInt1
178
        );
179
    }
180
}
181