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

Fix for issue #17901 - improvements in fragment handling and unittesting #17954

Open
wants to merge 2 commits into
base: 5.next
Choose a base branch
from

Conversation

BEFICENT
Copy link

@BEFICENT BEFICENT commented Oct 8, 2024

Description:

This pull request addresses issue #17901 by improving the handling of fragments in the View::fragment() method. Specifically, it ensures proper functionality when storing, updating, and clearing fragment data. We focused on refining the method and adding unit tests, without creating actual fragment templates.

Reason for Changes:

The changes are aimed at fixing inconsistencies in how fragment data was being managed. The updated View::fragment() method now better aligns with the element() method’s behavior, making fragment handling more reliable and predictable.

We didn't create actual fragment templates as part of these changes. Instead, we used mocking in the unit tests to simulate the behavior of the fragments and the element() method. This allowed us to thoroughly test the fragment functionality without needing real fragment files, which saved time and kept the tests flexible.

Tests:

  • Unit tests were added to verify that fragment data can be stored, updated, and cleared as expected.
  • The element() method was mocked in the tests to simulate fragment usage dynamically.
  • These tests ensured the functionality works correctly, even without real fragment templates.

@BEFICENT BEFICENT changed the base branch from 5.x to 5.next October 8, 2024 00:30
@dereuromark dereuromark added this to the 5.2.0 milestone Oct 8, 2024
if ($name[0] == '/') {
$path = "..{$name}";
} else {
$path = "..{$this->name}/{$name}";
Copy link
Member

@ADmad ADmad Oct 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using the $this->name is not correct IMO. For e.g. if the current controller name is Admins but the template path is set to Users then I would expect it to look for the fragment under Users folder.

The View class is also used for rendering emails and cell where the name property is irrelevant/empty.

"phpunit/phpunit": "^10.5.5 || ^11.1.3"
"phpstan/phpstan": "1.12.3",
"phpunit/phpunit": "^10.5.5 || ^11.1.3",
"squizlabs/php_codesniffer": "^3.10",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we omit this. We don't need to change dependencies to add view methods.

->getMock();

// Set up the element method to return the stored fragment data
$this->View->expects($this->any())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would avoid using mocks for this. Instead add more templates to the test application and use those for your tests. Our test suite is more reliable and easier to understand if we use fewer mocks and have more 'integration tests' that use the framework like applications would.


// Verify that the fragment is cleared by checking the stored fragment
$clearedResult = $this->View->fragment('testFragment');
echo 'Cleared fragment: ' . $clearedResult . "\n";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests shouldn't be generating output.

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

Successfully merging this pull request may close these issues.

4 participants