Skip to content

Commit

Permalink
Merge pull request #17 from olexp/add-factory
Browse files Browse the repository at this point in the history
Add addFactoryToContainer method
  • Loading branch information
Naktibalda authored Oct 16, 2021
2 parents 3cfcc47 + 1fd4b05 commit 7423b3b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/Codeception/Lib/Connector/Laminas.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ class Laminas extends AbstractBrowser
*/
private $persistentServices = [];

/**
* @var array
*/
private $persistentFactories = [];

public function setApplicationConfig(array $applicationConfig): void
{
$this->applicationConfig = $applicationConfig;
Expand Down Expand Up @@ -148,6 +153,15 @@ public function addServiceToContainer(string $name, $service): void
$this->persistentServices[$name] = $service;
}

public function addFactoryToContainer(string $name, $factory): void
{
$this->application->getServiceManager()->setAllowOverride(true);
$this->application->getServiceManager()->setFactory($name, $factory);
$this->application->getServiceManager()->setAllowOverride(false);

$this->persistentFactories[$name] = $factory;
}

private function extractHeaders(BrowserKitRequest $browserKitRequest): Headers
{
$headers = [];
Expand Down Expand Up @@ -200,5 +214,9 @@ private function createApplication(): void
$eventManager = $this->application->getEventManager();

$eventManager->detach([$sendResponseListener, 'sendResponse']);

$serviceManager->setAllowOverride(true);
$serviceManager->configure(['factories' => $this->persistentFactories]);
$serviceManager->setAllowOverride(false);
}
}
14 changes: 14 additions & 0 deletions src/Codeception/Module/Laminas.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Laminas\Router\Http\Hostname;
use Laminas\Router\Http\Part;
use Laminas\Router\Http\TreeRouteStack;
use Laminas\ServiceManager\Factory\FactoryInterface;
use Traversable;
use function array_unique;
use function class_exists;
Expand Down Expand Up @@ -181,6 +182,19 @@ public function addServiceToContainer(string $name, object $service): void
$this->client->addServiceToContainer($name, $service);
}

/**
* Adds factory to a Laminas container
*
* @param string $name
* @param string|callable|FactoryInterface $factory
* @return void
* @part services
*/
public function addFactoryToContainer(string $name, $factory): void
{
$this->client->addFactoryToContainer($name, $factory);
}

/**
* Opens web page using route name and parameters.
*
Expand Down

0 comments on commit 7423b3b

Please sign in to comment.