|
| 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