Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Open sidebar
project
drupal
Commits
29dd37a2
Commit
29dd37a2
authored
Sep 21, 2013
by
alexpott
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Issue
#2083983
by damiankloip: Unit test Drupal\serialization\Encoder\XmlEncoder class.
parent
4be4a983
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
125 additions
and
5 deletions
+125
-5
core/modules/serialization/lib/Drupal/serialization/Encoder/XmlEncoder.php
...alization/lib/Drupal/serialization/Encoder/XmlEncoder.php
+21
-5
core/modules/serialization/tests/Drupal/serialization/Tests/Encoder/XmlEncoderTest.php
...sts/Drupal/serialization/Tests/Encoder/XmlEncoderTest.php
+104
-0
No files found.
core/modules/serialization/lib/Drupal/serialization/Encoder/XmlEncoder.php
View file @
29dd37a2
...
...
@@ -35,10 +35,26 @@ class XmlEncoder extends SerializerAwareEncoder implements EncoderInterface, Dec
protected
$baseEncoder
;
/**
* Constucts the XmlEncoder object, creating a BaseXmlEncoder class also.
* Gets the base encoder instance.
*
* @return \Symfony\Component\Serializer\Encoder\XmlEncoder
* The base encoder.
*/
public
function
getBaseEncoder
()
{
if
(
!
isset
(
$this
->
baseEncoder
))
{
$this
->
baseEncoder
=
new
BaseXmlEncoder
();
}
return
$this
->
baseEncoder
;
}
/**
* Sets the base encoder instance.
*
* @param \Symfony\Component\Serializer\Encoder\XmlEncoder $encoder
*/
public
function
__construct
(
)
{
$this
->
baseEncoder
=
new
BaseXmlE
ncoder
()
;
public
function
setBaseEncoder
(
$encoder
)
{
$this
->
baseEncoder
=
$e
ncoder
;
}
/**
...
...
@@ -46,7 +62,7 @@ public function __construct() {
*/
public
function
encode
(
$data
,
$format
,
array
$context
=
array
()){
$normalized
=
$this
->
serializer
->
normalize
(
$data
,
$format
,
$context
);
return
$this
->
b
aseEncoder
->
encode
(
$normalized
,
$format
,
$context
);
return
$this
->
getB
aseEncoder
()
->
encode
(
$normalized
,
$format
,
$context
);
}
/**
...
...
@@ -60,7 +76,7 @@ public function supportsEncoding($format) {
* Implements \Symfony\Component\Serializer\Encoder\EncoderInterface::decode().
*/
public
function
decode
(
$data
,
$format
,
array
$context
=
array
()){
return
$this
->
b
aseEncoder
->
decode
(
$data
,
$format
,
$context
);
return
$this
->
getB
aseEncoder
()
->
decode
(
$data
,
$format
,
$context
);
}
/**
...
...
core/modules/serialization/tests/Drupal/serialization/Tests/Encoder/XmlEncoderTest.php
0 → 100644
View file @
29dd37a2
<?php
/**
* @file
* Contains \Drupal\serialization\Tests\Encoder\XmlEncoderTest.
*/
namespace
Drupal\serialization\Tests\Encoder
;
use
Drupal\serialization\Encoder\XmlEncoder
;
use
Drupal\Tests\UnitTestCase
;
/**
* Tests the XmlEncoder class.
*
* @see \Drupal\serialization\Encoder\XmlEncoder
*/
class
XmlEncoderTest
extends
UnitTestCase
{
/**
* The XmlEncoder instance.
*
* @var \Drupal\serialization\Encoder\XmlEncoder
*/
protected
$encoder
;
/**
* @var \Symfony\Component\Serializer\Encoder\XmlEncoder|\PHPUnit_Framework_MockObject_MockObject
*/
protected
$baseEncoder
;
/**
* An array of test data.
*
* @var array
*/
protected
$testArray
=
array
(
'test'
=>
'test'
);
public
static
function
getInfo
()
{
return
array
(
'name'
=>
'XmlEncoderTest'
,
'description'
=>
'Tests the XmlEncoder class.'
,
'group'
=>
'Serialization'
,
);
}
public
function
setUp
()
{
$this
->
baseEncoder
=
$this
->
getMock
(
'Symfony\Component\Serializer\Encoder\XmlEncoder'
);
$this
->
encoder
=
new
XmlEncoder
();
$this
->
encoder
->
setBaseEncoder
(
$this
->
baseEncoder
);
}
/**
* Tests the supportsEncoding() method.
*/
public
function
testSupportsEncoding
()
{
$this
->
assertTrue
(
$this
->
encoder
->
supportsEncoding
(
'xml'
));
$this
->
assertFalse
(
$this
->
encoder
->
supportsEncoding
(
'json'
));
}
/**
* Tests the supportsDecoding() method.
*/
public
function
testSupportsDecoding
()
{
$this
->
assertTrue
(
$this
->
encoder
->
supportsDecoding
(
'xml'
));
$this
->
assertFalse
(
$this
->
encoder
->
supportsDecoding
(
'json'
));
}
/**
* Tests the encode() method.
*/
public
function
testEncode
()
{
$serializer
=
$this
->
getMockBuilder
(
'Symfony\Component\Serializer\Serializer'
)
->
disableOriginalConstructor
()
->
setMethods
(
array
(
'normalize'
))
->
getMock
();
$serializer
->
expects
(
$this
->
once
())
->
method
(
'normalize'
)
->
with
(
$this
->
testArray
,
'test'
,
array
())
->
will
(
$this
->
returnValue
(
$this
->
testArray
));
$this
->
encoder
->
setSerializer
(
$serializer
);
$this
->
baseEncoder
->
expects
(
$this
->
once
())
->
method
(
'encode'
)
->
with
(
$this
->
testArray
,
'test'
,
array
())
->
will
(
$this
->
returnValue
(
'test'
));
$this
->
assertEquals
(
'test'
,
$this
->
encoder
->
encode
(
$this
->
testArray
,
'test'
));
}
/**
* Tests the decode() method.
*/
public
function
testDecode
()
{
$this
->
baseEncoder
->
expects
(
$this
->
once
())
->
method
(
'decode'
)
->
with
(
'test'
,
'test'
,
array
())
->
will
(
$this
->
returnValue
(
$this
->
testArray
));
$this
->
assertEquals
(
$this
->
testArray
,
$this
->
encoder
->
decode
(
'test'
,
'test'
));
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment