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

php 8.x compatibility #3

Closed
stosef opened this issue Mar 26, 2024 · 2 comments · Fixed by #4
Closed

php 8.x compatibility #3

stosef opened this issue Mar 26, 2024 · 2 comments · Fixed by #4
Assignees

Comments

@stosef
Copy link

stosef commented Mar 26, 2024

No description provided.

@stosef stosef self-assigned this Mar 26, 2024
stosef pushed a commit that referenced this issue Mar 27, 2024
stosef pushed a commit that referenced this issue Mar 27, 2024
stosef pushed a commit that referenced this issue Mar 27, 2024
stosef pushed a commit that referenced this issue Mar 27, 2024
stosef pushed a commit that referenced this issue Apr 3, 2024
stosef pushed a commit that referenced this issue Apr 3, 2024
…ponse|null but return statement is missing" #3
stosef pushed a commit that referenced this issue Apr 3, 2024
…d>>) does not accept default value of type array<int, string>" #3
@stosef stosef linked a pull request Apr 3, 2024 that will close this issue
stosef pushed a commit that referenced this issue Apr 3, 2024
stosef pushed a commit that referenced this issue Apr 3, 2024
stosef pushed a commit that referenced this issue Apr 8, 2024
stosef pushed a commit that referenced this issue May 28, 2024
stosef pushed a commit that referenced this issue May 28, 2024
stosef pushed a commit that referenced this issue May 28, 2024
stosef pushed a commit that referenced this issue May 28, 2024
@stosef
Copy link
Author

stosef commented May 29, 2024

Continue working on fixing PHP Unit Tests

Taking a look into to readme to find out about new way of launching a unit test. Inspecting the new /Test/phpunit.sh file. I see the shell variable $IFS that is not defined in the phpunit.sh file. Let's see what google has to say about it.

$IFS is the "Internal field separator", a special shell variable.

Continue to fix test testSessionStartupData in FilterComponentTest.php (Test/phpunit.sh Test/Case/Controller/Component/FilterComponentTest.php --filter testSessionStartupData)

Current run shows:

There was 1 failure:

1) FilterComponentTest::testSessionStartupData
InvalidArgumentException was not thrown

/cakephp-filter-plugin/Test/Case/Controller/Component/FilterComponentTest.php:216

FAILURES!
Tests: 1, Assertions: 2, Failures: 1.

This is the version of the file I'm working on: https://github.com/the-kbA-team/cakephp-filter-plugin/blob/34c653fa21949a24daeb2cd9f639cff8baa90d7b/Test/Case/Controller/Component/FilterComponentTest.php

/**
     * Test loading filter data from session (both full and empty).
     */
    public function testSessionStartupData(): void
    {
        $testSettings = array(
            'index' => array(
                'Document' => array(
                    'Document.title' => array('type' => 'text')
                ),
                'FakeNonexistant' => array(
                    'drink' => array('type' => 'select')
                )
            )
        );
        $this->Controller->filters = $testSettings;

        $sessionKey = sprintf('FilterPlugin.Filters.%s.%s', $this->Controller->name, $this->Controller->action);

        $filterValues = array();
        $this->Controller->Session->write($sessionKey, $filterValues);
        try {
            $this->Controller->Components->trigger('initialize', array($this->Controller));
            $this->fail('InvalidArgumentException was not thrown');
        } catch (InvalidArgumentException $e1) {
            $this->assertSame('Filter model not found: FakeNonexistant', $e1->getMessage());
        }

        try {
            $this->Controller->Components->trigger('startup', array($this->Controller));
            $this->fail('InvalidArgumentException was not thrown');
        } catch (InvalidArgumentException $e2) {
            $this->assertSame('xxxFilter model not found: FakeNonexistant', $e2->getMessage());
        }
        $actualFilterValues = $this->Controller->Document->getFilterValues();
        $this->assertEquals(
            $filterValues,
            $actualFilterValues[$this->Controller->Document->alias]
        );

        $filterValues = array('Document' => array('title' => 'in'));
        $this->Controller->Session->write($sessionKey, $filterValues);

        $this->Controller->Components->trigger('startup', array($this->Controller));
        $actualFilterValues = $this->Controller->Document->getFilterValues();
        $this->assertEquals(
            $filterValues,
            $actualFilterValues[$this->Controller->Document->alias]
        );

        $this->Controller->Session->delete($sessionKey);
    }

This unit test works with session data and I just found out that the values are not saved.

I did

var_dump($this->Controller->Session->write($sessionKey, $filterValues));

and the resulting output on the console was

bool(false)

which means that the data could not be saved as seen in CakeSession.php:

/**
 * Writes value to given session variable name.
 *
 * @param string|array $name Name of variable
 * @param mixed $value Value to write
 * @return bool True if the write was successful, false if the write failed
 */
	public static function write($name, $value = null) {
		if (!static::start()) {
			return false;
		}

		$write = $name;
		if (!is_array($name)) {
			$write = array($name => $value);
		}
		foreach ($write as $key => $val) {
			static::_overwrite($_SESSION, Hash::insert($_SESSION, $key, $val));
			if (Hash::get($_SESSION, $key) !== $val) {
				return false;
			}
		}
		return true;
	}

At this point manually debugging gets time consuming annoying and I think about what's necessary to be able to use PHPStorm for debugging.

I try to talk to Martin Mitterhauser to get some ideas. Didn't reach him.

Had the idea to look into the CakePHP Cookbook and I found something!

Running tests that use sessions

When running tests on the command line that use sessions you’ll need to include the --stderr flag. Failing to do so will cause sessions to not work. PHPUnit outputs test progress to stdout by default, this causes PHP to assume that headers have been sent which prevents sessions from starting. By switching PHPUnit to output on stderr, this issue is avoided.

I looked into the Github testing workflow of the CakePHP2 Fork we are using. The call to PHPUnit looks like this:

./vendors/bin/phpunit --stderr --verbose lib/Cake/Test/Case/AllTestsTest.php

It looks like that this argument can also be defined in the phpunit.xml configuration: https://docs.phpunit.de/en/9.6/configuration.html#the-stderr-attribute

I added the stderr attribute to our phpunit.xml.dist: the-kbA-team/cake2-app-template@b99bc71

Yes!! Session works now:

var_dump($this->Controller->Session->write($sessionKey, $filterValues));

and the resulting output on the console was

bool(true)

I was able to fix the testSessionStartupData() as far as I understand what should be tested (1e27ad6)

Current run shows (Test/phpunit.sh Test/Case/Controller/Component/FilterComponentTest.php --filter testSessionStartupData):

PHPUnit 9.6.19 by Sebastian Bergmann and contributors.

Runtime:       PHP 8.1.2-1ubuntu2.17
Configuration: /app/phpunit.xml.dist

.                                                                   1 / 1 (100%)

Time: 00:00.162, Memory: 10.00 MB

OK (1 test, 3 assertions)

Working on testNoModelFound() next (Test/phpunit.sh Test/Case/Controller/Component/FilterComponentTest.php --filter testNoModelFound)

Current output is:

PHPUnit 9.6.19 by Sebastian Bergmann and contributors.

Runtime:       PHP 8.1.2-1ubuntu2.17
Configuration: /app/phpunit.xml.dist

E                                                                   1 / 1 (100%)

Time: 00:00.126, Memory: 8.00 MB

There was 1 error:

1) FilterComponentTest::testNoModelFound
Error: Call to undefined method FilterComponentTest::setExpectedException()

/cakephp-filter-plugin/Test/Case/Controller/Component/FilterComponentTest.php:304

ERRORS!
Tests: 1, Assertions: 0, Errors: 1.

I was able to fix testNoModelFound() as far as I understand what should be tested (ce74bd0)

Current run shows (Test/phpunit.sh Test/Case/Controller/Component/FilterComponentTest.php --filter testSessionStartupData):

PHPUnit 9.6.19 by Sebastian Bergmann and contributors.

Runtime:       PHP 8.1.2-1ubuntu2.17
Configuration: /app/phpunit.xml.dist

.                                                                   1 / 1 (100%)

Time: 00:00.147, Memory: 10.00 MB

OK (1 test, 3 assertions)

Now there is only 1 assertion that fails in FilterComponentTest.php:

➜  cakephp-filter-plugin git:(3-php-8x-compatibility) ✗ Test/phpunit.sh Test/Case/Controller/Component/FilterComponentTest.php                          
staging: Pulling from devkba/cake2-app-template
Digest: sha256:e50f8ea6ad911cbd66af28505a3a1d612837f933bee675e1ef080d013e6d72e0
Status: Image is up to date for devkba/cake2-app-template:staging
docker.io/devkba/cake2-app-template:staging
 * Starting MySQL database server mysqld                                                                                                                                               [ OK ] 
./composer.json has been updated
Running composer update kba-team/cakephp-filter-plugin
Loading composer repositories with package information
Updating dependencies
Lock file operations: 1 install, 0 updates, 0 removals
  - Locking kba-team/cakephp-filter-plugin (dev-main)
Writing lock file
Installing dependencies from lock file (including require-dev)
Package operations: 1 install, 0 updates, 0 removals
  - Installing kba-team/cakephp-filter-plugin (dev-main): Symlinking from /cakephp-filter-plugin
Package ad7six/dsn is abandoned, you should avoid using it. No replacement was suggested.
Generating autoload files
27 packages you are using are looking for funding.
Use the `composer fund` command to find out more!
No security vulnerability advisories found.
PHPUnit 9.6.19 by Sebastian Bergmann and contributors.

Runtime:       PHP 8.1.2-1ubuntu2.17
Configuration: /app/phpunit.xml.dist

..........F.....                                                  16 / 16 (100%)

Time: 00:00.612, Memory: 10.00 MB

There was 1 failure:

1) FilterComponentTest::testCustomSelector
null does not match expected type "array".

/cakephp-filter-plugin/Test/Case/Controller/Component/FilterComponentTest.php:464

FAILURES!
Tests: 16, Assertions: 25, Failures: 1.
 * Stopping MySQL database server mysqld 

The unit test fails because FilterComponent->startup() returns early here


It has to be investigated further what is wrong here, it should not return early.

That's it for today.

@gregor-j See above what I did today

@stosef
Copy link
Author

stosef commented Jun 26, 2024

Continuing where I left off, the unit test for FilterComponent is not done. The unit test "testCustomSelector" still fails.

Test/phpunit.sh Test/Case/Controller/Component/FilterComponentTest.php --filter testCustomSelector

It seems that there must be a model DocumentCategory with the method customSelector, this is not the case atm.

I found the DocumentCategory here: https://github.com/the-kbA-team/cakephp-filter-plugin/blob/ce74bd0571f1b6ce9ddb09307165bc676296133e/Test/Case/MockObjects/DocumentCategory.php

Let's see what I have to do to get unit test to use this class.

stosef pushed a commit that referenced this issue Jul 3, 2024
Fixing them would not improve code quality
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

Successfully merging a pull request may close this issue.

1 participant