|
10 | 10 |
|
11 | 11 |
|
12 | 12 | use PHPUnit_Framework_TestCase;
|
| 13 | +use RicardoFiorani\Container\ServicesContainer; |
| 14 | +use stdClass; |
13 | 15 |
|
14 | 16 | class ServicesContainerTest extends PHPUnit_Framework_TestCase
|
15 | 17 | {
|
| 18 | + public function testServiceContainerServiceRegistrationByArray() |
| 19 | + { |
| 20 | + $config = $this->getMockConfig(); |
| 21 | + $serviceContainer = $this->createServiceContainer($config); |
| 22 | + $this->assertTrue($serviceContainer->hasService('Youtube')); |
| 23 | + $this->assertInstanceOf('\\RicardoFiorani\\Renderer\\DefaultRenderer', $serviceContainer->getRenderer()); |
| 24 | + } |
16 | 25 |
|
| 26 | + public function testServiceContainerServiceRegistrationByInjection() |
| 27 | + { |
| 28 | + $serviceContainer = $this->createServiceContainer(); |
| 29 | + $serviceContainer->registerService('TestService', array('#testPattern#'), function () { |
| 30 | + // @todo test the injected service maybe ? |
| 31 | + }); |
| 32 | + |
| 33 | + $this->assertContains('TestService', $serviceContainer->getServiceNameList()); |
| 34 | + $this->setExpectedException('\\RicardoFiorani\\Exception\\DuplicatedServiceNameException'); |
| 35 | + $serviceContainer->registerService('TestService', array('#testPattern#'), function () { |
| 36 | + }); |
| 37 | + } |
| 38 | + |
| 39 | + public function testServicesList() |
| 40 | + { |
| 41 | + $config = $this->getMockConfig(); |
| 42 | + $serviceContainer = $this->createServiceContainer($config); |
| 43 | + $this->assertInternalType('array', $serviceContainer->getServices()); |
| 44 | + $this->assertContains('Youtube', $serviceContainer->getServices()); |
| 45 | + } |
| 46 | + |
| 47 | + public function testIfReturnsAlreadyInstantiatedFactory(){ |
| 48 | + $config = $this->getMockConfig(); |
| 49 | + $serviceContainer = $this->createServiceContainer($config); |
| 50 | + $factory = $serviceContainer->getFactory('Youtube'); |
| 51 | + $this->assertInstanceOf('\\RicardoFiorani\\Adapter\\Youtube\\Factory\\YoutubeServiceAdapterFactory',$factory); |
| 52 | + |
| 53 | + $alreadyInstantiatedFactory = $serviceContainer->getFactory('Youtube'); |
| 54 | + $this->assertEquals($factory,$alreadyInstantiatedFactory); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * @return ServicesContainer |
| 59 | + */ |
| 60 | + private function createServiceContainer(array $constructArray = array()) |
| 61 | + { |
| 62 | + $serviceContainer = new ServicesContainer($constructArray); |
| 63 | + |
| 64 | + return $serviceContainer; |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * @return array |
| 69 | + */ |
| 70 | + private function getMockConfig() |
| 71 | + { |
| 72 | + return array( |
| 73 | + 'services' => array( |
| 74 | + 'Youtube' => array( |
| 75 | + 'patterns' => array( |
| 76 | + '#(?:<\>]+href=\")?(?:http://)?((?:[a-zA-Z]{1,4}\.)?youtube.com/(?:watch)?\?v=(.{11}?))[^"]*(?:\"[^\<\>]*>)?([^\<\>]*)(?:)?#', |
| 77 | + '%(?:youtube\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i', |
| 78 | + ), |
| 79 | + 'factory' => '\\RicardoFiorani\\Adapter\\Youtube\\Factory\\YoutubeServiceAdapterFactory', |
| 80 | + ), |
| 81 | + ), |
| 82 | + 'renderer' => array( |
| 83 | + 'name' => 'DefaultRenderer', |
| 84 | + 'factory' => '\\RicardoFiorani\\Renderer\\Factory\\DefaultRendererFactory', |
| 85 | + ) |
| 86 | + ); |
| 87 | + } |
17 | 88 | }
|
0 commit comments