Skip to content

Commit

Permalink
Merge pull request #1 from jbcr/main
Browse files Browse the repository at this point in the history
Fix Lambdatest error and add CS Fix
  • Loading branch information
macintoshplus authored Jun 3, 2021
2 parents c12b68a + f8866b7 commit 71d971e
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 17 deletions.
43 changes: 43 additions & 0 deletions .php_cs.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

return PhpCsFixer\Config::create()
->setRules([
'@Symfony' => true,
'@Symfony:risky' => true,
'@PSR2' => true,
'array_syntax' => [
'syntax' => 'short'
],
'combine_consecutive_unsets' => true,
'native_function_invocation' => [
'include' => [
'@compiler_optimized'
]
],
'no_extra_blank_lines' => [
'break',
'continue',
'extra',
'return',
'throw',
'use',
'parenthesis_brace_block',
'square_brace_block',
'curly_brace_block',
],
'yoda_style' => [
'always_move_variable' => false,
'equal' => false,
'identical' => false,
'less_and_greater' => false,
],
'ordered_class_elements' => true,
'ordered_imports' => true,
])
->setRiskyAllowed(true)
->setFinder(
PhpCsFixer\Finder::create()
->in('src')
->files()->name('*.php')
)
;
12 changes: 12 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,26 @@
"role": "owner"
}
],
"repositories": [
{
"type": "git",
"url": "https://github.com/jbcr/MinkFacebookWebDriver.git"
}
],
"require": {
"php": "^7.0",
"ext-json": "*",
"behat/mink-selenium2-driver": "^1.0||^2.0.x-dev",
"silverstripe/mink-facebook-web-driver": "1.x-dev",
"behat/mink-extension": "^2.0",
"behat/behat": "^3.0"
},
"autoload": {
"psr-4": {
"Macintoshplus\\Lambdatest\\": "src/"
}
},
"config": {
"sort-packages": true
}
}
39 changes: 22 additions & 17 deletions src/Driver/LambdatestFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

namespace Macintoshplus\Lambdatest\Driver;

use Behat\MinkExtension\ServiceContainer\Driver\Selenium2Factory;
use Macintoshplus\Lambdatest\Exception\LambdatestServiceException;
use Macintoshplus\Lambdatest\Exception\TooManyParallelExecutionException;
use SilverStripe\MinkFacebookWebDriver\FacebookFactory;
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
use WebDriver\Service\CurlService;

final class LambdatestFactory extends Selenium2Factory
final class LambdatestFactory extends FacebookFactory
{
/**
* {@inheritdoc}
Expand Down Expand Up @@ -34,29 +37,31 @@ public function buildDriver(array $config)
{
$envValues = getenv();
if ((!isset($config['user']) || !isset($config['key'])) && (!isset($envValues['LT_USERNAME']) || !isset($envValues['LT_USERKEY']))) {
throw new \Exception('Configure environment variable LT_USERNAME and LT_USERKEY with credential from Lambdatest');
throw new LambdatestServiceException('Configure environment variable LT_USERNAME and LT_USERKEY with credential from Lambdatest');
}

$user = urlencode($envValues['LT_USERNAME'] ?? $config['user'] ?? null);
$key = urlencode($envValues['LT_USERKEY'] ?? $config['key'] ?? null);

$wd_host = $config['wd_host'];
$config['capabilities']['extra_capabilities']['user'] = $user;
$config['capabilities']['extra_capabilities']['accessKey'] = $key;

$infos = parse_url($wd_host);
$curl = new CurlService();
$url = sprintf('https://%s:%[email protected]/automation/api/v1/org/concurrency', $user, $key);
list($result, $infos) = $curl->execute('GET', $url);

$wd_host = sprintf(
'%s://%s:%s@%s%s%s%s%s',
$infos['scheme'],
$user,
$key,
$infos['host'],
isset($infos['port']) ? ':'.$infos['port'] : '',
$infos['path'] ?? '',
isset($infos['query']) ? '?'.$infos['query'] : '',
isset($infos['fragment']) ? '#'.$infos['fragment'] : ''
);
//Example : {"data":{"created":0,"max_concurrency":1,"max_queue":150,"pqueued":0,"queued":0,"running":0},"status":"success"}
$data = json_decode($result, true);
if (json_last_error() !== \JSON_ERROR_NONE) {
throw new LambdatestServiceException('JSON Error on decode Lambdatest response : '.json_last_error().' '.json_last_error_msg());
}
if (isset($data['data']) === false || isset($data['data']['max_concurrency']) === false || isset($data['data']['running']) === false) {
throw new LambdatestServiceException('Concurency response is a valid JSON but does not contrains expected keys');
}

$config['wd_host'] = $wd_host;
if ((int) ($data['data']['max_concurrency']) <= (int) ($data['data']['running'])) {
throw new TooManyParallelExecutionException(sprintf('Unable to launch anothe parallel automation test. max concurency: %s, current running test: %s', $data['data']['max_concurrency'], $data['data']['running']));
}

return parent::buildDriver($config);
}
Expand Down
9 changes: 9 additions & 0 deletions src/Exception/LambdatestServiceException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace Macintoshplus\Lambdatest\Exception;

class LambdatestServiceException extends \Exception
{
}
9 changes: 9 additions & 0 deletions src/Exception/TooManyParallelExecutionException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace Macintoshplus\Lambdatest\Exception;

final class TooManyParallelExecutionException extends LambdatestServiceException
{
}

0 comments on commit 71d971e

Please sign in to comment.