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