Passed
Branch master (7a0b0e)
by Randy
02:56
created

TranslatorTest::testXmlElementTranslation()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 43
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 43
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 34
nc 1
nop 0
1
<?php
2
3
namespace Dgame\Soap\Test;
4
5
use Dgame\Soap\Dom\Translator;
6
use Dgame\Soap\XmlElement;
7
use Dgame\Soap\XmlNode;
8
use DOMDocument;
9
use PHPUnit\Framework\TestCase;
10
11
/**
12
 * Class TranslatorTest
13
 * @package Dgame\Soap\Test
14
 */
15
final class TranslatorTest extends TestCase
16
{
17
    public function testXmlElementTranslation()
18
    {
19
        $doc        = new DOMDocument('1.0');
20
        $translator = new Translator();
21
22
        $node = $translator->translateNode($doc->createElement('test', 'foobar'));
23
24
        $this->assertNotNull($node);
25
        $this->assertInstanceOf(XmlElement::class, $node);
26
        $this->assertEquals('test', $node->getName());
27
        $this->assertTrue($node->hasValue());
28
        $this->assertFalse($node->hasAttributes());
29
        $this->assertEquals('test', $node->getName());
30
        $this->assertEquals('foobar', $node->getValue());
31
32
        $node = $translator->translateNode($doc->createElement('test', '42'));
33
34
        $this->assertNotNull($node);
35
        $this->assertInstanceOf(XmlElement::class, $node);
36
        $this->assertEquals('test', $node->getName());
37
        $this->assertTrue($node->hasValue());
38
        $this->assertFalse($node->hasAttributes());
39
        $this->assertEquals('test', $node->getName());
40
        $this->assertEquals(42, $node->getValue());
41
42
        $element = $doc->createElementNs('http://www.example.com/bar', 'bar:test', 'foobar');
43
        $element->setAttribute('id', '0');
44
45
        $node = $translator->translateNode($element);
46
47
        $this->assertNotNull($node);
48
        $this->assertInstanceOf(XmlElement::class, $node);
49
        $this->assertEquals('test', $node->getName());
50
        $this->assertTrue($node->hasValue());
51
        $this->assertTrue($node->hasAttributes());
52
        $this->assertTrue($node->hasPrefix());
53
        $this->assertCount(1, $node->getAttributes());
54
        $this->assertEquals('bar', $node->getPrefix());
55
        $this->assertEquals('test', $node->getName());
56
        $this->assertEquals('foobar', $node->getValue());
57
        $this->assertEquals('id', $node->getAttributes()[0]->getName());
58
        $this->assertEquals(0, $node->getAttributes()[0]->getValue());
59
    }
60
61
    public function testXmlNodeTranslation()
62
    {
63
        $doc        = new DOMDocument('1.0');
64
        $translator = new Translator();
65
66
        $element = $doc->createElementNs('http://www.example.com/abc', 'abc:test');
67
        $element->setAttribute('id', '0');
68
        $element->appendChild($doc->createElement('name', 'Franz'));
69
        $element->appendChild($doc->createElement('age', '42'));
70
71
        $node = $translator->translateNode($element);
72
73
        $this->assertNotNull($node);
74
        $this->assertInstanceOf(XmlNode::class, $node);
75
        $this->assertEquals('test', $node->getName());
76
        $this->assertFalse($node->hasValue());
77
        $this->assertTrue($node->hasAttributes());
78
        $this->assertTrue($node->hasChildren());
79
        $this->assertTrue($node->hasPrefix());
80
        $this->assertEquals('abc', $node->getPrefix());
81
        $this->assertEquals('test', $node->getName());
82
        $this->assertFalse($node->hasValue());
83
        $this->assertCount(1, $node->getAttributes());
84
        $this->assertEquals('id', $node->getAttributes()[0]->getName());
85
        $this->assertEquals(0, $node->getAttributes()[0]->getValue());
86
        $this->assertInstanceOf(XmlElement::class, $node->getChildren()[0]);
87
        $this->assertInstanceOf(XmlElement::class, $node->getChildren()[1]);
88
        $this->assertCount(2, $node->getChildren());
89
        $this->assertEquals('name', $node->getChildren()[0]->getName());
90
        $this->assertEquals('Franz', $node->getChildren()[0]->getValue());
91
        $this->assertEquals('age', $node->getChildren()[1]->getName());
92
        $this->assertEquals(42, $node->getChildren()[1]->getValue());
93
    }
94
95
    public function testDocumentTranslation()
96
    {
97
        $doc = new DOMDocument('1.0');
98
        $doc->load(__DIR__ . '/xml/test1.xml');
99
100
        $translator = new Translator();
101
        $elements   = $translator->translateDocument($doc);
102
103
        $this->assertCount(1, $elements);
104
        $this->assertEquals('soap-env', $elements[0]->getName());
105
        $this->assertCount(2, $elements[0]->getChildren());
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Dgame\Soap\Element as the method getChildren() does only exist in the following sub-classes of Dgame\Soap\Element: Dgame\Soap\XmlNode. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
106
107
        $child1 = $elements[0]->getChildren()[0];
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Dgame\Soap\Element as the method getChildren() does only exist in the following sub-classes of Dgame\Soap\Element: Dgame\Soap\XmlNode. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
108
        $child2 = $elements[0]->getChildren()[1];
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Dgame\Soap\Element as the method getChildren() does only exist in the following sub-classes of Dgame\Soap\Element: Dgame\Soap\XmlNode. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
109
110
        $this->assertInstanceOf(XmlNode::class, $child1);
111
        $this->assertInstanceOf(XmlNode::class, $child2);
112
113
        $this->assertEquals('person', $child1->getName());
114
        $this->assertTrue($child1->hasChildren());
115
        $this->assertCount(4, $child1->getChildren());
116
        $this->assertTrue($child1->hasAttributes());
117
        $this->assertCount(1, $child1->getAttributes());
118
119
        $this->assertEquals('name', $child1->getAttributes()[0]->getName());
120
        $this->assertEquals('Max Musterman', $child1->getAttributes()[0]->getValue());
121
122
        $this->assertInstanceOf(XmlElement::class, $child1->getChildren()[0]);
123
        $this->assertEquals('car', $child1->getChildren()[0]->getName());
124
        $this->assertFalse($child1->getChildren()[0]->hasValue());
125
        $this->assertCount(2, $child1->getChildren()[0]->getAttributes());
126
        $this->assertEquals('marke', $child1->getChildren()[0]->getAttributes()[0]->getName());
127
        $this->assertEquals('BMW', $child1->getChildren()[0]->getAttributes()[0]->getValue());
128
129
        $this->assertInstanceOf(XmlElement::class, $child1->getChildren()[1]);
130
        $this->assertEquals('phone', $child1->getChildren()[1]->getName());
131
        $this->assertEquals(9, $child1->getChildren()[1]->getValue());
132
        $this->assertCount(1, $child1->getChildren()[1]->getAttributes());
133
        $this->assertEquals('name', $child1->getChildren()[1]->getAttributes()[0]->getName());
134
        $this->assertEquals('iPhone', $child1->getChildren()[1]->getAttributes()[0]->getValue());
135
136
        $this->assertInstanceOf(XmlElement::class, $child1->getChildren()[2]);
137
        $this->assertEquals('birth-place', $child1->getChildren()[2]->getName());
138
        $this->assertEquals('Hamburg', $child1->getChildren()[2]->getValue());
139
        $this->assertFalse($child1->getChildren()[2]->hasAttributes());
140
141
        $this->assertInstanceOf(XmlNode::class, $child1->getChildren()[3]);
142
        $this->assertEquals('address', $child1->getChildren()[3]->getName());
143
        $this->assertFalse($child1->getChildren()[3]->hasValue());
144
        $this->assertFalse($child1->getChildren()[3]->hasAttributes());
145
        $this->assertCount(2, $child1->getChildren()[3]->getChildren());
146
147
        $i = 0;
148
        foreach (['street' => 'Hauptstraße 1', 'plz' => '245698'] as $name => $value) {
149
            $this->assertEquals($name, $child1->getChildren()[3]->getChildren()[$i]->getName());
150
            $this->assertEquals($value, $child1->getChildren()[3]->getChildren()[$i]->getValue());
151
152
            $i++;
153
        }
154
155
        $this->assertEquals('person', $child2->getName());
156
        $this->assertTrue($child2->hasAttributes());
157
        $this->assertCount(1, $child2->getAttributes());
158
        $this->assertTrue($child2->hasChildren());
159
        $this->assertCount(4, $child2->getChildren());
160
161
        $this->assertEquals('name', $child2->getAttributes()[0]->getName());
162
        $this->assertEquals('Dr. Dolittle', $child2->getAttributes()[0]->getValue());
163
164
        $this->assertInstanceOf(XmlElement::class, $child2->getChildren()[0]);
165
        $this->assertEquals('car', $child2->getChildren()[0]->getName());
166
        $this->assertFalse($child2->getChildren()[0]->hasValue());
167
        $this->assertCount(2, $child2->getChildren()[0]->getAttributes());
168
        $this->assertEquals('marke', $child2->getChildren()[0]->getAttributes()[0]->getName());
169
        $this->assertEquals('Audi', $child2->getChildren()[0]->getAttributes()[0]->getValue());
170
171
        $this->assertInstanceOf(XmlElement::class, $child2->getChildren()[1]);
172
        $this->assertEquals('phone', $child2->getChildren()[1]->getName());
173
        $this->assertEquals('Xperia Z3', $child2->getChildren()[1]->getValue());
174
        $this->assertCount(1, $child2->getChildren()[1]->getAttributes());
175
        $this->assertEquals('name', $child2->getChildren()[1]->getAttributes()[0]->getName());
176
        $this->assertEquals('Sony', $child2->getChildren()[1]->getAttributes()[0]->getValue());
177
178
        $this->assertInstanceOf(XmlElement::class, $child2->getChildren()[2]);
179
        $this->assertEquals('birth-place', $child2->getChildren()[2]->getName());
180
        $this->assertEquals('München', $child2->getChildren()[2]->getValue());
181
        $this->assertFalse($child2->getChildren()[2]->hasAttributes());
182
183
        $this->assertInstanceOf(XmlNode::class, $child2->getChildren()[3]);
184
        $this->assertEquals('address', $child2->getChildren()[3]->getName());
185
        $this->assertFalse($child2->getChildren()[3]->hasValue());
186
        $this->assertFalse($child2->getChildren()[3]->hasAttributes());
187
        $this->assertCount(2, $child2->getChildren()[3]->getChildren());
188
189
        $i = 0;
190
        foreach (['street' => 'Partkstraße', 'plz' => '365494'] as $name => $value) {
191
            $this->assertEquals($name, $child2->getChildren()[3]->getChildren()[$i]->getName());
192
            $this->assertEquals($value, $child2->getChildren()[3]->getChildren()[$i]->getValue());
193
194
            $i++;
195
        }
196
    }
197
}