4de7bbccd70c528dbdbb0f982c1cb0c78e83620b
[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 use CoreFramework\Generic\UnsupportedOperationException;
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 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 boolean key
527          */
528         public function testUnsettingBooleanConfigKey () {
529                 // Will throw this exception
530                 $this->expectException(InvalidArgumentException::class);
531
532                 // Test it
533                 self::$configInstance->unsetConfigEntry(FALSE);
534         }
535
536         /**
537          * Tests unsetting decimal key
538          */
539         public function testUnsettingDecimalConfigKey () {
540                 // Will throw this exception
541                 $this->expectException(InvalidArgumentException::class);
542
543                 // Test it
544                 self::$configInstance->unsetConfigEntry(12345);
545         }
546
547         /**
548          * Tests unsetting float key
549          */
550         public function testUnsettingFloatConfigKey () {
551                 // Will throw this exception
552                 $this->expectException(InvalidArgumentException::class);
553
554                 // Test it
555                 self::$configInstance->unsetConfigEntry(123.45);
556         }
557
558         /**
559          * Tests unsetting array key
560          */
561         public function testUnsettingArrayConfigKey () {
562                 // Will throw this exception
563                 $this->expectException(InvalidArgumentException::class);
564
565                 // Test it
566                 self::$configInstance->unsetConfigEntry(array());
567         }
568
569         /**
570          * Tests unsetting object key
571          */
572         public function testUnsettingObjectConfigKey () {
573                 // Will throw this exception
574                 $this->expectException(InvalidArgumentException::class);
575
576                 // Test it
577                 self::$configInstance->unsetConfigEntry($this);
578         }
579
580         /**
581          * Tests unsetting resource key
582          */
583         public function testUnsettingResourceConfigKey () {
584                 // Will throw this exception
585                 $this->expectException(InvalidArgumentException::class);
586
587                 // Init some resource
588                 $resource = fopen(__FILE__, 'r');
589
590                 // Test it
591                 self::$configInstance->unsetConfigEntry($resource);
592         }
593
594         /**
595          * Tests unsetting an empty key
596          */
597         public function testUnettingEmptyConfigKey () {
598                 // Will throw this exception
599                 $this->expectException(InvalidArgumentException::class);
600
601                 // Test it
602                 self::$configInstance->unsetConfigEntry('');
603         }
604
605         /**
606          * Tests unsetting a non-existing key
607          */
608         public function testUnettingNonExistingConfigKey () {
609                 // Will throw this exception
610                 $this->expectException(NoConfigEntryException::class);
611
612                 // Test it
613                 self::$configInstance->unsetConfigEntry('__non_existing_key__');
614         }
615
616         /**
617          * Tests setting and getting a key will return same value of same type
618          * (NULL). This unit test does also unset the then existing key.
619          */
620         public function testSettingGettiingNullConfigKey () {
621                 // Set test value
622                 self::$configInstance->setConfigEntry('__test_key', NULL);
623
624                 // Get it
625                 $value = self::$configInstance->getConfigEntry('__test_key');
626
627                 // Must be equal
628                 $this->assertEquals(NULL, $value);
629
630                 // Unset it
631                 self::$configInstance->unsetConfigEntry('__test_key');
632         }
633
634         /**
635          * Tests setting and getting a key will return same value of same type.
636          * This unit test does also unset the then existing key.
637          */
638         public function testSettingGettiingConfigKey () {
639                 // Set test value
640                 self::$configInstance->setConfigEntry('__test_key', 'foo');
641
642                 // Get it
643                 $value = self::$configInstance->getConfigEntry('__test_key');
644
645                 // Must be equal
646                 $this->assertEquals('foo', $value);
647
648                 // Unset it
649                 self::$configInstance->unsetConfigEntry('__test_key');
650         }
651
652         /**
653          * Tests if the method getField() is still unsupported in this class. Please
654          * note, that this and isFieldSet() may get removed in the future. So also
655          * these test methods will be gone.
656          */
657         public function testConfigGetFieldUnsupported () {
658                 // Expect this exception
659                 $this->expectException(UnsupportedOperationException::class);
660
661                 // Test it
662                 $dummy = self::$configInstance->getField('foo');
663         }
664
665         /**
666          * Tests if the method isFieldSet() is still unsupported in this class. Please
667          * note, that this and getField() may get removed in the future. So also
668          * these test methods will be gone.
669          */
670         public function testConfigIsFieldSetUnsupported () {
671                 // Expect this exception
672                 $this->expectException(UnsupportedOperationException::class);
673
674                 // Test it
675                 $dummy = self::$configInstance->isFieldSet('foo');
676         }
677
678 }