/** * Instantiate and configure the node according to this definition * * @return Faker\Components\Engine\Common\Composite\TypeNode The node instance * * @throws InvalidDefinitionException When the definition is invalid */ public function getNode() { $type = new Date(); $type->setGenerator($this->generator); $type->setUtilities($this->utilities); $type->setLocale($this->locale); foreach ($this->attributes as $attribute => $value) { $type->setOption($attribute, $value); } return new TypeNode('Date', $this->eventDispatcher, $type); }
public function testGenerate() { $utilities = $this->getMockBuilder('Faker\\Components\\Engine\\Common\\Utilities')->disableOriginalConstructor()->getMock(); $generator = $this->getMock('\\PHPStats\\Generator\\GeneratorInterface'); $locale = $this->getMock('\\Faker\\Locale\\LocaleInterface'); $rnd_date = new \DateTime(); $generator->expects($this->once())->method('generate')->with($this->isType('integer'), $this->isType('integer'))->will($this->returnValue($rnd_date->getTimestamp())); $type = new Date(); $type->setGenerator($generator); $type->setLocale($locale); $type->setUtilities($utilities); # test with start > 0 $type->setOption('start', 'today'); $type->setOption('modify', '+ 1 hour'); $type->validate(); $values = array(); $this->assertInstanceOf('\\DateTime', $type->generate(1, $values)); $this->assertInstanceOf('\\DateTime', $type->generate(1, $values)); # test with max $type->setOption('start', 'today'); $type->setOption('modify', '+ 1 hour'); $type->setOption('max', 'today +3 hours'); $type->validate(); $values = array(); $dte1 = $type->generate(1, $values); $dte2 = $type->generate(2, $values); $dte3 = $type->generate(3, $values); $dte4 = $type->generate(4, $values); $dte5 = $type->generate(4, $values); # test if date has been reset once max reached $this->assertEquals($dte1->format('U'), $dte5->format('U')); # iterations are not equal ie modify is appied on each loop $this->assertFalse($dte1->format('U') === $dte2->format('U')); $this->assertFalse($dte2->format('U') === $dte3->format('U')); $this->assertFalse($dte3->format('U') === $dte4->format('U')); $this->assertFalse($dte4->format('U') === $dte5->format('U')); # test with modify $type->setOption('modify', false); $type->setOption('random', true); $this->assertEquals($rnd_date->getTimestamp(), $type->generate(1, $values)->getTimestamp()); # test fixed date $start = new \DateTime(); $type->setOption('modify', false); $type->setOption('random', false); $type->setOption('start', $start); $this->assertEquals($start->getTimestamp(), $type->generate(1, $values)->getTimestamp()); $this->assertEquals($start->getTimestamp(), $type->generate(2, $values)->getTimestamp()); $this->assertEquals($start->getTimestamp(), $type->generate(3, $values)->getTimestamp()); }