जैसा कि मैंने Symfony's WebTestCase उपयोग कर रहा हूँ PHPUnit परीक्षण का उपयोग कर कार्यात्मक परीक्षण प्रदर्शन करने के लिए बंडल, डेटटाइम क्लास के सभी उपयोगों को नकल करने के लिए यह जल्दी ही अव्यवहारिक हो गया।
मैं के रूप में यह इस तरह के परीक्षण कुकी या कैश समाप्ति, आदि
सबसे अच्छा तरीका है मैं ऐसा करने के लिए मिल गया है मेरे अपने समयदिनाँक वर्ग फैली लागू करने के लिए है के रूप में समय के साथ अनुरोध, संभालती आवेदन का परीक्षण करना चाहता था डिफ़ॉल्ट वर्ग, और उस बिंदु से बनाई गई सभी डेटटाइम ऑब्जेक्ट्स को डिफॉल्ट टाइम स्कू को जोड़ने/घटाए जाने के लिए कुछ स्थैतिक विधियां प्रदान करने के लिए।
यह लागू करने के लिए वास्तव में एक आसान सुविधा है, और कस्टम पुस्तकालयों को स्थापित करने की आवश्यकता नहीं है।
चेतावनी एम्प्टर: इस विधि को केवल दोष यह है सिम्फोनी ढांचा (या जो भी ढांचा आप उपयोग कर रहे) अपने पुस्तकालय का उपयोग नहीं होगा, इसलिए किसी भी व्यवहार में यह इस तरह के आंतरिक कैश/कुकी के रूप में ही है, संभाल करने की उम्मीद है समाप्ति, इन परिवर्तनों से प्रभावित नहीं होंगे।
namespace My\AppBundle\Util;
/**
* Class DateTime
*
* Allows unit-testing of DateTime dependent functions
*/
class DateTime extends \DateTime
{
/** @var \DateInterval|null */
private static $defaultTimeOffset;
public function __construct($time = 'now', \DateTimeZone $timezone = null)
{
parent::__construct($time, $timezone);
if (self::$defaultTimeOffset && $this->isRelativeTime($time)) {
$this->modify(self::$defaultTimeOffset);
}
}
/**
* Determines whether to apply the default time offset
*
* @param string $time
* @return bool
*/
public function isRelativeTime($time)
{
if($time === 'now') {
//important, otherwise we get infinite recursion
return true;
}
$base = new \DateTime('2000-01-01T01:01:01+00:00');
$base->modify($time);
$test = new \DateTime('2001-01-01T01:01:01+00:00');
$test->modify($time);
return ($base->format('c') !== $test->format('c'));
}
/**
* Apply a time modification to all future calls to create a DateTime instance relative to the current time
* This method does not have any effect on existing DateTime objects already created.
*
* @param string $modify
*/
public static function setDefaultTimeOffset($modify)
{
self::$defaultTimeOffset = $modify ?: null;
}
/**
* @return int the unix timestamp, number of seconds since the Epoch (Jan 1st 1970, 00:00:00)
*/
public static function getUnixTime()
{
return (int)(new self)->format('U');
}
}
इस का उपयोग करते हुए सरल है:
public class myTestClass() {
public function testMockingDateTimeObject()
{
echo "fixed: ". (new DateTime('18th June 2016'))->format('c') . "\n";
echo "before: ". (new DateTime('tomorrow'))->format('c') . "\n";
echo "before: ". (new DateTime())->format('c') . "\n";
DateTime::setDefaultTimeOffset('+25 hours');
echo "fixed: ". (new DateTime('18th June 2016'))->format('c') . "\n";
echo "after: ". (new DateTime('tomorrow'))->format('c') . "\n";
echo "after: ". (new DateTime())->format('c') . "\n";
// fixed: 2016-06-18T00:00:00+00:00 <-- stayed same
// before: 2016-09-20T00:00:00+00:00
// before: 2016-09-19T11:59:17+00:00
// fixed: 2016-06-18T00:00:00+00:00 <-- stayed same
// after: 2016-09-21T01:00:00+00:00 <-- added 25 hours
// after: 2016-09-20T12:59:17+00:00 <-- added 25 hours
}
}
आपने अभी तक एक जवाब स्वीकार नहीं किया। क्या आप कृपया उत्तर में जो खोज रहे हैं उसे स्पष्ट कर सकते हैं और दिए गए उत्तरों आपको संतुष्ट क्यों नहीं करते हैं। – Gordon
बिल्कुल वही समस्या थी, @Shouze के उत्तर से php timecop एक्सटेंशन एक आकर्षण की तरह काम करता था। –