/** * @test */ public function executeSpecificationWithParametrization() { $spec = new Specification(); $setupBlock = \Mockery::mock(SimpleBlock::clazz())->shouldReceive('compileCode')->twice()->andReturn('$__specification__assertCount += 1;')->mock(); $whenBlock = \Mockery::mock(SimpleBlock::clazz())->shouldReceive('compileCode')->times(4)->andReturn('$__specification__assertCount += 1;')->mock(); $thenBlock = \Mockery::mock(ThenBlock::clazz())->shouldReceive('compileCode')->times(4)->andReturn('$__specification__assertCount += 2;')->shouldReceive('getPreConditions')->times(4)->andReturn(array('$__specification__assertCount += 3'))->mock(); // and +4 for preconditions $spec->setSetupBlock($setupBlock); $pair1 = new \PhpSpock\Specification\WhenThenPair(); $pair1->setWhenBlock($whenBlock); $pair1->setThenBlock($thenBlock); $pair2 = new \PhpSpock\Specification\WhenThenPair(); $pair2->setWhenBlock($whenBlock); $pair2->setThenBlock($thenBlock); $spec->setWhenThenPairs(array($pair1, $pair2)); $whereBlock = \Mockery::mock(WhereBlock::clazz())->shouldReceive('compileCode')->twice()->with(\Mockery::on(function ($arg) { static $counter = 0; $counter++; return $arg == $counter; }))->andReturn('$__parametrization__hasMoreVariants = true;', '$__parametrization__hasMoreVariants = false;')->mock(); $spec->setSetupBlock($setupBlock); $spec->setWhereBlock($whereBlock); $result = $spec->run(); $this->assertEquals(30, $result); }
public function parseBlocks($blocks, Specification $spec) { $pairs = array(); $currentPair = null; for ($i = 0; $i < count($blocks); $i++) { $block = $blocks[$i]; $blockName = $block['name']; $blockCode = $block['code']; if (in_array($blockName, array('when', 'then'))) { if (!$currentPair) { $currentPair = new \PhpSpock\Specification\WhenThenPair(); } if ($blockName == 'when') { $parser = new SimpleBlockParser(); $currentPair->setWhenBlock($parser->parse($blockCode)); } if ($blockName == 'then') { $parser = new ThenBlockParser(); $currentPair->setThenBlock($parser->parse($blockCode)); $pairs[] = $currentPair; $currentPair = null; } } else { switch ($blockName) { case 'setup': $parser = new SetupBlockParser(); $spec->setSetupBlock($parser->parse($blockCode)); break; case 'cleanup': $parser = new SimpleBlockParser(); $spec->setCleanupBlock($parser->parse($blockCode)); break; case 'where': $parser = new WhereBlockParser(); $spec->setWhereBlock($parser->parse($blockCode)); break; } } } if (count($pairs)) { $spec->setWhenThenPairs($pairs); } }