2.11. Registry

    • Zend Framework 1: holds the application’s loggerobject, front controller etc.
    • Yii Framework: holds all the applicationcomponents, such as , , etc.
    1. <?php
    2.  
    3. namespace DesignPatterns\Structural\Registry\Tests;
    4.  
    5. use DesignPatterns\Structural\Registry\Registry;
    6. use stdClass;
    7. use PHPUnit\Framework\TestCase;
    8.  
    9. class RegistryTest extends TestCase
    10. public function testSetAndGetLogger()
    11. {
    12. $key = Registry::LOGGER;
    13. $logger = new stdClass();
    14.  
    15. Registry::set($key, $logger);
    16. $storedLogger = Registry::get($key);
    17.  
    18. $this->assertSame($logger, $storedLogger);
    19. $this->assertInstanceOf(stdClass::class, $storedLogger);
    20. }
    21.  
    22. /**
    23. * @expectedException \InvalidArgumentException
    24. */
    25. public function testThrowsExceptionWhenTryingToSetInvalidKey()
    26. Registry::set('foobar', new stdClass());
    27. }
    28.  
    29. /**
    30. * notice @runInSeparateProcess here: without it, a previous test might have set it already and
    31. * testing would not be possible. That's why you should implement Dependency Injection where an
    32. * injected class may easily be replaced by a mockup
    33. *
    34. * @runInSeparateProcess
    35. * @expectedException \InvalidArgumentException
    36. */
    37. public function testThrowsExceptionWhenTryingToGetNotSetKey()
    38. {
    39. Registry::get(Registry::LOGGER);
    40. }
    41. }