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

Update WizardComponent.php #24

Merged
merged 2 commits into from Apr 19, 2017
Merged

Update WizardComponent.php #24

merged 2 commits into from Apr 19, 2017

Conversation

mlingureanu
Copy link

test type for $proceed

test type for $proceed
@ProLoser
Copy link
Owner

ProLoser commented Mar 31, 2017

I'll leave this up to @bancer who's become far more active on this repo than I. Also, thank you @mlingureanu for your first contribution!

@ProLoser ProLoser assigned ProLoser and bancer and unassigned ProLoser Mar 31, 2017
@ProLoser
Copy link
Owner

@mlingureanu since this is your first contribution, can I ask you what this PR is trying to accomplish? What was the problem you ran into that made this fix necessary?

@mlingureanu
Copy link
Author

mlingureanu commented Apr 1, 2017

The documentation says

The process callback must return either true or false. If true, the wizard will continue to the next step; if false, the user will remain on the step and any validation errors will be presented.

I run into trouble, on unit tests, when instead of returning boolean, I need to force a redirect like
return $this->redirect($url);
The problem is, this process callback will return an mocked object in unit tests, and variable $proceed from wizard component will get this mocked object instead of boolean value.
Next condition if ($proceed) without checking type, will assume this object is true and will execute unwanted code.

@ProLoser
Copy link
Owner

ProLoser commented Apr 2, 2017 via email

@mlingureanu
Copy link
Author

When testing actions that contain redirect() and other code following the redirect it is generally a good idea to return when redirecting. The reason for this, is that redirect() is mocked in testing, and does not exit like normal. And instead of your code exiting, it will continue to run code following the redirect.

https://book.cakephp.org/2.0/en/development/testing.html#testing-controllers

@bancer
Copy link
Collaborator

bancer commented Apr 3, 2017

I am not convinced this change is needed while the same can be achieved by $this->redirect($url); return false; or $this->redirect($url); return true; in the controller. @ProLoser do you have an opinion on that?

@ProLoser
Copy link
Owner

ProLoser commented Apr 3, 2017

I'm too removed from the code to know how it works anymore, I just don't like strict comparisons to true unless you handle other truthy values differently (which this code doesn't)

@@ -382,7 +382,7 @@ public function process($step) {
} else {
throw new NotImplementedException(sprintf(__('Process Callback not found. Please create Controller::%s', $processCallback)));
}
if ($proceed) {
if ($proceed === true) {
Copy link
Author

Choose a reason for hiding this comment

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

$proceed from line 379 and line 381 should return always boolean, that's why it's a matter of best practices, to test type when dealing with booleans

Copy link
Author

Choose a reason for hiding this comment

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

Copy link
Author

Choose a reason for hiding this comment

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

Gotchas
When testing the return value of a function that can return either 0 or boolean false, like strpos(), always use === and !==, or you’ll run in to problems.

Copy link
Owner

Choose a reason for hiding this comment

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

That specifically talks about falsey values if they represent different things than null.

Again, I think something is wrong with your unit test. I need more context on how this code is called.

Copy link
Collaborator

@bancer bancer left a comment

Choose a reason for hiding this comment

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

Looks good for me. @ProLoser ?

@ProLoser
Copy link
Owner

Why does processCallback not return a boolean?

@mlingureanu
Copy link
Author

mlingureanu commented Apr 12, 2017

in WizardComponentTest.php at line 119

public function redirect($url = null, $status = null, $exit = true) {
// Do not allow redirect() to exit in unit tests.
return parent::redirect($url, $status, false);
}

it's override default cakephp redirect method to not exit and to continue to how Bancer says

$this->redirect($url); return false; or $this->redirect($url); return true

this is a snippet from my code:

public function processRestaurant() {
		if (!empty($this->request->data)) {
		...
		// Create the redirect url with the new restaurant and the extra values from before.
		$url = array(
			'controller' => 'book',
			'action' => 'table',
			$stepToGo,
			$restaurantId,
		);
		$url = array_merge($url, $extra);
		$this->redirect($url, null, false);
		return false;
	}
	return false;
}

I need this redirect, because I need to change param $restaurantId and method redirect from wizard don't allow params to be changed.

@mlingureanu
Copy link
Author

by the way this comment // Do not allow redirect() to exit in unit tests. it's wrong because in unit tests redirect is mocked and third param $exit is false

@ProLoser
Copy link
Owner

I mean I'll leave it to bancer

@bancer bancer merged commit 50e80e8 into ProLoser:master Apr 19, 2017
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 this pull request may close these issues.

None yet

3 participants