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

Call to srand() with non-int fails with PHP < 8.3 #6717

Open
fulstadev opened this issue Nov 26, 2023 · 0 comments
Open

Call to srand() with non-int fails with PHP < 8.3 #6717

fulstadev opened this issue Nov 26, 2023 · 0 comments

Comments

@fulstadev
Copy link

fulstadev commented Nov 26, 2023

What are you trying to achieve?

I'm trying to run Codeception tests within PHP code, using

$codecept = new Codecept();

$codecept->run(
            suite : 'Acceptance',
            test  : 'SampleCest'
);

To run a Codeception Acceptance test at a specific moment (in between two integration tests currently written in PHPUnit).

What do you get instead?

If your PHP version is below 8.3, the call to srand($this->options['seed']); within Codecept::runSuite() fails, as seed is in this scenario not an int. As soon as you upgrade your PHP to 8.3, that error disappears and the Acceptance test completes successfully without any issue (supposingly due to the fact that the signature of srand() accepts null from upon PHP 8.3?).

As I'm new to Codeception, please let me know if I'm misunderstanding something / if it's better to dispatch Acceptance tests from Codeception in another way via PHP.

  • Codeception version: 5.0.12
  • PHP Version: 8.3.0 (now, failed with 8.2.12 in above-mentioned way)
  • Operating System: macOS
  • Installation type: Composer
  • List of installed packages (composer show)
  • Suite configuration:
# Codeception Test Suite Configuration
#
# Suite for acceptance tests.
# Perform tests in browser using the WebDriver or PhpBrowser.
# If you need both WebDriver and PHPBrowser tests - create a separate suite.

actor: AcceptanceTester
modules:
    enabled:
        - WebDriver:
              url: 'https://{uname}:{pword}@mydom.org'
              browser: chrome
# add Codeception\Step\Retry trait to AcceptanceTester to enable retries
step_decorators:
    - Codeception\Step\ConditionalAssertion
    - Codeception\Step\TryTo
    - Codeception\Step\Retry

When running the code with PHP 8.3, I'm getting the PHP warning:

1) /.../vendor/codeception/codeception/src/Codeception/Codecept.php:257
Undefined array key "seed"

So it seems to be treated as warning instead of an error now, but at least the test executes. Am I misunderstanding about how to properly execute a Codeception test within PHP code?

POTENTIAL FIX

I've been digging within the docs of the concerned Codecept and related file, and found that srand() is used to initiate the pseudo-random number generator within Codeception. I've not researched why this is done, so my approach may be the wrong solution. So before posting this as an answer, I'd like to know if it's a proper solution.

If I'm properly informed and srand() behaves in PHP as it does in C, srand() will seed the pseudo-random number generator with the current timestamp in PHP if its default value (in PHP that's 0) is provided to its call. According to the Codecept constructor, the options property of a new instance gets initiated via:

$this->options = $this->mergeOptions($options);

The line of code causing the failure when running the Acceptance test via PHP, using the Codecept class, then is:

srand($this->options['seed']);

So I thought by simply providing the default solution for srand() within the Codecept constructor, this should solve the problem. Hence I replaced the line of my code above:

$codecept = new Codecept();

with:

$codecept = new Codecept(['seed' => 0 ]);

This eliminated thrown warnings / errors, but I'd still like to know if it's the proper way to go to dispatch a Codeception acceptance test from within PHP.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant