Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Single responsibility and new binary script #1

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
119 changes: 5 additions & 114 deletions ComposerListener.php
Original file line number Diff line number Diff line change
@@ -1,134 +1,25 @@
<?php

use CodeceptionSkeleton\Installer;
use Composer\Script\Event;

class ComposerListener
{
const DEFAULT_SELENIUM_VERSION = '3.10.0';
const DEFAULT_GECKODRIVER_VERSION = '0.19.1';
const DEFAULT_CHROMEDRIVER_VERSION = '2.35';
const SELENIUM_FILENAME = 'selenium-ss.jar';
const GECKODRIVER_FILENAME = 'geckodriver.tar.gz';
const CHROMEDRIVER_FILENAME = 'chromedriver.zip';

const SUPPORTED_OPERATIVE_SYSTEMS = [
'linux32',
'linux64',
'mac64',
'win32',
];

/**
* @param Event $event
*/
public static function downloadSelenium(Event $event)
{
$args = self::parseArguments($event->getArguments());
self::downloadFile(
self::SELENIUM_FILENAME,
self::buildSeleniumUrl(
$args['selenium'] ?? self::DEFAULT_SELENIUM_VERSION
)
);
$args = Installer::parseArguments($event->getArguments());
Installer::downloadSelenium($args);
}

/**
* @param Event $event
*/
public static function downloadDrivers(Event $event)
{
$args = self::parseArguments($event->getArguments());
$os = $args['os'] ?? self::determineOS();
$gdVersion = $args['gdVersion'] ?? self::DEFAULT_GECKODRIVER_VERSION;
$cdVersion = $args['cdVersion'] ?? self::DEFAULT_CHROMEDRIVER_VERSION;
if (!in_array($os, self::SUPPORTED_OPERATIVE_SYSTEMS)) {
$event->stopPropagation();
echo "Provided OS '$os' is not supported.\n";
exit(1);
}
self::downloadFile(
self::GECKODRIVER_FILENAME,
self::buildGeckoDriverUrl($os, $gdVersion)
);
self::downloadFile(
self::CHROMEDRIVER_FILENAME,
self::buildChromeDriverUrl($os, $cdVersion)
);
}

private static function determineOS(): string
{
return strtolower(php_uname('s')) . (8 * PHP_INT_SIZE);
}

private static function buildGeckoDriverUrl(
string $os,
string $version
): string {
return 'https://github.com/mozilla/geckodriver/releases/download/'
. "v{$version}/geckodriver-v{$version}-{$os}.tar.gz";
}

private static function buildChromeDriverUrl(
string $os,
string $version
): string {
return "https://chromedriver.storage.googleapis.com/{$version}/"
. "chromedriver_{$os}.zip";
}

private static function buildSeleniumUrl(string $version): string
{
return 'http://selenium-release.storage.googleapis.com/'
. substr($version, 0, strpos($version, '.', 2))
. "/selenium-server-standalone-{$version}.jar";
$args = Installer::parseArguments($event->getArguments());
Installer::downloadDrivers($args);
}

private static function downloadFile($filename, $url)
{
echo "Downloading $url into $filename.\n";
stream_copy_to_stream(
fopen($url, 'r'),
fopen($filename, 'w')
);
}

/**
* Arguments in string are parsed to key-pair values.
*
* ```php
* $args = self::parseArguments([
* 'opt=value',
* 'foo=bar'
* ]);
*
* print_r($args);
* ```
*
* The output will be:
*
* > Array
* > (
* > [opt] => value
* > [foo] => bar
* > )
*
* @param array[] $args
* @return array[]
*/
public static function parseArguments(array $args)
{
$parsed = [];
foreach ($args as $arg) {
$parse = explode('=', $arg);
if (!isset($parse[1])) {
$parsed[$parse[0]] = true;
} else {
$parsed[$parse[0]] = $parse[1];
}
}

return $parsed;
}

}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Skeleton to create codeception tests using for external projects using suites
Requirements
------------

- Php >=7.0
- PHP >=7.0
- Composer >=1.3
- Java JDK able to run selenium web drivers
[Official Documentation](http://www.seleniumhq.org/docs/03_webdriver.jsp#php)
Expand Down
92 changes: 92 additions & 0 deletions codecept-skeleton
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#!/usr/bin/php
<?php
require __DIR__ . '/vendor/autoload.php';

use CodeceptionSkeleton\Installer;
use CodeceptionSkeleton\WebFile\{
SeleniumDriver,
ChromeDriver,
GeckoDriver
};

$cmd = $argv[0];

if ($argc === 1 || in_array('-h', $argv)) {
usage();
exit(1);
}

if (!is_writable(__DIR__)) {
echo 'Required writing permissions';
exit(1);
}

if (in_array('-i', $argv)) {
$options = array_merge([
'os' => null,
'selenium' => null,
'gecko' => null,
'chrome' => null,
], getopt('', ['os:', 'selenium:', 'gecko:', 'chrome:']));

foreach ($options as $option => $value) {
switch ($option) {
case 'os':
Installer::downloadDrivers(array_merge([
'gdVersion' => $options['gecko'],
'cdVersion' => $options['chrome'],
], $options));

extractDrivers();
flushTempFiles();
break;

case 'selenium':
Installer::downloadSelenium($options);
break;
}
}

}

function extractDrivers()
{
$zip = new ZipArchive();
if ($zip->open(ChromeDriver::$fileName)) {
$zip->extractTo(__DIR__);
$zip->close();
}

$gzip = new PharData(__DIR__ .'/'. GeckoDriver::$fileName);
$gzip->extractTo(__DIR__);
}

function flushTempFiles() {
unlink(__DIR__.'/'.GeckoDriver::$fileName);
unlink(__DIR__.'/'.ChromeDriver::$fileName);
}

function usage()
{
global $cmd;

echo <<<EOL
\033[1;31mNAME \033[0m
\033[0;32m$cmd \033[0m- Skeleton to create codeception tests using for external projects

\033[1;31mSYNOPSIS \033[0m
\033[0;32m$cmd \033[0m-i [--os OS] [--selenium VERSION]

\033[1;31mDESCRIPTION \033[0m
\033[0;32m$cmd \033[0mwill install the php dependencies using composer and download "selenium standalone" and the supported
webdrivers in the files: selenium-ss.jar, geckodriver and chromedriver.

\033[1;31mEXAMPLES \033[0m
\033[0;32m$cmd \033[0m-i --os linux64
Download the drivers for GNU/Linux 32 version.

\033[0;32m$cmd \033[0m-i --selenium 3.11.0
Download Selenium Standalone Server 3.11.0.

EOL;
}
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,18 @@
"require": {
"php": ">=7.0.0",
"codeception/base": "^2.2.3",
"codeception/verify": "~0.3.1",
"codeception/verify": "^0.3.1",
"facebook/webdriver": "^1.0.1",
"flow/jsonpath": "^0.3.0",
"guzzlehttp/guzzle": ">=4.1.4 <7.0"
},
"autoload": {
"psr-4": { "CodeceptionSkeleton\\": "src/" },
"classmap": [
"ComposerListener.php"
]
},
"bin": ["codecept-skeleton"],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why put this as a binary file? it wont be executed on third party repos.

Copy link
Member

@neverabe neverabe May 8, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Chofoteddy answer back asap

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason: is to be able to use it as both (library or binary). The need arose because I wanted to use the functionality offered by the library, but I could not execute it from composer unless it was binary. Example: Codeception has its library and binary at the same time.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The deployer is only meant to be used in the skeleton, it wont work as a library.

So I dont understand how you intend to use the binary.

Codeception is a library.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, Codeception is a library, with a binary that supports the construction of the initial skeleton: the same purpose as this pull-request.

https://github.com/Codeception/Codeception/blob/2.4/composer.json

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok it seems the use case for bin files is not clear.

When someone adds a dependency on a project the 'require' section of composer.json its called a library, said library can include bin files to be executed by the project.

This repository is not a library and is never meant to be included as dependency in the 'require' section of any project. This repository is meant to be used as the skeleton for a project. So there is no use for any bin file here.

"scripts": {
"deploy": [
"ComposerListener::downloadDrivers",
Expand Down
Loading