Passed
Push — devel-3.0 ( 09ea81...3c7891 )
by Rubén
03:32
created

XmlHandlerTest::testSave()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * sysPass
4
 *
5
 * @author    nuxsmin
6
 * @link      https://syspass.org
7
 * @copyright 2012-2018, Rubén Domínguez nuxsmin@$syspass.org
8
 *
9
 * This file is part of sysPass.
10
 *
11
 * sysPass is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU General Public License as published by
13
 * the Free Software Foundation, either version 3 of the License, or
14
 * (at your option) any later version.
15
 *
16
 * sysPass is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU General Public License
22
 *  along with sysPass.  If not, see <http://www.gnu.org/licenses/>.
23
 */
24
25
namespace SP\Tests\Storage;
26
27
use PHPUnit\Framework\TestCase;
28
use SP\Storage\File\FileException;
29
use SP\Storage\File\FileHandler;
30
31
/**
32
 * Class XmlHandlerTest
33
 *
34
 * Tests unitarios para comprobar el funcionamiento de la clase SP\Storage\File\XmlHandler
35
 *
36
 * @package SP\Tests
37
 */
38
class XmlHandlerTest extends TestCase
39
{
40
    /**
41
     * @var \SP\Storage\File\XmlHandler
42
     */
43
    protected static $xmlHandler;
44
    /**
45
     * @var object Objeto con los datos a guardar en el archivo XML
46
     */
47
    protected static $itemsData;
48
    /**
49
     * @var array Elementos del archivo XML
50
     */
51
    protected $items;
52
53
    public static function setUpBeforeClass()
54
    {
55
        $file = RESOURCE_DIR . DIRECTORY_SEPARATOR . 'config.xml';
56
        self::$xmlHandler = new \SP\Storage\File\XmlHandler(new FileHandler($file));
57
58
        self::$itemsData = new \stdClass();
59
        self::$itemsData->configString = 'Hello world.';
60
        self::$itemsData->configNumber = 1;
61
        self::$itemsData->configArray = [1, 2, 3, 4];
62
    }
63
64
    /**
65
     * Test para comprobar el guardado de un archivo XML
66
     *
67
     * @doesNotPerformAssertions
68
     * @throws FileException
69
     */
70
    public function testSave()
71
    {
72
        self::$xmlHandler->save(self::$itemsData, 'config');
73
    }
74
75
    /**
76
     * Test para comprobar la carga de un archivo XML
77
     *
78
     * @throws \SP\Storage\File\FileException
79
     */
80
    public function testLoadMissingNode()
81
    {
82
        $this->expectException(\RuntimeException::class);
83
84
        self::$xmlHandler->load('root')->getItems();
85
    }
86
87
    /**
88
     * Test para comprobar la carga de un archivo XML
89
     *
90
     * @throws \SP\Storage\File\FileException
91
     */
92
    public function testLoad()
93
    {
94
        $this->items = self::$xmlHandler->load('config')->getItems();
95
96
        $this->assertTrue(is_array($this->items));
97
        $this->assertCount(3, $this->items);
98
99
        $this->assertSame(self::$itemsData->configString, $this->items['configString']);
100
        $this->assertSame(self::$itemsData->configNumber, $this->items['configNumber']);
101
102
        $this->assertTrue(is_array($this->items['configArray']));
103
        $this->assertCount(count(self::$itemsData->configArray), $this->items['configArray']);
104
    }
105
106
    /**
107
     * Test para comprobar el guardado de un archivo XML
108
     *
109
     * @throws FileException
110
     */
111
    public function testSaveNoItems()
112
    {
113
        $this->expectException(\RuntimeException::class);
114
115
        self::$xmlHandler->save(null, 'config');
116
    }
117
}