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