Continued:
[core.git] / tests / framework / config / FrameworkConfigurationTest.php
1 <?php
2 // Same namespace as target class
3 namespace Org\Mxchange\CoreFramework\Configuration;
4
5 // Inport framework stuff
6 use Org\Mxchange\CoreFramework\Bootstrap\FrameworkBootstrap;
7 use Org\Mxchange\CoreFramework\Configuration\FrameworkConfiguration;
8 use Org\Mxchange\CoreFramework\Loader\ClassLoader;
9 use Org\Mxchange\CoreFramework\Generic\NullPointerException;
10 use Org\Mxchange\CoreFramework\Generic\UnsupportedOperationException;
11
12 // Import PHPUnit stuff
13 use PHPUnit\Framework\Error\Notice;
14 use PHPUnit\Framework\TestCase;
15
16 // Import SPL stuff
17 use \InvalidArgumentException;
18
19 /*
20  * Copyright (C) 2017 Roland Haeder<roland@mxchange.org>
21  *
22  * This program is free software: you can redistribute it and/or modify
23  * it under the terms of the GNU General Public License as published by
24  * the Free Software Foundation, either version 3 of the License, or
25  * (at your option) any later version.
26  *
27  * This program is distributed in the hope that it will be useful,
28  * but WITHOUT ANY WARRANTY; without even the implied warranty of
29  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
30  * GNU General Public License for more details.
31  *
32  * You should have received a copy of the GNU General Public License
33  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
34  */
35 class FrameworkConfigurationTest extends TestCase {
36
37         /**
38          * The configuration instance being tested
39          */
40         private static $configInstance = NULL;
41
42         /**
43          * Setup test case
44          */
45         public function setUp() {
46                 // Trace message
47                 //* NOISY-DEBUG: */ printf('[%s:%d]: CALLED!' . PHP_EOL, __METHOD__, __LINE__);
48
49                 // Call parent method
50                 parent::setUp();
51
52                 // Trace message
53                 //* NOISY-DEBUG: */ printf('[%s:%d]: EXIT!' . PHP_EOL, __METHOD__, __LINE__);
54
55         }
56
57         /**
58          * Setup test case
59          */
60         public static function setUpBeforeClass() {
61                 // Trace message
62                 //* NOISY-DEBUG: */ printf('[%s:%d]: CALLED!' . PHP_EOL, __METHOD__, __LINE__);
63
64                 // Call parent method
65                 parent::setUpBeforeClass();
66
67                 // Init instance
68                 self::$configInstance = FrameworkBootstrap::getConfigurationInstance();
69
70                 /*
71                  * Disable strict naming-convention check in own class loader, because
72                  * PHP_Invoker doesn't have namespaces.
73                  */
74                 ClassLoader::enableStrictNamingConventionCheck(FALSE);
75
76                 // Trace message
77                 //* NOISY-DEBUG: */ printf('[%s:%d]: EXIT!' . PHP_EOL, __METHOD__, __LINE__);
78         }
79
80         /**
81          * Tests if __toString() returns proper name
82          */
83         public function testConfigToStringClassName () {
84                 // Get it
85                 $className = self::$configInstance->__toString();
86
87                 // Should be equal
88                 $this->assertEquals('Org\Mxchange\CoreFramework\Configuration\FrameworkConfiguration', $className);
89         }
90
91         /**
92          * Tests getting a singleton instance
93          */
94         public function testGettingSelfConfigInstance () {
95                 // Get instance
96                 $dummyInstance = FrameworkBootstrap::getConfigurationInstance();
97
98                 // Should be equal to own instance
99                 $this->assertEquals(self::$configInstance, $dummyInstance);
100         }
101
102         /**
103          * Tests equals() method
104          */
105         public function testEqualsConfigInstance () {
106                 // Get instance
107                 $dummyInstance = new FrameworkConfiguration();
108
109                 // Should return TRUE
110                 $this->assertTrue(self::$configInstance->equals($dummyInstance));
111         }
112
113         /**
114          * Tests hashCode() method
115          */
116         public function testHashCodeConfigInstance () {
117                 // Get instance
118                 $dummyInstance = FrameworkBootstrap::getConfigurationInstance();
119
120                 // Get hash code from both
121                 $hashCodeExpected = self::$configInstance->hashCode();
122                 $hashCodeActual = $dummyInstance->hashCode();
123
124                 // Should be equal
125                 $this->assertEquals($hashCodeExpected, $hashCodeActual);
126         }
127
128         /**
129          * Tests if getting configuration entry returns an array
130          */
131         public function testGettingConfigurationArray () {
132                 // Get it
133                 $config = self::$configInstance->getConfigurationArray();
134
135                 // Should be an array
136                 $this->assertTrue(is_array($config));
137         }
138
139         /**
140          * Tests if getting configuration entry returns a filled array
141          */
142         public function testGettingfilledConfigurationArray () {
143                 // Get it
144                 $config = self::$configInstance->getConfigurationArray();
145
146                 // Should be an array
147                 $this->assertTrue(count($config) > 0);
148         }
149
150         /**
151          * Tests if getting whole configuration entry is the same for another
152          * singleton instance
153          */
154         public function testSameConfigurationArrayGetter () {
155                 // Get instance
156                 $dummyInstance = FrameworkBootstrap::getConfigurationInstance();
157
158                 // Get it from both instances
159                 $config1 = self::$configInstance->getConfigurationArray();
160                 $config2 = $dummyInstance->getConfigurationArray();
161
162                 // Should be the same
163                 $this->assertEquals($config1, $config2);
164         }
165
166         /**
167          * Tests if a proper exception is thrown when check for a NULL key
168          */
169         public function testCheckingNullConfigKey () {
170                 // Will throw this exception
171                 $this->expectException(NullPointerException::class);
172
173                 // Test it
174                 $dummy = self::$configInstance->isConfigurationEntrySet(NULL);
175         }
176
177         /**
178          * Tests if a proper exception is thrown when checking a boolean key
179          */
180         public function testCheckingBooleanConfigKey () {
181                 // Will throw this exception
182                 $this->expectException(InvalidArgumentException::class);
183
184                 // Test it
185                 $dummy = self::$configInstance->isConfigurationEntrySet(FALSE);
186         }
187
188         /**
189          * Tests if a proper exception is thrown when checking an empty key
190          */
191         public function testCheckingEmptyConfigKey () {
192                 // Will throw this exception
193                 $this->expectException(InvalidArgumentException::class);
194
195                 // Test it
196                 $dummy = self::$configInstance->isConfigurationEntrySet('');
197         }
198
199         /**
200          * Tests if a proper exception is thrown when checking an array key
201          */
202         public function testCheckingArrayConfigKey () {
203                 // Will throw this exception
204                 $this->expectException(InvalidArgumentException::class);
205
206                 // Test it
207                 $dummy = self::$configInstance->isConfigurationEntrySet(array());
208         }
209
210         /**
211          * Tests if a proper exception is thrown when checking a decimal key
212          */
213         public function testCheckingDecimalConfigKey () {
214                 // Will throw this exception
215                 $this->expectException(InvalidArgumentException::class);
216
217                 // Test it
218                 $dummy = self::$configInstance->isConfigurationEntrySet(12345);
219         }
220
221         /**
222          * Tests if a proper exception is thrown when checking a float key
223          */
224         public function testCheckingFloatConfigKey () {
225                 // Will throw this exception
226                 $this->expectException(InvalidArgumentException::class);
227
228                 // Test it
229                 $dummy = self::$configInstance->isConfigurationEntrySet(123.45);
230         }
231
232         /**
233          * Tests if a proper exception is thrown when checking an object key
234          */
235         public function testCheckingObjectConfigKey () {
236                 // Will throw this exception
237                 $this->expectException(InvalidArgumentException::class);
238
239                 // Test it
240                 $dummy = self::$configInstance->isConfigurationEntrySet($this);
241         }
242
243         /**
244          * Tests if a proper exception is thrown when checking a resource key
245          */
246         public function testCheckingResourceConfigKey () {
247                 // Will throw this exception
248                 $this->expectException(InvalidArgumentException::class);
249
250                 // Init some resource
251                 $resource = fopen(__FILE__, 'r');
252
253                 // Test it
254                 $dummy = self::$configInstance->isConfigurationEntrySet($resource);
255         }
256
257         /**
258          * Tests if checking an existing (well-known) key can be found and returns
259          * TRUE.
260          */
261         public function testCheckingExistingConfigKey () {
262                 // Should return TRUE
263                 $this->assertTrue(self::$configInstance->isConfigurationEntrySet('application_base_path'));
264         }
265
266         /**
267          * Tests if checking a non-existing key will return FALSE.
268          */
269         public function testCheckingNonExistingConfigKey () {
270                 // Should return FALSE
271                 $this->assertFalse(self::$configInstance->isConfigurationEntrySet('__non_existing_key__'));
272         }
273
274         /**
275          * Tests if a proper exception is thrown when getting a NULL key
276          */
277         public function testGettingNullConfigKey () {
278                 // Will throw this exception
279                 $this->expectException(NullPointerException::class);
280
281                 // Test it
282                 $dummy = self::$configInstance->getConfigEntry(NULL);
283         }
284
285         /**
286          * Tests if a proper exception is thrown when getting a boolean key
287          */
288         public function testGettingBooleanConfigKey () {
289                 // Will throw this exception
290                 $this->expectException(InvalidArgumentException::class);
291
292                 // Test it
293                 $dummy = self::$configInstance->getConfigEntry(FALSE);
294         }
295
296         /**
297          * Tests if a proper exception is thrown when getting an empty key
298          */
299         public function testGettingEmptyConfigKey () {
300                 // Will throw this exception
301                 $this->expectException(InvalidArgumentException::class);
302
303                 // Test it
304                 $dummy = self::$configInstance->getConfigEntry('');
305         }
306
307         /**
308          * Tests if a proper exception is thrown when getting a decimal key
309          */
310         public function testGettingDecimalConfigKey () {
311                 // Will throw this exception
312                 $this->expectException(InvalidArgumentException::class);
313
314                 // Test it
315                 $dummy = self::$configInstance->getConfigEntry(12345);
316         }
317
318         /**
319          * Tests if a proper exception is thrown when getting a float key
320          */
321         public function testGettingFloatConfigKey () {
322                 // Will throw this exception
323                 $this->expectException(InvalidArgumentException::class);
324
325                 // Test it
326                 $dummy = self::$configInstance->getConfigEntry(123.45);
327         }
328
329         /**
330          * Tests if a proper exception is thrown when getting an array key
331          */
332         public function testGettingArrayConfigKey () {
333                 // Will throw this exception
334                 $this->expectException(InvalidArgumentException::class);
335
336                 // Test it
337                 $dummy = self::$configInstance->getConfigEntry(array());
338         }
339
340         /**
341          * Tests if a proper exception is thrown when getting an object key
342          */
343         public function testGettingObjectConfigKey () {
344                 // Will throw this exception
345                 $this->expectException(InvalidArgumentException::class);
346
347                 // Test it
348                 $dummy = self::$configInstance->getConfigEntry($this);
349         }
350
351         /**
352          * Tests if a proper exception is thrown when getting a resource key
353          */
354         public function testGettingResourceConfigKey () {
355                 // Will throw this exception
356                 $this->expectException(InvalidArgumentException::class);
357
358                 // Init some resource
359                 $resource = fopen(__FILE__, 'r');
360
361                 // Test it
362                 $dummy = self::$configInstance->getConfigEntry($resource);
363         }
364
365         /**
366          * Tests if getting a non-existing key will cause a proper exception been
367          * thrown.
368          */
369         public function testGettingNonExistingConfigKey () {
370                 // Should cause this exception
371                 $this->expectException(NoConfigEntryException::class);
372
373                 // Get non-existing key
374                 $dummy = self::$configInstance->getConfigEntry('__non_existing_key__');
375         }
376
377         /**
378          * Tests if a generic key 'application_base_path' can be found.
379          */
380         public function testGettingConfigKeyApplicationBasePathExists () {
381                 // Get it from config instance
382                 $value = self::$configInstance->getConfigEntry('application_base_path');
383
384                 // Is it a readable path?
385                 $this->assertDirectoryIsReadable($value);
386         }
387
388         /**
389          * Tests setting a NULL key (value doesn't matter)
390          */
391         public function testSettingNullConfigKey () {
392                 // Will throw this exception
393                 $this->expectException(NullPointerException::class);
394
395                 // Test it
396                 self::$configInstance->setConfigEntry(NULL, 'foo');
397         }
398
399         /**
400          * Tests setting a boolean key (value doesn't matter)
401          */
402         public function testSettingBooleanConfigKey () {
403                 // Will throw this exception
404                 $this->expectException(InvalidArgumentException::class);
405
406                 // Test it
407                 self::$configInstance->setConfigEntry(FALSE, 'foo');
408         }
409
410         /**
411          * Tests setting an empty key (value doesn't matter)
412          */
413         public function testSettingEmptyConfigKey () {
414                 // Will throw this exception
415                 $this->expectException(InvalidArgumentException::class);
416
417                 // Test it
418                 self::$configInstance->setConfigEntry('', 'foo');
419         }
420
421         /**
422          * Tests setting a decimal key (value doesn't matter)
423          */
424         public function testSettingDecimalConfigKey () {
425                 // Will throw this exception
426                 $this->expectException(InvalidArgumentException::class);
427
428                 // Test it
429                 self::$configInstance->setConfigEntry(12345, 'foo');
430         }
431
432         /**
433          * Tests setting a float key (value doesn't matter)
434          */
435         public function testSettingFloatConfigKey () {
436                 // Will throw this exception
437                 $this->expectException(InvalidArgumentException::class);
438
439                 // Test it
440                 self::$configInstance->setConfigEntry(123.45, 'foo');
441         }
442
443         /**
444          * Tests setting an array key (value doesn't matter)
445          */
446         public function testSettingArrayConfigKey () {
447                 // Will throw this exception
448                 $this->expectException(InvalidArgumentException::class);
449
450                 // Test it
451                 self::$configInstance->setConfigEntry(array(), 'foo');
452         }
453
454         /**
455          * Tests setting an object key (value doesn't matter)
456          */
457         public function testSettingObjectConfigKey () {
458                 // Will throw this exception
459                 $this->expectException(InvalidArgumentException::class);
460
461                 // Test it
462                 self::$configInstance->setConfigEntry($this, 'foo');
463         }
464
465         /**
466          * Tests setting a resource key (value doesn't matter)
467          */
468         public function testSettingResourceConfigKey () {
469                 // Will throw this exception
470                 $this->expectException(InvalidArgumentException::class);
471
472                 // Init some resource
473                 $resource = fopen(__FILE__, 'r');
474
475                 // Test it
476                 self::$configInstance->setConfigEntry($resource, 'foo');
477         }
478
479         /**
480          * Tests setting a valid key but array for value
481          */
482         public function testSettingArrayValueConfigKey () {
483                 // Will throw this exception
484                 $this->expectException(InvalidArgumentException::class);
485
486                 // Test it
487                 self::$configInstance->setConfigEntry('foo', array());
488         }
489
490         /**
491          * Tests setting a valid key but object for value
492          */
493         public function testSettingObjectValueConfigKey () {
494                 // Will throw this exception
495                 $this->expectException(InvalidArgumentException::class);
496
497                 // Test it
498                 self::$configInstance->setConfigEntry('foo', $this);
499         }
500
501         /**
502          * Tests setting a valid key but resource for value
503          */
504         public function testSettingResourceValueConfigKey () {
505                 // Will throw this exception
506                 $this->expectException(InvalidArgumentException::class);
507
508                 // Init some resource
509                 $resource = fopen(__FILE__, 'r');
510
511                 // Test it
512                 self::$configInstance->setConfigEntry('foo', $resource);
513         }
514
515         /**
516          * Tests unsetting NULL key
517          */
518         public function testUnsettingNullConfigKey () {
519                 // Will throw this exception
520                 $this->expectException(NullPointerException::class);
521
522                 // Test it
523                 self::$configInstance->unsetConfigEntry(NULL);
524         }
525
526         /**
527          * Tests unsetting boolean key
528          */
529         public function testUnsettingBooleanConfigKey () {
530                 // Will throw this exception
531                 $this->expectException(InvalidArgumentException::class);
532
533                 // Test it
534                 self::$configInstance->unsetConfigEntry(FALSE);
535         }
536
537         /**
538          * Tests unsetting decimal key
539          */
540         public function testUnsettingDecimalConfigKey () {
541                 // Will throw this exception
542                 $this->expectException(InvalidArgumentException::class);
543
544                 // Test it
545                 self::$configInstance->unsetConfigEntry(12345);
546         }
547
548         /**
549          * Tests unsetting float key
550          */
551         public function testUnsettingFloatConfigKey () {
552                 // Will throw this exception
553                 $this->expectException(InvalidArgumentException::class);
554
555                 // Test it
556                 self::$configInstance->unsetConfigEntry(123.45);
557         }
558
559         /**
560          * Tests unsetting array key
561          */
562         public function testUnsettingArrayConfigKey () {
563                 // Will throw this exception
564                 $this->expectException(InvalidArgumentException::class);
565
566                 // Test it
567                 self::$configInstance->unsetConfigEntry(array());
568         }
569
570         /**
571          * Tests unsetting object key
572          */
573         public function testUnsettingObjectConfigKey () {
574                 // Will throw this exception
575                 $this->expectException(InvalidArgumentException::class);
576
577                 // Test it
578                 self::$configInstance->unsetConfigEntry($this);
579         }
580
581         /**
582          * Tests unsetting resource key
583          */
584         public function testUnsettingResourceConfigKey () {
585                 // Will throw this exception
586                 $this->expectException(InvalidArgumentException::class);
587
588                 // Init some resource
589                 $resource = fopen(__FILE__, 'r');
590
591                 // Test it
592                 self::$configInstance->unsetConfigEntry($resource);
593         }
594
595         /**
596          * Tests unsetting an empty key
597          */
598         public function testUnettingEmptyConfigKey () {
599                 // Will throw this exception
600                 $this->expectException(InvalidArgumentException::class);
601
602                 // Test it
603                 self::$configInstance->unsetConfigEntry('');
604         }
605
606         /**
607          * Tests unsetting a non-existing key
608          */
609         public function testUnettingNonExistingConfigKey () {
610                 // Will throw this exception
611                 $this->expectException(NoConfigEntryException::class);
612
613                 // Test it
614                 self::$configInstance->unsetConfigEntry('__non_existing_key__');
615         }
616
617         /**
618          * Tests setting and getting a key will return same value of same type
619          * (NULL). This unit test does also unset the then existing key.
620          */
621         public function testSettingGettiingNullConfigKey () {
622                 // Set test value
623                 self::$configInstance->setConfigEntry('__test_key', NULL);
624
625                 // Get it
626                 $value = self::$configInstance->getConfigEntry('__test_key');
627
628                 // Must be equal
629                 $this->assertEquals(NULL, $value);
630
631                 // Unset it
632                 self::$configInstance->unsetConfigEntry('__test_key');
633         }
634
635         /**
636          * Tests setting and getting a key will return same value of same type.
637          * This unit test does also unset the then existing key.
638          */
639         public function testSettingGettiingConfigKey () {
640                 // Set test value
641                 self::$configInstance->setConfigEntry('__test_key', 'foo');
642
643                 // Get it
644                 $value = self::$configInstance->getConfigEntry('__test_key');
645
646                 // Must be equal
647                 $this->assertEquals('foo', $value);
648
649                 // Unset it
650                 self::$configInstance->unsetConfigEntry('__test_key');
651         }
652
653         /**
654          * Tests if the method getField() is still unsupported in this class. Please
655          * note, that this and isFieldSet() may get removed in the future. So also
656          * these test methods will be gone.
657          */
658         public function testConfigGetFieldUnsupported () {
659                 // Expect this exception
660                 $this->expectException(UnsupportedOperationException::class);
661
662                 // Test it
663                 $dummy = self::$configInstance->getField('foo');
664         }
665
666         /**
667          * Tests if the method isFieldSet() is still unsupported in this class. Please
668          * note, that this and getField() may get removed in the future. So also
669          * these test methods will be gone.
670          */
671         public function testConfigIsFieldSetUnsupported () {
672                 // Expect this exception
673                 $this->expectException(UnsupportedOperationException::class);
674
675                 // Test it
676                 $dummy = self::$configInstance->isFieldSet('foo');
677         }
678
679 }