|
2 | 2 |
|
3 | 3 | namespace ErrorHeroModule\Spec; |
4 | 4 |
|
| 5 | +use Doctrine\DBAL\Connection; |
| 6 | +use Doctrine\DBAL\Driver\PDOMySql\Driver; |
| 7 | +use Doctrine\ORM\EntityManager; |
5 | 8 | use ErrorHeroModule\Listener\Mvc; |
6 | 9 | use ErrorHeroModule\Module; |
7 | 10 | use Kahlan\Plugin\Double; |
8 | 11 | use Zend\EventManager\EventManagerInterface; |
9 | 12 | use Zend\Mvc\Application; |
10 | 13 | use Zend\Mvc\MvcEvent; |
11 | 14 | use Zend\ServiceManager\ServiceLocatorInterface; |
| 15 | +use Zend\ModuleManager\ModuleEvent; |
| 16 | +use Zend\ModuleManager\ModuleManager; |
| 17 | +use Zend\ModuleManager\Listener\ConfigListener; |
12 | 18 |
|
13 | 19 | describe('Module', function () { |
14 | 20 |
|
|
29 | 35 |
|
30 | 36 | }); |
31 | 37 |
|
| 38 | + describe('->init()', function () { |
| 39 | + |
| 40 | + it('receive ModuleManager that get Eventmanager that attach ModuleEvent::EVENT_LOAD_MODULES_POST', function () { |
| 41 | + |
| 42 | + $moduleManager = Double::instance(['extends' => ModuleManager::class, 'methods' => '__construct']); |
| 43 | + $eventManager = Double::instance(['implements' => EventManagerInterface::class]); |
| 44 | + |
| 45 | + allow($moduleManager)->toReceive('getEventManager')->andReturn($eventManager); |
| 46 | + expect($eventManager)->toReceive('attach')->with(ModuleEvent::EVENT_LOAD_MODULES_POST, [$this->module, 'convertDoctrineToZendDbConfig']); |
| 47 | + |
| 48 | + $this->module->init($moduleManager); |
| 49 | + |
| 50 | + }); |
| 51 | + |
| 52 | + }); |
| 53 | + |
| 54 | + describe('->convertDoctrineToZendDbConfig()', function () { |
| 55 | + |
| 56 | + it('does not has EntityManager service', function () { |
| 57 | + |
| 58 | + $moduleEvent = Double::instance(['extends' => ModuleEvent::class, 'methods' => '__construct']); |
| 59 | + $serviceManager = Double::instance(['implements' => ServiceLocatorInterface::class]); |
| 60 | + allow($moduleEvent)->toReceive('getParam')->with('ServiceManager')->andReturn($serviceManager); |
| 61 | + allow($serviceManager)->toReceive('has')->with(EntityManager::class)->andReturn(false); |
| 62 | + |
| 63 | + $this->module->convertDoctrineToZendDbConfig($moduleEvent); |
| 64 | + expect($moduleEvent)->not->toReceive('getConfigListener'); |
| 65 | + |
| 66 | + }); |
| 67 | + |
| 68 | + it('has EntityManager service but already has db config', function () { |
| 69 | + |
| 70 | + $moduleEvent = Double::instance(['extends' => ModuleEvent::class, 'methods' => '__construct']); |
| 71 | + $serviceManager = Double::instance(['implements' => ServiceLocatorInterface::class]); |
| 72 | + allow($moduleEvent)->toReceive('getParam')->with('ServiceManager')->andReturn($serviceManager); |
| 73 | + allow($serviceManager)->toReceive('has')->with(EntityManager::class)->andReturn(true); |
| 74 | + |
| 75 | + $configListener = Double::instance(['extends' => ConfigListener::class, 'methods' => '__construct']); |
| 76 | + allow($moduleEvent)->toReceive('getConfigListener')->andReturn($configListener); |
| 77 | + allow($configListener)->toReceive('getMergedConfig')->andReturn([ |
| 78 | + 'db' => [ |
| 79 | + 'username' => 'root', |
| 80 | + 'password' => '', |
| 81 | + 'driver' => 'pdo_mysql', |
| 82 | + 'database' => 'mydb', |
| 83 | + 'host' => 'localhost', |
| 84 | + ], |
| 85 | + ]); |
| 86 | + |
| 87 | + $this->module->convertDoctrineToZendDbConfig($moduleEvent); |
| 88 | + expect($serviceManager)->not->toReceive('get')->with(EntityManager::class); |
| 89 | + |
| 90 | + }); |
| 91 | + |
| 92 | + it('has EntityManager service but already does not has db config', function () { |
| 93 | + |
| 94 | + $moduleEvent = Double::instance(['extends' => ModuleEvent::class, 'methods' => '__construct']); |
| 95 | + $serviceManager = Double::instance(['implements' => ServiceLocatorInterface::class]); |
| 96 | + allow($moduleEvent)->toReceive('getParam')->with('ServiceManager')->andReturn($serviceManager); |
| 97 | + allow($serviceManager)->toReceive('has')->with(EntityManager::class)->andReturn(true); |
| 98 | + |
| 99 | + $configListener = Double::instance(['extends' => ConfigListener::class, 'methods' => '__construct']); |
| 100 | + allow($moduleEvent)->toReceive('getConfigListener')->andReturn($configListener); |
| 101 | + allow($configListener)->toReceive('getMergedConfig')->andReturn([]); |
| 102 | + |
| 103 | + $entityManager = Double::instance(['extends' => EntityManager::class, 'methods' => '__construct']); |
| 104 | + $connection = Double::instance(['extends' => Connection::class, 'methods' => '__construct']); |
| 105 | + |
| 106 | + $driver = Double::instance(['extends' => Driver::class, 'methods' => '__construct']); |
| 107 | + allow($driver)->toReceive('getName')->andReturn('pdo_mysql'); |
| 108 | + |
| 109 | + allow($connection)->toReceive('getUsername')->andReturn('root'); |
| 110 | + allow($connection)->toReceive('getPassword')->andReturn(''); |
| 111 | + allow($connection)->toReceive('getDriver')->andReturn($driver); |
| 112 | + allow($connection)->toReceive('getDatabase')->andReturn('mydb'); |
| 113 | + allow($connection)->toReceive('getHost')->andReturn('localhost'); |
| 114 | + allow($connection)->toReceive('getPort')->andReturn('3306'); |
| 115 | + |
| 116 | + allow($entityManager)->toReceive('getConnection')->andReturn($connection); |
| 117 | + allow($serviceManager)->toReceive('get')->with(EntityManager::class)->andReturn($entityManager); |
| 118 | + |
| 119 | + $this->module->convertDoctrineToZendDbConfig($moduleEvent); |
| 120 | + expect($serviceManager)->toReceive('get')->with(EntityManager::class); |
| 121 | + |
| 122 | + }); |
| 123 | + |
| 124 | + }); |
| 125 | + |
32 | 126 | describe('->onBootstrap()', function () { |
33 | 127 |
|
34 | 128 | it('pull Mvc Listener and use it to attach with EventManager', function () { |
|
0 commit comments