-
Notifications
You must be signed in to change notification settings - Fork 46
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
Move C3 code to separate class (2023) #85
base: main
Are you sure you want to change the base?
Conversation
Basic usage: ``` require __DIR__ . '/vendor/codeception/c3/c3.php'; $c3 = new Codeception\C3(__DIR__); $c3->run(); ```
@Naktibalda Do you need any help getting this reviewed? I think it would be really benificial for the maintainability of this project if it becomes a composer requirement through this PR. |
@TavoNiievez Is there anyone else that can review this? |
@marcovtwout looks good (haven't tried it), you should resolve conflicts and update this line for the tests here with the latest PHP versions, and drop the ones that are no longer supported https://github.com/Codeception/c3/blob/main/.github/workflows/main.yml#L11 |
@delboy1978uk That line is not part of this PR, but I can rebase and fix conflicts again and again.. but there is no point until somebody reviews this first. So can somebody please review this? :) |
|
||
if (isset($_COOKIE['CODECEPTION_CODECOVERAGE'])) { | ||
$cookie = json_decode($_COOKIE['CODECEPTION_CODECOVERAGE'], true); | ||
// $_SERVER['HTTP_X_CODECEPTION_CODECOVERAGE_DEBUG'] = 1; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess this one was there as a configuration option. How to configure it now if the file isn't yours?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This (commented) code was only moved from above the use-statements to below. Functionality is unchanged.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've given a lot of detailed comments and they could lead to the following conclusion:
- This new release will be a backwards compatibility break
- We could use this to drop support for older versions of Codeception / PHPUnit
Most if this code is very old and therefore very messy. It could be immensely simplified:
- Remove constants, use constructor arguments for configuration
- Remove workarounds for class loading, it's 2023, we use composer autoloader, with additional support for Codeception PHAR.
Additionally, I'm not sure why we need to load codeception configuration at all, or need codeception classes for that matter. Couldn't we just rewrite this as a thin wrapper on the code coverage package which has been improved since C3 was originally written?
public function setAutoloadRootDir($dir) | ||
{ | ||
$this->autoloadRootDir = $dir; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think the autoloadRootDir should be mutable. I'm not even sure it's needed at all in this class.
ini_set('memory_limit', $requiredMemory); | ||
} | ||
// Autoload Codeception classes | ||
if (!class_exists('\\Codeception\\Codecept') || !function_exists('codecept_is_path_absolute')) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use ::class
(and add import at top)
if (!class_exists('\\Codeception\\Codecept') || !function_exists('codecept_is_path_absolute')) { | |
if (!class_exists(Codecept::class) || !function_exists('codecept_is_path_absolute')) { |
} elseif (stream_resolve_include_path($this->autoloadRootDir . '/vendor/autoload.php')) { | ||
require_once $this->autoloadRootDir . '/vendor/autoload.php'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should never include the composer autoloader, since it's responsible for loading our class. So I'd say remvoe this code path.
The only thing we should manually autoload are:
- Codeception from phar file
if (stream_resolve_include_path($this->autoloadRootDir . '/vendor/codeception/codeception/autoload.php')) { | ||
require_once $this->autoloadRootDir . '/vendor/codeception/codeception/autoload.php'; | ||
} | ||
} elseif (stream_resolve_include_path('Codeception/autoload.php')) { | ||
require_once 'Codeception/autoload.php'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From inspecting that file I don't think we actually need to include it: https://github.com/Codeception/Codeception/blob/5.0/autoload.php
define('C3_CODECOVERAGE_PROJECT_ROOT', Codeception\Configuration::projectDir()); | ||
define('C3_CODECOVERAGE_TESTNAME', $_SERVER['HTTP_X_CODECEPTION_CODECOVERAGE']); | ||
// phpunit codecoverage shimming | ||
if (!class_exists('PHP_CodeCoverage') and class_exists('SebastianBergmann\CodeCoverage\CodeCoverage')) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This class no longer exist (it was removed ~2012) from PHPUnit. I propose not supporting it in this new version of C3. Just remove this shimming.
if (isset($_SERVER['HTTP_X_CODECEPTION_CODECOVERAGE_CONFIG'])) { | ||
$configFile = $this->configDir . DIRECTORY_SEPARATOR . $_SERVER['HTTP_X_CODECEPTION_CODECOVERAGE_CONFIG']; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm personally not a fan of having this artifact in the code. We should not be dynamically loading user supplied paths.
I imagine this is a very niche cache where you want to dynamically configure it for C3 based on the request.
$requestedC3Report = (strpos($_SERVER['REQUEST_URI'], 'c3/report') !== false); | ||
function __c3_clear() | ||
{ | ||
\Codeception\Util\FileSystem::doEmptyDir(C3_CODECOVERAGE_MEDIATE_STORAGE); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should not be using this constant.
Instead:
- define it as a constructor argument
- store it in a private property
- in the constructor check if the constant is defined and if so use it to initialize the private property
- remove all references to this constant
if ($cookie) { | ||
foreach ($cookie as $key => $value) { | ||
if (!empty($value)) { | ||
$_SERVER["HTTP_X_CODECEPTION_" . strtoupper($key)] = $value; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't use the global $_SERVER
instead add a constructor param $server
.
This makes the dependencies clear from the call signature:
(new C3($_SERVER, $_REQUEST, $argument1, ...))->run();
@SamMousa Thanks for your review! I agree exactly with your conclusions, but I have split them up into two parts for the sake of easier reviewing and continued development:
|
Cool, go ahead with step 1 then. For the new implementation I'd say have a look at #93! If you want to give hte implementation a try go ahead. |
This PR is a first step in improving #40 applying the same solution as #72
Instead of having a copy of c3.php in the project root it can be included from any location.
Basic usage:
I only made the minimal changes to use this concept. Best to disable whitespace comparison when reviewing this PR.
There's more to be improved after this:
__c3_
-functions part of the classBut please review this PR first and decide if this could start a new v3 branch. If so, I'd be happy to submit further improvement PR's on top of that (based on main...marcovtwout:c3:class_based_c3_improvements).