Raine, Dave, Francisco: I’ve updated the article to make the notice errors go away :)

mark story on 2/13/10

Ok, good fix, Thks for the article.

Francisco.

Francisco on 2/15/10

Any idea what to do about this little error?

“Unexpected PHP error [You cannot use an empty key for Security::cipher()] severity [E_USER_WARNING] …..”

Thanks!!

Cameron on 3/4/10

Cameron: From reading the error it sounds like you have an empty cipherSeed. This isn’t allowed, adding a cipherSeed in your core.php should make the message go away.

mark story on 3/5/10

Hey, I found this extremely useful. I was struggling for a while figuring out how to use the test suite. I am more confident now and my test cases are actually producing clean and useful results!

Thanks.
Mohammed

Mohammed on 3/20/10

Mark: Didn’t see your reply until today. That definitely did the trick. Thanks!

Cameron Perry on 3/20/10

I wrote a function to do the extending of a controller for you. http://gist.github.com/382823

Joe Beeson on 4/28/10

Fatal error: Call to a member function check() on a non-object in C:\xampp\htdocs\wp\cake\libs\controller\components\auth.php on line 527

Keep getting the above and i am using cake 1.3 i started off using the code inside the super awesome pdf by matt curry so i came here to double check. Only difference is the $this->Posts->params = Router::parse(’/admin/profile/edit’);

My line is $this->Merchants->params = Router::parse(’/admin/profile/edit’);

Please advise. thank you.

keisimone on 4/29/10

Joe beeson: Neat, you could probably use partial mocks to accomplish a similar thing. :)

mark story on 5/1/10

Hi Mark, thanks for this great article. How can I access view variables set in controller for assertion? Do I have to access them directly as $this->viewVars?

Kaupo on 9/23/10

Thanks. The article was really useful for testing Controllers beyond the testAction() method (which is not good, IMO).

The only thing I noticed is that the code shown in this article does not use test cases for models (see: $this->Posts->Post->read(null, 2)). The ‘Post’ model here is the actual model class and not the ‘PostTestCase’ class defined under /app/tests. Is there a way to import those test models and test against them instead? I’ve tried but no luck.

Another question: is there a way to test AppController (or AppModel) in the same way as shown here?

Thanks.

(P.S. Also, for some reason, fixtures are not being created for test models that use more than one fixture. While “var $fixtures = array(‘app.charity’)” works and creates the tables in test schema, “var $fixtures = array(‘app.coupon’, ‘app.user’)” doesn’t).

YOMorales on 2/15/11

Hi,

Really cool blog post (just as the one about mocks), but I still seem to get stuck as I get some strange errors I can’t explain.

First one, when I’m trying to Mock the Acl Component: Unexpected PHP error [Undefined property: MockAclComponent::$enabled] severity [E_NOTICE] in [/private/var/git/cakephp-1.3/cake/libs/controller/component.php line 185]

Second one is this: ERROR->Unexpected PHP error [Undefined index: controller] severity [E_NOTICE] in [/private/var/git/cakephp-1.3/cake/libs/controller/components/auth.php line 801]
Even though I’m filling params using the Router::parse function and when I do debug($this->->params) I get the array with all the indices set correctly.

Any thoughts?

Vincent on 3/2/11

Solved my issues.

By putting the creation of mock components after $this->{Controller}->Component->startup(); the error about $enabled not being set disappears.

Same goes for the other issue which is caused by an empty $this->params. I put $this->params = Router::parse() before the $this->{Controller}->beforeFilter and everything works fine.

Vincent on 3/3/11

Small correction on my last remark: $this->params needs to be defined before you do Component->startup, just as in the sample code above, but I needed to call the beforeFilter callback AFTER Component->startup and not before as it would raise errors regarding some components I was mocking later on (Acl component in this case).

Vincent on 3/3/11

I am having a bit of a problem using this method when testing controllers from a plugin.

I’m trying to do

App::import(‘Controller’, ‘UserPlugin.Users’);

However, when I do that, I only get a dump h2 saying Users, and the test does not even start executing.

How can I specify that I want to import a controller from a plugin?

Thanks,
Radu

Radu on 3/5/11

I’m starting with TDD & cakePHP and I clearly understand what you explain here. The documentation of CakePHP is terrific.

Thanks for the article and for your contribution in the CakePHP project!

raultm on 4/15/11

I see you can still use testAction method:

$result = $this->testAction(’/products/index’));
debug($result);

so, what is the difference ?

Guillermo on 5/4/11

I saw the difference.
You should not use testAction so that you can catch the redirect property

$this->Products->params = Router::parse(’/products/index’); $this->Products->beforeFilter(); $this->Products->Component->startup($this->Products); $this->Products->index(); debug($this->Products->redirectUrl);

Guillermo on 5/4/11

f_CK! can’t even start ONE FREAKING test?? what’s up with that?

How about, you find a second and write a “dumb-dumbo” tutorial to cake-unit-testing for dummies???????!!!!

crap on 5/27/11

I am wondering if some of the method calls in this post are no longer valid after CakePHP 2.x. For example, I cannot call $myMockController->Component->initialize(). PHP complains that it has attempted to call a method on a non-object (null).

Larry Siden on 8/14/12