Page 1 of 2

testing return type of getters?

Posted: Sun Mar 15, 2009 3:12 pm
by koen.h
Is it meaningful to test the return type of getters? eg test if a method returns a string, array, ...?

Re: testing return type of getters?

Posted: Sun Mar 15, 2009 4:26 pm
by Christopher
I don't think it would hurt. But you have to maintain the test. And I think it depends on the getter/setter. If it is supposed to convert/enforce all data to a specific type then it would make sense to test the type. Especially if there is a good chance that the problem could occur. Otherwise you are on the line between unit tests and smoke tests.

Re: testing return type of getters?

Posted: Sun Mar 15, 2009 7:07 pm
by josh
Use assertIdentical, if you find the test is too brittle relax it with assertEquals

Re: testing return type of getters?

Posted: Mon Mar 16, 2009 8:15 am
by Jenk
No, there's no point. Your asserts should not be that explicit, but should be implicit enough to cover such concerns. Testing types is enforcing tight-coupling. Bad.

Code: Select all

public function testGetter () {
  $foo = $objectUnderTest->getSomething();
  $this->assertTrue($foo->fooBar());
}
The above is implicitly testing that what ever is returned by getSomething() implements fooBar().

Re: testing return type of getters?

Posted: Mon Mar 16, 2009 8:26 am
by koen.h
Why not make the implicit explicit? If one needs something implicitly tested, isn't that a sign it should be tested explicitly or not?

Re: testing return type of getters?

Posted: Mon Mar 16, 2009 9:07 am
by Jenk
No, because what happens when you need to use the component with a different type, but implements the same interface? You'll get a failure.

The entire point of OO is to be implicit. If it walks and talks like a duck, it's a duck. I don't need to match it's DNA to check it's a duck.

Re: testing return type of getters?

Posted: Mon Mar 16, 2009 9:19 am
by koen.h
Ideally the return type would be part of the interface but that's a feature not yet available. If it walks and talks like a duck but lays duck-eggs one time, but chicken-eggs the other time it will break the system.

Re: testing return type of getters?

Posted: Mon Mar 16, 2009 11:04 am
by Jenk
and when you try and treat what was just laid as a duck egg, your test will implicitly fail.

Re: testing return type of getters?

Posted: Mon Mar 16, 2009 2:47 pm
by koen.h
And then I'd have to find out what the problem is while with an explicit test it's known immediately.

Re: testing return type of getters?

Posted: Mon Mar 16, 2009 4:54 pm
by Benjamin
I'd probably only test for that if it needs to be an explicit type. If something needs to return an object I would test for that, maybe even test to verify it's an instance of a particular class.

Re: testing return type of getters?

Posted: Mon Mar 16, 2009 5:20 pm
by Chris Corbyn
Yeah I disagree with Jenk here. I don't think you should test return types all the time, but I don't think you should never test a return type. That's almost like saying the === operator has no place in PHP.

Re: testing return type of getters?

Posted: Mon Mar 16, 2009 9:35 pm
by Jenk
Not quite.. === isn't comparing type, it's only comparing instance.

Code: Select all

$this->assertIdentical($registry->get('foo'), $registry->get('foo'));
That's a pass..

If you are required to have an explicit type returned, then ok.. but I really cannot think of *any* scenario where this is applicable, save for maybe one which is a misnomer.. a test for a Factory. At the *most* I'd agree on testing for an interface (rather than concrete class); but then I disagree with type stricting at all. Interfaces and Notations are the kick-back from using type-strict in the first place, rather than ditching type restrictions you have to work with such inferior work arounds :P

Re: testing return type of getters?

Posted: Tue Mar 17, 2009 10:13 pm
by Jenk
I'm actually interested in hearing some examples of where explicitly testing the return type would be required, if anyone has an example. :)

Re: testing return type of getters?

Posted: Wed Mar 18, 2009 5:07 am
by koen.h
An example would be (from memory) Zend Authentication. It returns an Result object. Something like this:

Code: Select all

 
<?php
$result = $auth->login($username, $pass);
if ($result->isAuthenticated()) {}
?>
 
Here I would like to be sure the returned result-object implements a certain interface.

Re: testing return type of getters?

Posted: Wed Mar 18, 2009 6:49 am
by Jenk
Which is implicitly tested by calling isAuthenticated(). I wouldn't test for the interface then.