testing return type of getters?
Moderator: General Moderators
testing return type of getters?
Is it meaningful to test the return type of getters? eg test if a method returns a string, array, ...?
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: testing return type of getters?
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.
(#10850)
Re: testing return type of getters?
Use assertIdentical, if you find the test is too brittle relax it with assertEquals
Re: testing return type of getters?
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.
The above is implicitly testing that what ever is returned by getSomething() implements fooBar().
Code: Select all
public function testGetter () {
$foo = $objectUnderTest->getSomething();
$this->assertTrue($foo->fooBar());
}Re: testing return type of getters?
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?
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.
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?
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?
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?
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?
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.
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
Re: testing return type of getters?
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?
Not quite.. === isn't comparing type, it's only comparing instance.
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
Code: Select all
$this->assertIdentical($registry->get('foo'), $registry->get('foo'));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
Re: testing return type of getters?
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?
An example would be (from memory) Zend Authentication. It returns an Result object. Something like this:
Here I would like to be sure the returned result-object implements a certain interface.
Code: Select all
<?php
$result = $auth->login($username, $pass);
if ($result->isAuthenticated()) {}
?>
Re: testing return type of getters?
Which is implicitly tested by calling isAuthenticated(). I wouldn't test for the interface then.