File: factoryTest.php

package info (click to toggle)
phpdox 0.7.0-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 5,064 kB
  • ctags: 2,240
  • sloc: xml: 50,881; php: 7,770; makefile: 22; sh: 5
file content (261 lines) | stat: -rw-r--r-- 9,961 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
<?php
/**
 * Copyright (c) 2010-2011 Arne Blankerts <arne@blankerts.de>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 *   * Redistributions of source code must retain the above copyright notice,
 *     this list of conditions and the following disclaimer.
 *
 *   * Redistributions in binary form must reproduce the above copyright notice,
 *     this list of conditions and the following disclaimer in the documentation
 *     and/or other materials provided with the distribution.
 *
 *   * Neither the name of Arne Blankerts nor the names of contributors
 *     may be used to endorse or promote products derived from this software
 *     without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT  * NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER ORCONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 * @package    phpDox
 * @subpackage Tests
 * @author     Bastian Feder <phpdox@bastian-feder.de>
 * @copyright  Arne Blankerts <arne@blankerts.de>, All rights reserved.
 * @license    BSD License
 */

namespace TheSeer\phpDox\Tests\Unit {

    use TheSeer\phpDox\Factory;

    /**
     * Class FactoryTest
     *
     * @covers TheSeer\phpDox\Factory
     */
    class FactoryTest extends \PHPUnit_Framework_TestCase {

        /**
         * @covers TheSeer\phpDox\Factory::__construct
         * @covers TheSeer\phpDox\Factory::setXMLDir
         */
        /*
        public function testSetXMLDir() {
            $factory = new Factory(array('Tux' => 'Gnu'));
            $factory->setXMLDir('/os/mascott/Tux');

            $this->assertAttributeEquals('/os/mascott/Tux', 'xmlDir', $factory);
            $this->assertAttributeEquals(array('Tux' => 'Gnu'), 'map', $factory);
        }
        */

        /**
         * @covers TheSeer\phpDox\Factory::addFactory
         */
        public function testAddFactory() {
            $factory = new Factory();
            $factory->addFactory('Gnu', $factory);
            $actual = $this->readAttribute($factory, 'map');

            $this->assertInstanceOf('TheSeer\phpDox\Factory', $actual['Gnu']);
        }

        /**
         * @covers TheSeer\phpDox\Factory::addClass
         */
        public function testAddClass() {
            $factory = new Factory();
            $factory->addClass('Gnu', 'myClass');
            $actual = $this->readAttribute($factory, 'map');

            $this->assertEquals('myClass', $actual['Gnu']);
        }

        /**
         * @covers TheSeer\phpDox\Factory::getInstanceFor
         */
        public function testGetInstanceForClass() {
            $factory = new Factory();
            $factory->addClass('Gnu', 'TheSeer\\phpDox\\Factory');

            $this->assertInstanceOf('TheSeer\\phpDox\\Factory', $factory->getInstanceFor('Gnu'));
        }

        /**
         * @covers TheSeer\phpDox\Factory::getInstanceFor
         */
        public function testGetInstanceForClassWithParameterArray() {
            $factory = new Factory();
            $factory->addClass('Gnu', 'TheSeer\\phpDox\\Factory');

            $this->assertInstanceOf('TheSeer\\phpDox\\Factory', $factory->getInstanceFor('Gnu', array('Tux' =>200)));
        }

        /**
         * @covers TheSeer\phpDox\Factory::getInstanceFor
         */
        public function testGetInstanceForClassWithParameterString() {
            $factory = new Factory();
            $factory->addClass('Gnu', 'Exception');

            $this->assertInstanceOf('Exception', $factory->getInstanceFor('Gnu', 'Tux', 200));
        }

        /**
         * @covers TheSeer\phpDox\Factory::getInstanceFor
         * @uses TheSeer\phpDox\Factory
         */
        public function testGetInstanceForFactory() {
            $factory = new Factory();
            $factory->addClass('TheSeer\\phpDox\\Factory', new Factory());

            $this->assertInstanceOf('TheSeer\\phpDox\\Factory', $factory->getInstanceFor('TheSeer\\phpDox\\Factory'));
        }

        /**
         * @covers TheSeer\phpDox\Factory::getInstanceFor
         * @uses TheSeer\phpDox\ProgressLogger
         */
        public function testGetInstanceForMethod() {
            $factory = new Factory();

            $this->assertInstanceOf('TheSeer\phpDox\ProgressLogger', $factory->getInstanceFor('Logger', 'silent'));
        }

        /**
         * @dataProvider getGenericInstanceDataprovider
         * @covers TheSeer\phpDox\Factory::getGenericInstance
         */
        public function testgetGenericInstance($expected, $classname, $arguments) {
            $factory = new FactoryProxy();
            $this->assertInstanceOf($expected, $factory->getGenericInstance($classname, $arguments));
        }

        /**
         * @expectedException \TheSeer\phpDox\FactoryException
         * @dataProvider getGenericInstanceExpectionFactoryExceptionDataprovider
         * @covers TheSeer\phpDox\Factory::getGenericInstance
         */
        public function testgetGenericInstanceExpectingFactoryException($classname, $argument) {
            $factory = new FactoryProxy();
            $factory->getGenericInstance($classname, $argument);

        }

        /**
         * @dataProvider getScannerDataproviderNoExclude
         * @covers TheSeer\phpDox\Factory::getScanner
         */
        public function testGetScannerNoExclude($methodName, $include) {

            $directoryScanner = $this->getMockBuilder('\\stdClass')
                ->setMethods(array($methodName))
                ->getMock();

            $directoryScanner
                ->expects($this->once())
                ->method($methodName)
                ->with($this->equalTo($include));

            $factory = $this->getMockBuilder('\\TheSeer\\phpDox\\Tests\\Unit\\FactoryProxy')
                ->setMethods(array('getInstanceFor'))
                ->getMock();
            $factory
                ->expects($this->once())
                ->method('getInstanceFor')
                ->with($this->equalTo('DirectoryScanner'))
                ->will($this->returnValue($directoryScanner));

                $this->assertInstanceOf('\\stdClass', $factory->getScanner($include));
        }

        /**
         * @dataProvider getScannerDataproviderWithExclude
         * @covers TheSeer\phpDox\Factory::getScanner
         */
        public function testGetScannerWithExclude($methodName, $include, $exclude) {

            $directoryScanner = $this->getMockBuilder('\\stdClass')
                ->setMethods($methodName)
                ->getMock();

            $directoryScanner
                ->expects($this->once())
                ->method($methodName[0])
                ->with($this->equalTo($include));

            $directoryScanner
                ->expects($this->once())
                ->method($methodName[1])
                ->with($this->equalTo($exclude));

            $factory = $this->getMockBuilder('\\TheSeer\\phpDox\\Tests\\Unit\\FactoryProxy')
                ->setMethods(array('getInstanceFor'))
                ->getMock();
            $factory
                ->expects($this->once())
                ->method('getInstanceFor')
                ->with($this->equalTo('DirectoryScanner'))
                ->will($this->returnValue($directoryScanner));

                $this->assertInstanceOf('\\stdClass', $factory->getScanner($include, $exclude));
        }


        /*********************************************************************/
        /* Dataprovider & Callbacks                                          */
        /*********************************************************************/

        public static function getScannerDataproviderNoExclude() {
            return array(
                'include as string' => array('addInclude', 'myInclude'),
                'include as array' => array('setIncludes', array('myInclude')),
            );
        }

        public static function getScannerDataproviderWithExclude() {
            return array(
                'exclude as string' => array(array('addInclude', 'addExclude'), 'myInclude', 'myExclude'),
                'exclude as array' => array(array('setIncludes', 'setExcludes'), array('myInclude'), array('myExclude')),
            );
        }

        public static function getGenericInstanceDataprovider() {
            return array(
                'class with no arguments' => array('TheSeer\\phpDox\\Factory', 'TheSeer\\phpDox\\Factory', array()),
                'class with array as argument' => array('TheSeer\\phpDox\\Factory', 'TheSeer\\phpDox\\Factory', array(array())),
            );
        }

        public static function getGenericInstanceExpectionFactoryExceptionDataprovider() {
            return array(
                'not instanciable' => array('Iterator', array()),
                'no constructor' => array('stdClass', array(array())),
            );
        }
    }


    class FactoryProxy extends Factory {

        public function getScanner($include, $exclude = null) {
            return parent::getScanner($include, $exclude);
        }

        public function getGenericInstance($class, array $params) {
            return parent::getGenericInstance($class, $params);
        }
    }
}