Completed
Push — v2 ( 3135e6...5d3e3a )
by Joschi
04:42 queued 10s
created

DocumentFactoryTest.php ➔ extension_loaded()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * micrometa
5
 *
6
 * @category Jkphl
7
 * @package Jkphl\Micrometa
8
 * @subpackage Infrastructure
9
 * @author Joschi Kuphal <[email protected]> / @jkphl
10
 * @copyright Copyright © 2017 Joschi Kuphal <[email protected]> / @jkphl
11
 * @license http://opensource.org/licenses/MIT The MIT License (MIT)
12
 */
13
14
/***********************************************************************************
15
 *  The MIT License (MIT)
16
 *
17
 *  Copyright © 2017 Joschi Kuphal <[email protected]> / @jkphl
18
 *
19
 *  Permission is hereby granted, free of charge, to any person obtaining a copy of
20
 *  this software and associated documentation files (the "Software"), to deal in
21
 *  the Software without restriction, including without limitation the rights to
22
 *  use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
23
 *  the Software, and to permit persons to whom the Software is furnished to do so,
24
 *  subject to the following conditions:
25
 *
26
 *  The above copyright notice and this permission notice shall be included in all
27
 *  copies or substantial portions of the Software.
28
 *
29
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
31
 *  FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
32
 *  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
33
 *  IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
34
 *  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35
 ***********************************************************************************/
36
37
namespace Jkphl\Micromoeta\Tests\Infrastructure {
38
39
    use Jkphl\Micrometa\Infrastructure\Factory\DocumentFactory;
40
41
    /**
42
     * Document factory test
43
     *
44
     * @package Jkphl\Micrometa
45
     * @subpackage Jkphl\Micromoeta\Tests
46
     */
47
    class DocumentFactoryTest extends \PHPUnit_Framework_TestCase
48
    {
49
        /**
50
         * Valid local test HTML document
51
         *
52
         * @var string
53
         */
54
        const VALID_HTML_URL = 'http://localhost:1349/valid-test.html';
55
        /**
56
         * Invalid local test document
57
         *
58
         * @var string
59
         */
60
        const INVALID_DOCUMENT_URL = 'http://localhost:1349/invalid-test.html';
61
        /**
62
         * Non-existing local test document
63
         *
64
         * @var string
65
         */
66
        const NONEXISTING_DOCUMENT_URL = 'http://localhost:1349/none';
67
68
        /**
69
         * Test the HTML document instantiation via a HTTP client
70
         */
71
        public function testDocumentCreationViaHttpClient()
72
        {
73
            $dom = DocumentFactory::createFromUri(self::VALID_HTML_URL);
74
            $this->assertInstanceOf(\DOMDocument::class, $dom);
75
        }
76
77
        /**
78
         * Test the HTML document instantiation via the PHP stream wrapper
79
         */
80
        public function testDocumentCreationViaStreamWrapper()
81
        {
82
            putenv('MOCK_EXTENSION_LOADED=1');
83
            $dom = DocumentFactory::createFromUri(self::VALID_HTML_URL);
84
            $this->assertInstanceOf(\DOMDocument::class, $dom);
85
            putenv('MOCK_EXTENSION_LOADED=');
86
        }
87
88
        /**
89
         * Test an invalid document
90
         *
91
         * @expectedException \Jkphl\Micrometa\Ports\Exceptions\InvalidArgumentException
92
         */
93
        public function testDocumentCreationWithInvalidDocument()
94
        {
95
            DocumentFactory::createFromUri(self::INVALID_DOCUMENT_URL);
96
        }
97
98
        /**
99
         * Test a malformed URL
100
         *
101
         * @expectedException \Jkphl\Micrometa\Ports\Exceptions\InvalidArgumentException
102
         */
103
        public function testDocumentCreationWithMalformedUrl()
104
        {
105
            DocumentFactory::createFromUri('test://');
106
        }
107
108
        /**
109
         * Test a non-existing document
110
         *
111
         * @expectedException \Jkphl\Micrometa\Ports\Exceptions\RuntimeException
112
         */
113
        public function testDocumentCreationWithNonExistingDocument()
114
        {
115
            DocumentFactory::createFromUri(self::NONEXISTING_DOCUMENT_URL);
116
        }
117
    }
118
}
119
120
121
namespace Jkphl\Micrometa\Infrastructure\Factory {
122
123
    /**
124
     * Find out whether an extension is loaded
125
     *
126
     * @link http://php.net/manual/en/function.extension-loaded.php
127
     * @param string $name The extension name
128
     * @return bool true if the extension identified by name is loaded, false otherwise
129
     */
130
    function extension_loaded($name)
131
    {
132
        return getenv('MOCK_EXTENSION_LOADED') ? false : \extension_loaded($name);
133
    }
134
}
135