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