Skip to content

Commit

Permalink
Merge pull request #50 from aik099/42-refactor-browserconfigurationfa…
Browse files Browse the repository at this point in the history
…ctory-createapiclient

Move creation of API clients to corresponding browser configurations
  • Loading branch information
Alexander Obuhovich committed May 6, 2015
2 parents 4f04e4f + 4dff5b0 commit 62889c5
Show file tree
Hide file tree
Showing 12 changed files with 134 additions and 176 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,26 +39,16 @@ abstract class ApiBrowserConfiguration extends BrowserConfiguration
*/
const NAME_CAPABILITY = 'name';

/**
* Browser configuration factory.
*
* @var IBrowserConfigurationFactory
*/
protected $browserConfigurationFactory;

/**
* Creates browser configuration.
*
* @param EventDispatcherInterface $event_dispatcher Event dispatcher.
* @param DriverFactoryRegistry $driver_factory_registry Driver factory registry.
* @param IBrowserConfigurationFactory $browser_configuration_factory Browser configuration factory.
* @param EventDispatcherInterface $event_dispatcher Event dispatcher.
* @param DriverFactoryRegistry $driver_factory_registry Driver factory registry.
*/
public function __construct(
EventDispatcherInterface $event_dispatcher,
DriverFactoryRegistry $driver_factory_registry,
IBrowserConfigurationFactory $browser_configuration_factory
DriverFactoryRegistry $driver_factory_registry
) {
$this->browserConfigurationFactory = $browser_configuration_factory;
$this->defaults['driver'] = 'selenium2';
$this->defaults['apiUsername'] = '';
$this->defaults['apiKey'] = '';
Expand Down Expand Up @@ -225,10 +215,7 @@ public function onTestEnded(TestEndedEvent $event)
*
* @return IAPIClient
*/
public function getAPIClient()
{
return $this->browserConfigurationFactory->createAPIClient($this);
}
public abstract function getAPIClient();

/**
* Get Selenium2 current session id.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,7 @@
namespace aik099\PHPUnit\BrowserConfiguration;


use aik099\PHPUnit\APIClient\BrowserStackAPIClient;
use aik099\PHPUnit\APIClient\IAPIClient;
use aik099\PHPUnit\APIClient\SauceLabsAPIClient;
use aik099\PHPUnit\BrowserTestCase;
use WebDriver\SauceLabs\SauceRest;
use WebDriver\ServiceFactory;

/**
* Browser configuration factory.
Expand Down Expand Up @@ -90,30 +85,4 @@ protected function create($type)
return clone $this->browserConfigurations[$type];
}

/**
* Creates API client.
*
* @param BrowserConfiguration $browser Browser configuration.
*
* @return IAPIClient
* @throws \LogicException When unsupported browser configuration given.
*/
public function createAPIClient(BrowserConfiguration $browser)
{
if ( $browser instanceof SauceLabsBrowserConfiguration ) {
$sauce_rest = new SauceRest($browser->getApiUsername(), $browser->getApiKey());

return new SauceLabsAPIClient($sauce_rest);
}
elseif ( $browser instanceof BrowserStackBrowserConfiguration ) {
return new BrowserStackAPIClient(
$browser->getApiUsername(),
$browser->getApiKey(),
ServiceFactory::getInstance()->getService('service.curl')
);
}

throw new \LogicException('Unsupported browser configuration given');
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
namespace aik099\PHPUnit\BrowserConfiguration;


use aik099\PHPUnit\APIClient\BrowserStackAPIClient;
use aik099\PHPUnit\APIClient\IAPIClient;
use aik099\PHPUnit\Event\TestEvent;
use WebDriver\ServiceFactory;

/**
* Browser configuration tailored to use with "BrowserStack" service.
Expand All @@ -22,6 +25,20 @@ class BrowserStackBrowserConfiguration extends ApiBrowserConfiguration
{
const TYPE = 'browserstack';

/**
* Returns API class for service interaction.
*
* @return IAPIClient
*/
public function getAPIClient()
{
return new BrowserStackAPIClient(
$this->getApiUsername(),
$this->getApiKey(),
ServiceFactory::getInstance()->getService('service.curl')
);
}

/**
* Hook, called from "BrowserTestCase::setUp" method.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
namespace aik099\PHPUnit\BrowserConfiguration;


use aik099\PHPUnit\APIClient\IAPIClient;
use aik099\PHPUnit\BrowserTestCase;

/**
Expand Down Expand Up @@ -42,14 +41,4 @@ public function createBrowserConfiguration(array $config, BrowserTestCase $test_
*/
public function register(BrowserConfiguration $browser);

/**
* Creates API client.
*
* @param BrowserConfiguration $browser Browser configuration.
*
* @return IAPIClient
* @throws \LogicException When unsupported browser configuration given.
*/
public function createAPIClient(BrowserConfiguration $browser);

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
namespace aik099\PHPUnit\BrowserConfiguration;


use aik099\PHPUnit\APIClient\IAPIClient;
use aik099\PHPUnit\APIClient\SauceLabsAPIClient;
use aik099\PHPUnit\Event\TestEvent;
use WebDriver\SauceLabs\SauceRest;

/**
* Browser configuration tailored to use with "Sauce Labs" service.
Expand All @@ -22,6 +25,18 @@ class SauceLabsBrowserConfiguration extends ApiBrowserConfiguration
{
const TYPE = 'saucelabs';

/**
* Returns API class for service interaction.
*
* @return IAPIClient
*/
public function getAPIClient()
{
$sauce_rest = new SauceRest($this->getApiUsername(), $this->getApiKey());

return new SauceLabsAPIClient($sauce_rest);
}

/**
* Hook, called from "BrowserTestCase::setUp" method.
*
Expand Down
12 changes: 2 additions & 10 deletions library/aik099/PHPUnit/DIContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,18 +143,10 @@ public function __construct(array $values = array())
new BrowserConfiguration($c['event_dispatcher'], $c['driver_factory_registry'])
);
$browser_configuration_factory->register(
new SauceLabsBrowserConfiguration(
$c['event_dispatcher'],
$c['driver_factory_registry'],
$browser_configuration_factory
)
new SauceLabsBrowserConfiguration($c['event_dispatcher'], $c['driver_factory_registry'])
);
$browser_configuration_factory->register(
new BrowserStackBrowserConfiguration(
$c['event_dispatcher'],
$c['driver_factory_registry'],
$browser_configuration_factory
)
new BrowserStackBrowserConfiguration($c['event_dispatcher'], $c['driver_factory_registry'])
);

return $browser_configuration_factory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@
namespace tests\aik099\PHPUnit\BrowserConfiguration;


use aik099\PHPUnit\APIClient\IAPIClient;
use aik099\PHPUnit\BrowserConfiguration\ApiBrowserConfiguration;
use aik099\PHPUnit\BrowserConfiguration\IBrowserConfigurationFactory;
use aik099\PHPUnit\Event\TestEndedEvent;
use aik099\PHPUnit\Event\TestEvent;
use aik099\PHPUnit\Session\ISessionStrategyFactory;
use Mockery\MockInterface;
use Symfony\Component\EventDispatcher\EventDispatcher;
use aik099\PHPUnit\BrowserTestCase;
use Mockery as m;
Expand All @@ -29,25 +28,18 @@ abstract class ApiBrowserConfigurationTestCase extends BrowserConfigurationTest
const AUTOMATIC_TEST_NAME = 'AUTOMATIC';

/**
* Browser configuration class.
*
* @var string
*/
protected $browserConfigurationClass = '';

/**
* Browser configuration factory.
* Desired capabilities use to configure the tunnel.
*
* @var IBrowserConfigurationFactory|MockInterface
* @var array
*/
protected $browserConfigurationFactory;
protected $tunnelCapabilities = array();

/**
* Desired capabilities use to configure the tunnel.
* API client.
*
* @var array
* @var IAPIClient
*/
protected $tunnelCapabilities = array();
protected $apiClient;

/**
* Configures all tests.
Expand All @@ -60,9 +52,10 @@ protected function setUp()
$this->testsRequireSubscriber[] = 'testTestEndedEvent';
$this->testsRequireSubscriber[] = 'testTestEndedWithoutSession';
$this->testsRequireSubscriber[] = 'testTunnelIdentifier';
$this->browserConfigurationFactory = m::mock(
'aik099\\PHPUnit\\BrowserConfiguration\\IBrowserConfigurationFactory'
);

if ( $this->getName(false) === 'testTestEndedEvent' ) {
$this->mockBrowserMethods[] = 'getAPIClient';
}

parent::setUp();

Expand Down Expand Up @@ -133,7 +126,9 @@ public function testSetAPICorrect()
*/
public function testSetHostCorrect()
{
$browser = $this->createBrowserConfiguration(array(), false, true);
$browser = $this->createBrowserConfiguration();
$browser->setApiUsername('A');
$browser->setApiKey('B');

$this->assertSame($browser, $browser->setHost('EXAMPLE_HOST'));
$this->assertSame('A:[email protected]', $browser->getHost());
Expand All @@ -146,7 +141,10 @@ public function testSetHostCorrect()
*/
public function testSetPortCorrect()
{
$browser = $this->createBrowserConfiguration(array(), false, true);
$browser = $this->createBrowserConfiguration();
$browser->setApiUsername('A');
$browser->setApiKey('B');

$this->assertSame($browser, $browser->setPort(5555));
$this->assertSame(80, $browser->getPort());
}
Expand All @@ -158,7 +156,10 @@ public function testSetPortCorrect()
*/
public function testSetBrowserNameCorrect()
{
$browser = $this->createBrowserConfiguration(array(), false, true);
$browser = $this->createBrowserConfiguration();
$browser->setApiUsername('A');
$browser->setApiKey('B');

$this->assertSame($browser, $browser->setBrowserName(''));
$this->assertSame('chrome', $browser->getBrowserName());
}
Expand All @@ -174,7 +175,10 @@ public function testSetBrowserNameCorrect()
*/
public function testSetDesiredCapabilitiesCorrect(array $desired_capabilities = null, array $expected = null)
{
$browser = $this->createBrowserConfiguration(array(), false, true);
$browser = $this->createBrowserConfiguration();
$browser->setApiUsername('A');
$browser->setApiKey('B');

$this->assertSame($browser, $browser->setDesiredCapabilities($desired_capabilities));
$this->assertSame($expected, $browser->getDesiredCapabilities());
}
Expand Down Expand Up @@ -296,9 +300,7 @@ public function testTestEndedEvent($driver_type)
$test_case = $this->createTestCase('TEST_NAME');

$api_client = m::mock('aik099\\PHPUnit\\APIClient\\IAPIClient');
$this->browserConfigurationFactory->shouldReceive('createAPIClient')
->with($this->browser)
->andReturn($api_client);
$this->browser->shouldReceive('getAPIClient')->andReturn($api_client);

if ( $driver_type == 'selenium' ) {
$driver = m::mock('\\Behat\\Mink\\Driver\\Selenium2Driver');
Expand Down Expand Up @@ -471,33 +473,4 @@ public function tunnelIdentifierDataProvider()
);
}

/**
* Creates instance of browser configuration.
*
* @param array $aliases Aliases.
* @param boolean $add_subscriber Expect addition of subscriber to event dispatcher.
* @param boolean $with_api Include test API configuration.
*
* @return ApiBrowserConfiguration
*/
protected function createBrowserConfiguration(array $aliases = array(), $add_subscriber = false, $with_api = false)
{
/** @var ApiBrowserConfiguration $browser */
$browser = new $this->browserConfigurationClass(
$this->eventDispatcher,
$this->driverFactoryRegistry,
$this->browserConfigurationFactory
);
$browser->setAliases($aliases);

$this->eventDispatcher->shouldReceive('addSubscriber')->with($browser)->times($add_subscriber ? 1 : 0);

if ( $with_api ) {
$browser->setApiUsername('A');
$browser->setApiKey('B');
}

return $browser;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@

use aik099\PHPUnit\BrowserConfiguration\BrowserConfiguration;
use aik099\PHPUnit\BrowserConfiguration\BrowserConfigurationFactory;
use aik099\PHPUnit\BrowserConfiguration\BrowserStackBrowserConfiguration;
use aik099\PHPUnit\BrowserConfiguration\SauceLabsBrowserConfiguration;
use aik099\PHPUnit\MinkDriver\DriverFactoryRegistry;
use Mockery as m;
use tests\aik099\PHPUnit\TestCase\EventDispatcherAwareTestCase;
Expand Down Expand Up @@ -149,42 +147,6 @@ public function testRegisterFailure()
$this->_factory->register($browser_configuration);
}

/**
* Test description.
*
* @return void
*/
public function testCreateAPIClientSuccess()
{
$browser = new SauceLabsBrowserConfiguration(
$this->eventDispatcher,
$this->_driverFactoryRegistry,
$this->_factory
);
$api_client = $this->_factory->createAPIClient($browser);
$this->assertInstanceOf('aik099\\PHPUnit\\APIClient\\SauceLabsAPIClient', $api_client);

$browser = new BrowserStackBrowserConfiguration(
$this->eventDispatcher,
$this->_driverFactoryRegistry,
$this->_factory
);
$api_client = $this->_factory->createAPIClient($browser);
$this->assertInstanceOf('aik099\\PHPUnit\\APIClient\\BrowserStackAPIClient', $api_client);
}

/**
* Test description.
*
* @return void
* @expectedException \LogicException
*/
public function testCreateAPIClientFailure()
{
$browser = new BrowserConfiguration($this->eventDispatcher, $this->_driverFactoryRegistry);
$this->_factory->createAPIClient($browser);
}

/**
* Creates browser configuration.
*
Expand Down
Loading

0 comments on commit 62889c5

Please sign in to comment.