Skip to content

Commit

Permalink
Reduce SessionStrategyFactory memory usage
Browse files Browse the repository at this point in the history
  • Loading branch information
aik099 committed Mar 13, 2024
1 parent efd9e1d commit 01c25b6
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 64 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
- Changed default OS from "Windows 7" to "Windows 10" for BrowserStack/SauceLabs browser configurations.
- Allow using self-signed/invalid SSL certificates during testing on the SauceLabs by default.
- Rewritten library object communication mechanism (the event dispatcher is no longer used). Update any custom session strategy/browser configuration implementations.
- Reduce memory consumption by rewriting `SessionStrategyFactory` and `SessionStrategyManager` classes.

### Fixed
...
Expand Down
2 changes: 0 additions & 2 deletions library/.phpstorm.meta.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
'session_factory' => \aik099\PHPUnit\Session\SessionFactory::class,
'session_strategy_factory' => \aik099\PHPUnit\Session\SessionStrategyFactory::class,
'session_strategy_manager' => \aik099\PHPUnit\Session\SessionStrategyManager::class,
'isolated_session_strategy' => \aik099\PHPUnit\Session\IsolatedSessionStrategy::class,
'shared_session_strategy' => \aik099\PHPUnit\Session\SharedSessionStrategy::class,
'remote_url' => \aik099\PHPUnit\RemoteCoverage\RemoteUrl::class,
'remote_coverage_helper' => \aik099\PHPUnit\RemoteCoverage\RemoteCoverageHelper::class,
'test_suite_factory' => \aik099\PHPUnit\TestSuite\TestSuiteFactory::class,
Expand Down
22 changes: 13 additions & 9 deletions library/aik099/PHPUnit/DIContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use aik099\PHPUnit\MinkDriver\ZombieDriverFactory;
use aik099\PHPUnit\RemoteCoverage\RemoteCoverageHelper;
use aik099\PHPUnit\RemoteCoverage\RemoteUrl;
use aik099\PHPUnit\Session\ISessionStrategyFactory;
use aik099\PHPUnit\Session\IsolatedSessionStrategy;
use aik099\PHPUnit\Session\SessionFactory;
use aik099\PHPUnit\Session\SessionStrategyFactory;
Expand Down Expand Up @@ -65,7 +66,18 @@ public function __construct(array $values = array())

$this['session_strategy_factory'] = function ($c) {
$session_strategy_factory = new SessionStrategyFactory();
$session_strategy_factory->setApplication($c['application']);

$session_strategy_factory->register(
ISessionStrategyFactory::TYPE_ISOLATED,
new IsolatedSessionStrategy($c['session_factory'])
);

$session_strategy_factory->register(
ISessionStrategyFactory::TYPE_SHARED,
new SharedSessionStrategy(
new IsolatedSessionStrategy($c['session_factory'])
)
);

return $session_strategy_factory;
};
Expand All @@ -74,14 +86,6 @@ public function __construct(array $values = array())
return new SessionStrategyManager($c['session_strategy_factory']);
};

$this['isolated_session_strategy'] = $this->factory(function ($c) {
return new IsolatedSessionStrategy($c['session_factory']);
});

$this['shared_session_strategy'] = $this->factory(function ($c) {
return new SharedSessionStrategy($c['isolated_session_strategy']);
});

$this['remote_url'] = function () {
return new RemoteUrl();
};
Expand Down
36 changes: 19 additions & 17 deletions library/aik099/PHPUnit/Session/SessionStrategyFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,34 +11,39 @@
namespace aik099\PHPUnit\Session;


use aik099\PHPUnit\Application;
use aik099\PHPUnit\IApplicationAware;

/**
* Produces sessions.
*
* @method \Mockery\Expectation shouldReceive(string $name)
*/
class SessionStrategyFactory implements ISessionStrategyFactory, IApplicationAware
class SessionStrategyFactory implements ISessionStrategyFactory
{

/**
* Application.
* Session strategies.
*
* @var Application
* @var ISessionStrategy[]
*/
protected $application;
protected $sessionStrategies = array();

/**
* Sets application.
* Registers a browser configuration.
*
* @param Application $application The application.
* @param string $strategy_type Session strategy type.
* @param ISessionStrategy $session_strategy Session strategy.
*
* @return void
* @throws \InvalidArgumentException When session strategy is already registered.
*/
public function setApplication(Application $application)
public function register($strategy_type, ISessionStrategy $session_strategy)
{
$this->application = $application;
if ( isset($this->sessionStrategies[$strategy_type]) ) {
throw new \InvalidArgumentException(
'Session strategy with type "' . $strategy_type . '" is already registered'
);
}

$this->sessionStrategies[$strategy_type] = $session_strategy;
}

/**
Expand All @@ -51,14 +56,11 @@ public function setApplication(Application $application)
*/
public function createStrategy($strategy_type)
{
if ( $strategy_type == ISessionStrategyFactory::TYPE_ISOLATED ) {
return $this->application->getObject('isolated_session_strategy');
}
elseif ( $strategy_type == ISessionStrategyFactory::TYPE_SHARED ) {
return $this->application->getObject('shared_session_strategy');
if ( !isset($this->sessionStrategies[$strategy_type]) ) {
throw new \InvalidArgumentException('Session strategy type "' . $strategy_type . '" not registered');
}

throw new \InvalidArgumentException('Incorrect session strategy type');
return clone $this->sessionStrategies[$strategy_type];
}

}
2 changes: 0 additions & 2 deletions tests/aik099/PHPUnit/Integration/DIContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,6 @@ public function serviceDefinitionsDataProvider()
array('session_factory', 'aik099\\PHPUnit\\Session\\SessionFactory'),
array('session_strategy_factory', 'aik099\\PHPUnit\\Session\\SessionStrategyFactory'),
array('session_strategy_manager', 'aik099\\PHPUnit\\Session\\SessionStrategyManager'),
array('isolated_session_strategy', 'aik099\\PHPUnit\\Session\\IsolatedSessionStrategy'),
array('shared_session_strategy', 'aik099\\PHPUnit\\Session\\SharedSessionStrategy'),
array('remote_url', 'aik099\\PHPUnit\\RemoteCoverage\\RemoteUrl'),
array('remote_coverage_helper', 'aik099\\PHPUnit\\RemoteCoverage\\RemoteCoverageHelper'),
array('test_suite_factory', 'aik099\\PHPUnit\\TestSuite\\TestSuiteFactory'),
Expand Down
51 changes: 17 additions & 34 deletions tests/aik099/PHPUnit/Session/SessionStrategyFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@
namespace tests\aik099\PHPUnit\Session;


use aik099\PHPUnit\Session\ISessionStrategyFactory;
use aik099\PHPUnit\Session\ISessionStrategy;
use aik099\PHPUnit\Session\SessionStrategyFactory;
use tests\aik099\PHPUnit\TestCase\ApplicationAwareTestCase;
use Mockery as m;
use tests\aik099\PHPUnit\AbstractTestCase;
use Yoast\PHPUnitPolyfills\Polyfills\ExpectException;

class SessionStrategyFactoryTest extends ApplicationAwareTestCase
class SessionStrategyFactoryTest extends AbstractTestCase
{

use ExpectException;
Expand All @@ -33,49 +34,31 @@ class SessionStrategyFactoryTest extends ApplicationAwareTestCase
*/
protected function setUpTest()
{
parent::setUpTest();

$this->_factory = new SessionStrategyFactory();
$this->_factory->setApplication($this->application);
}

/**
* Test description.
*
* @param string $strategy_type Strategy type.
* @param string $service_id Service ID.
*
* @return void
* @dataProvider createStrategyDataProvider
*/
public function testCreateStrategySuccess($strategy_type, $service_id)
public function testRegisterSuccess()
{
$expected = 'OK';
$this->expectFactoryCall($service_id, $expected);
$this->assertEquals($expected, $this->_factory->createStrategy($strategy_type));
$session_strategy = m::mock(ISessionStrategy::class);
$this->_factory->register('strategy-type', $session_strategy);

$this->assertInstanceOf(ISessionStrategy::class, $this->_factory->createStrategy('strategy-type'));
}

/**
* Returns possible strategies.
*
* @return array
*/
public function createStrategyDataProvider()
public function testRegisterFailure()
{
return array(
array(ISessionStrategyFactory::TYPE_ISOLATED, 'isolated_session_strategy'),
array(ISessionStrategyFactory::TYPE_SHARED, 'shared_session_strategy'),
);
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Session strategy with type "strategy-type" is already registered');

$session_strategy = m::mock(ISessionStrategy::class);
$this->_factory->register('strategy-type', $session_strategy);
$this->_factory->register('strategy-type', $session_strategy);
}

/**
* Test description.
*
* @return void
*/
public function testCreateStrategyFailure()
{
$this->expectException('InvalidArgumentException');
$this->expectExceptionMessage('Session strategy type "wrong" not registered');

$this->_factory->createStrategy('wrong');
}
Expand Down

0 comments on commit 01c25b6

Please sign in to comment.