Skip to content

Commit 0d65213

Browse files
committed
Add unit test for \Magento\Framework\DomDocument\DomDocumentFactory
1 parent 177e540 commit 0d65213

File tree

2 files changed

+99
-2
lines changed

2 files changed

+99
-2
lines changed

lib/internal/Magento/Framework/DomDocument/DomDocumentFactory.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
namespace Magento\Framework\DomDocument;
88

9+
use DOMDocument;
10+
911
/**
1012
* DOM document factory
1113
*/
@@ -30,11 +32,11 @@ public function __construct(\Magento\Framework\ObjectManagerInterface $objectMan
3032
*
3133
* @param string $data the data to be loaded into the object
3234
*
33-
* @return \DOMDocument
35+
* @return DOMDocument
3436
*/
3537
public function create($data = null)
3638
{
37-
$dom = $this->objectManager->create('DOMDocument');
39+
$dom = $this->objectManager->create(DOMDocument::class);
3840

3941
if (!empty($data) && is_string($data)) {
4042
$dom->loadXML($data);
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Framework\Test\Unit\DomDocument;
8+
9+
use DOMDocument;
10+
use Magento\Framework\DomDocument\DomDocumentFactory;
11+
use Magento\Framework\ObjectManagerInterface;
12+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
13+
14+
class DomDocumentFactoryTest extends \PHPUnit\Framework\TestCase
15+
{
16+
/**
17+
* @var ObjectManagerHelper
18+
*/
19+
private $objectManagerHelper;
20+
21+
/**
22+
* @var ObjectManagerInterface|\PHPUnit_Framework_MockObject_MockObject
23+
*/
24+
private $objectManagerMock;
25+
26+
/**
27+
* @var DOMDocument|\PHPUnit_Framework_MockObject_MockObject
28+
*/
29+
private $domDocumentMock;
30+
31+
/**
32+
* @var DomDocumentFactory
33+
*/
34+
private $domDocumentFactory;
35+
36+
/**
37+
* @var string
38+
*/
39+
private $xmlSample = <<<EOT
40+
<?xml version="1.0"?>
41+
<root>
42+
</root>
43+
EOT;
44+
45+
/**
46+
* {@inheritdoc}
47+
*/
48+
protected function setUp()
49+
{
50+
$this->objectManagerHelper = new ObjectManagerHelper($this);
51+
$this->objectManagerMock = $this->createMock(ObjectManagerInterface::class);
52+
$this->domDocumentMock = $this->createMock(DOMDocument::class);
53+
$this->domDocumentFactory = $this->objectManagerHelper->getObject(
54+
DomDocumentFactory::class,
55+
[
56+
'objectManager' => $this->objectManagerMock
57+
]
58+
);
59+
}
60+
61+
/**
62+
* @dataProvider createDataProvider
63+
*/
64+
public function testCreate($data = null)
65+
{
66+
$this->objectManagerMock->expects($this->once())
67+
->method('create')
68+
->with(DOMDocument::class)
69+
->willReturn($this->domDocumentMock);
70+
71+
if (empty($data) || !is_string($data)) {
72+
$this->domDocumentMock->expects($this->never())
73+
->method('loadXML');
74+
} else {
75+
$this->domDocumentMock->expects($this->once())
76+
->method('loadXML')
77+
->with($data)
78+
->willReturn(true);
79+
}
80+
81+
$this->domDocumentFactory->create($data);
82+
}
83+
84+
/**
85+
* @return array
86+
*/
87+
public function createDataProvider(): array
88+
{
89+
return [
90+
[null],
91+
[''],
92+
[$this->xmlSample]
93+
];
94+
}
95+
}

0 commit comments

Comments
 (0)