You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Subject
10 - it can handle date time dummies
exception [err:Error("Call to a member function sub() on null")] has been thrown.
100% 1
1 specs
1 example (1 broken)
Expected Result
1 specs
1 example (1 passed)
Explanation
Prophecy uses the DateTimeComparator class from sebastian/comparator package from the ExactValueToken class in order to compare the actual date argument with the argument of the promise object. That worked fine before 2.1.1 or earlier versions. But in 2.1.2 methods of the \DateTime class are chained like $expected->setTimezone()->sub(). Since $expected is not a real \DateTime object, but actually a dummy test double, all of the methods will return null by definition. Therefore $expected->setTimezone() becomes null, that's why sub() cannot be called on that.
Since our compared object is not a real \DateTime, it should not be compared by the DateTimeComparator. The condition to support an object is to check the inheritance chain, it will be accepted for comparison which is fine when the real \DateTime class is extended.
The text was updated successfully, but these errors were encountered:
Summary
When you want to use
\DateTime
object in PHPSpec tests as dummy object, then the test fails with the following error message:The Problem
Let's suppose we have two classes
Subject
andCollaborator
and we are writing spec for the former.Source code looks like this:
composer.json
Finally execute the tests:
vendor/bin/phpspec run
Actual Result
Expected Result
Explanation
Prophecy uses the
DateTimeComparator
class fromsebastian/comparator
package from theExactValueToken
class in order to compare the actual date argument with the argument of the promise object. That worked fine before 2.1.1 or earlier versions. But in 2.1.2 methods of the\DateTime
class are chained like$expected->setTimezone()->sub()
. Since$expected
is not a real\DateTime
object, but actually a dummy test double, all of the methods will returnnull
by definition. Therefore$expected->setTimezone()
becomesnull
, that's whysub()
cannot be called on that.Since our compared object is not a real
\DateTime
, it should not be compared by theDateTimeComparator
. The condition to support an object is to check the inheritance chain, it will be accepted for comparison which is fine when the real\DateTime
class is extended.The text was updated successfully, but these errors were encountered: