opencart-module/tests/RoboFile.php

183 lines
5.9 KiB
PHP
Raw Normal View History

2018-06-13 16:13:08 +03:00
<?php
2020-02-17 17:56:13 +03:00
require_once(__DIR__.'/../vendor/autoload.php');
2020-02-17 17:46:01 +03:00
if (file_exists(__DIR__ . '/.env')) {
2018-06-13 16:13:08 +03:00
Dotenv::load(__DIR__);
}
class RoboFile extends \Robo\Tasks
{
use \Robo\Task\Development\loadTasks;
use \Robo\Common\TaskIO;
/**
* @var array
*/
private $opencart_config;
/**
* @var int
*/
private $server_port = 80;
/**
* @var string
*/
private $server_url = 'http://localhost';
2020-02-17 23:07:01 +03:00
private $root_dir = __DIR__ . '/../';
2018-06-13 16:13:08 +03:00
public function __construct()
{
2018-06-13 17:18:06 +03:00
if ($_ENV) {
foreach ($_ENV as $option => $value) {
if (substr($option, 0, 3) === 'OC_') {
$option = strtolower(substr($option, 3));
$this->opencart_config[$option] = $value;
} elseif ($option === 'SERVER_PORT') {
$this->server_port = (int) $value;
} elseif ($option === 'SERVER_URL') {
$this->server_url = $value;
}
2018-06-13 16:13:08 +03:00
}
2018-06-13 17:18:06 +03:00
} else {
$this->opencart_config = [
'db_hostname' => getenv('OC_DB_HOSTNAME'),
'db_username' => getenv('OC_DB_USERNAME'),
'db_password' => getenv('OC_DB_PASSWORD'),
'db_database' => getenv('OC_DB_DATABASE'),
'db_driver' => getenv('OC_DB_DRIVER'),
'username' => getenv('OC_USERNAME'),
'password' => getenv('OC_PASSWORD'),
'email' => getenv('OC_EMAIL')
];
2018-06-13 16:13:08 +03:00
}
$this->opencart_config['http_server'] = $this->server_url.':'.$this->server_port.'/';
$required = array('db_username', 'password', 'email');
$missing = array();
foreach ($required as $config) {
if (empty($this->opencart_config[$config])) {
$missing[] = 'OC_'.strtoupper($config);
}
}
if (!empty($missing)) {
$this->printTaskError("<error> Missing ".implode(', ', $missing));
$this->printTaskError("<error> See .env.sample ");
die();
}
}
public function opencartSetup()
{
$this->taskDeleteDir('www')->run();
$this->taskFileSystemStack()
2020-02-17 23:07:01 +03:00
->mirror(
$this->root_dir . 'vendor/opencart/opencart/upload',
$this->root_dir . 'www'
)
->copy(
$this->root_dir . 'vendor/beyondit/opencart-test-suite/src/upload/system/config/test-config.php',
$this->root_dir . 'www/system/config/test-config.php'
)
->copy(
$this->root_dir . 'vendor/beyondit/opencart-test-suite/src/upload/catalog/controller/startup/test_startup.php',
$this->root_dir . 'www/catalog/controller/startup/test_startup.php'
)
->chmod($this->root_dir . 'www', 0777, 0000, true)
2018-06-13 16:13:08 +03:00
->run();
// Create new database, drop if exists already
try {
$conn = new PDO("mysql:host=".$this->opencart_config['db_hostname'], $this->opencart_config['db_username'], $this->opencart_config['db_password']);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$conn->exec("DROP DATABASE IF EXISTS `" . $this->opencart_config['db_database'] . "`");
$conn->exec("CREATE DATABASE `" . $this->opencart_config['db_database'] . "`");
}
catch(PDOException $e)
{
$this->printTaskError("<error> Could not connect ot database...");
}
2020-02-17 23:07:01 +03:00
$install = $this->taskExec('php')->arg($this->root_dir . 'www/install/cli_install.php')->arg('install');
2018-06-13 16:13:08 +03:00
foreach ($this->opencart_config as $option => $value) {
$install->option($option, $value);
}
$install->run();
2020-02-17 23:07:01 +03:00
$this->taskDeleteDir($this->root_dir . 'www/install')->run();
2018-06-13 16:13:08 +03:00
$this->restoreSampleData($conn);
$conn = null;
}
public function opencartRun()
{
$this->taskServer($this->server_port)
2020-02-17 23:07:01 +03:00
->dir($this->root_dir . 'www')
2018-06-13 16:13:08 +03:00
->run();
}
public function projectDeploy()
{
$this->taskFileSystemStack()
2020-02-17 23:07:01 +03:00
->mirror($this->root_dir . 'src/upload', $this->root_dir . 'www')
2018-06-13 16:13:08 +03:00
// ->copy('src/install.xml','www/system/install.ocmod.xml') if exist modification for OCMOD
->run();
}
public function projectWatch()
{
$this->projectDeploy();
$this->taskWatch()
2020-02-17 23:07:01 +03:00
->monitor($this->root_dir . 'composer.json', function () {
2018-06-13 16:13:08 +03:00
$this->taskComposerUpdate()->run();
$this->projectDeploy();
2020-02-17 23:07:01 +03:00
})->monitor($this->root_dir . 'src/', function () {
2018-06-13 16:13:08 +03:00
$this->projectDeploy();
})->run();
}
public function projectPackage()
{
$this->taskDeleteDir('target')->run();
$this->taskFileSystemStack()->mkdir('target')->run();
$zip = new ZipArchive();
$filename = "target/build.ocmod.zip";
if ($zip->open($filename, ZipArchive::CREATE)!==TRUE) {
$this->printTaskError("<error> Could not create ZipArchive");
exit();
}
$iterator = new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator("src", \RecursiveDirectoryIterator::SKIP_DOTS), \RecursiveIteratorIterator::CHILD_FIRST
);
foreach ($iterator as $file) {
if ($file->isFile() && $file->isReadable()) {
$zip->addFile($file->getPathname(),substr($file->getPathname(),4));
}
}
$zip->close();
}
private function restoreSampleData($conn)
{
$sql = file_get_contents('tests/opencart_sample_data.sql');
$conn->exec("USE " . $this->opencart_config['db_database']);
foreach (explode(";\n", $sql) as $sql) {
$sql = trim($sql);
if ($sql) {
$conn->exec($sql);
}
}
}
}