Skip to content

Commit

Permalink
Use package events instead of command events (#22)
Browse files Browse the repository at this point in the history
* Use package events instead of command events

* Improved formatting

* Ignore composer.lock
  • Loading branch information
Naktibalda authored and DavertMik committed Sep 16, 2016
1 parent 0d6d9a2 commit d71e5d2
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 20 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.idea
composer.phar
vendor/
RoboFile.php
RoboFile.php
composer.lock
42 changes: 28 additions & 14 deletions Installer.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,17 @@ public function activate(Composer $composer, IOInterface $io)

public static function getSubscribedEvents()
{
return array(
ScriptEvents::POST_INSTALL_CMD => array(
array('copyC3', 0)
),
ScriptEvents::POST_UPDATE_CMD => array(
array('askForUpdate', 0)
)
);
return [
ScriptEvents::POST_PACKAGE_INSTALL => [
['copyC3', 0]
],
ScriptEvents::POST_PACKAGE_UPDATE => [
['askForUpdate', 0]
],
ScriptEvents::POST_PACKAGE_UNINSTALL => [
['deleteC3', 0]
]
];
}

public static function copyC3ToRoot(Event $event)
Expand All @@ -45,25 +48,36 @@ public function copyC3()
}

$this->io->write("<comment>[codeception/c3]</comment> Copying c3.php to the root of your project...");
copy(__DIR__.DIRECTORY_SEPARATOR.'c3.php', getcwd().DIRECTORY_SEPARATOR.'c3.php');
copy(__DIR__ . DIRECTORY_SEPARATOR . 'c3.php', getcwd() . DIRECTORY_SEPARATOR.'c3.php');
$this->io->write("<comment>[codeception/c3]</comment> Include c3.php into index.php in order to collect codecoverage from server scripts");
}

public function askForUpdate()
{
if ($this->c3NotChanged()) return;
if (file_exists(getcwd().DIRECTORY_SEPARATOR.'c3.php')) {
if ($this->c3NotChanged()) {
return;
}
if (file_exists(getcwd() . DIRECTORY_SEPARATOR . 'c3.php')) {
$replace = $this->io->askConfirmation("<warning>c3.php has changed</warning> Do you want to replace c3.php with latest version?", false);
if (!$replace) return;
if (!$replace) {
return;
}
}
$this->copyC3();
}

private function c3NotChanged()
{
return file_exists(getcwd().DIRECTORY_SEPARATOR.'c3.php') &&
md5_file(__DIR__.DIRECTORY_SEPARATOR.'c3.php') === md5_file(getcwd().DIRECTORY_SEPARATOR.'c3.php');
return file_exists(getcwd() . DIRECTORY_SEPARATOR . 'c3.php') &&
md5_file(__DIR__ . DIRECTORY_SEPARATOR . 'c3.php') === md5_file(getcwd() . DIRECTORY_SEPARATOR . 'c3.php');
}

public function deleteC3()
{
if (file_exists(getcwd() . DIRECTORY_SEPARATOR . 'c3.php')) {
$this->io->write("<comment>[codeception/c3]</comment> Deleting c3.php from the root of your project...");
unlink(getcwd() . DIRECTORY_SEPARATOR . 'c3.php');
}
}
}

10 changes: 5 additions & 5 deletions c3.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

if ($cookie) {
foreach ($cookie as $key => $value) {
$_SERVER["HTTP_X_CODECEPTION_".strtoupper($key)] = $value;
$_SERVER["HTTP_X_CODECEPTION_" . strtoupper($key)] = $value;
}
}
}
Expand All @@ -38,7 +38,7 @@ function __c3_error($message)
C3_CODECOVERAGE_MEDIATE_STORAGE . DIRECTORY_SEPARATOR . 'error.txt';
if (is_writable($errorLogFile)) {
file_put_contents($errorLogFile, $message);
}else{
} else {
$message = "Could not write error to log file ($errorLogFile), original message: $message";
}
if (!headers_sent()) {
Expand All @@ -61,7 +61,7 @@ class_alias('SebastianBergmann\CodeCoverage\Exception', 'PHP_CodeCoverage_Except
// Autoload Codeception classes
if (!class_exists('\\Codeception\\Codecept')) {
if (file_exists(__DIR__ . '/codecept.phar')) {
require_once 'phar://'.__DIR__ . '/codecept.phar/autoload.php';
require_once 'phar://' . __DIR__ . '/codecept.phar/autoload.php';
} elseif (stream_resolve_include_path(__DIR__ . '/vendor/autoload.php')) {
require_once __DIR__ . '/vendor/autoload.php';
// Required to load some methods only available at codeception/autoload.php
Expand All @@ -71,7 +71,7 @@ class_alias('SebastianBergmann\CodeCoverage\Exception', 'PHP_CodeCoverage_Except
} elseif (stream_resolve_include_path('Codeception/autoload.php')) {
require_once 'Codeception/autoload.php';
} else {
__c3_error('Codeception is not loaded. Please check that either PHAR or Composer or PEAR package can be used');
__c3_error('Codeception is not loaded. Please check that either PHAR or Composer package can be used');
}
}

Expand Down Expand Up @@ -266,7 +266,7 @@ function () use ($codeCoverage, $current_report) {

$codeCoverage->stop();
if (!file_exists(dirname($current_report))) { // verify directory exists
if(!mkdir(dirname($current_report), 0777, true)){
if (!mkdir(dirname($current_report), 0777, true)) {
__c3_error("Can't write CodeCoverage report into $current_report");
}
}
Expand Down

0 comments on commit d71e5d2

Please sign in to comment.