|
| 1 | +<?php |
| 2 | +namespace Codeception\Extension; |
| 3 | + |
| 4 | +use Codeception\Platform\Extension as CodeceptionExtension; |
| 5 | +use Codeception\Extension; |
| 6 | +use InterNations\Component\HttpMock\Matcher\ExtractorFactory; |
| 7 | +use InterNations\Component\HttpMock\Server; |
| 8 | +use InterNations\Component\HttpMock\MockBuilder; |
| 9 | +use InterNations\Component\HttpMock\Matcher\MatcherFactory; |
| 10 | +use Mcustiel\DependencyInjection\DependencyInjectionService; |
| 11 | + |
| 12 | +class HttpMock extends CodeceptionExtension |
| 13 | +{ |
| 14 | + /** |
| 15 | + * @var array |
| 16 | + */ |
| 17 | + private $defaults = [ |
| 18 | + 'port' => '28080', |
| 19 | + 'host' => 'localhost' |
| 20 | + ]; |
| 21 | + /** |
| 22 | + * @var \InterNations\Component\HttpMock\Server |
| 23 | + */ |
| 24 | + private $server; |
| 25 | + /** |
| 26 | + * @var \Mcustiel\DependencyInjection\DependencyInjectionService |
| 27 | + */ |
| 28 | + private $diManager; |
| 29 | + |
| 30 | + public function __construct(array $config, array $options) |
| 31 | + { |
| 32 | + parent::__construct(array_merge($this->defaults, $config), $options); |
| 33 | + $this->diManager = new DependencyInjectionService(); |
| 34 | + $this->startHttpMock(); |
| 35 | + } |
| 36 | + |
| 37 | + public function __destruct() |
| 38 | + { |
| 39 | + $this->stopHttpMock(); |
| 40 | + } |
| 41 | + |
| 42 | + private function startHttpMock() |
| 43 | + { |
| 44 | + echo "Starting http mock server on {$this->config['host']}:{$this->config['port']}" . PHP_EOL; |
| 45 | + $this->server = new Server($this->config['port'], $this->config['host']); |
| 46 | + $this->server->start(); |
| 47 | + $this->setUpDependencyInjection(); |
| 48 | + } |
| 49 | + |
| 50 | + private function setUpDependencyInjection() |
| 51 | + { |
| 52 | + $this->diManager->register( |
| 53 | + 'httpMockServer', |
| 54 | + function () { |
| 55 | + return $this->server; |
| 56 | + } |
| 57 | + ); |
| 58 | + $this->diManager->register( |
| 59 | + 'httpMockBuilder', |
| 60 | + function () { |
| 61 | + return new MockBuilder(new MatcherFactory(), new ExtractorFactory()); |
| 62 | + } |
| 63 | + ); |
| 64 | + } |
| 65 | + |
| 66 | + private function stopHttpMock() |
| 67 | + { |
| 68 | + echo 'Stoping http mock server' . PHP_EOL; |
| 69 | + $this->server->stop(); |
| 70 | + } |
| 71 | +} |
0 commit comments