/** * @test */ public function executeSpecification() { $spec = new Specification(); $setupBlock = \Mockery::mock(SimpleBlock::clazz())->shouldReceive('compileCode')->once()->andReturn('$__specification__assertCount = 1;')->mock(); $whenBlock = \Mockery::mock(SimpleBlock::clazz())->shouldReceive('compileCode')->twice()->andReturn('$__specification__assertCount += 1;')->mock(); $thenBlock = \Mockery::mock(ThenBlock::clazz())->shouldReceive('compileCode')->twice()->andReturn('$__specification__assertCount += 2;')->shouldReceive('getPreConditions')->twice()->andReturn(array('$__specification__assertCount += 3'))->mock(); // and +2 for preconditions $cleanupBlock = \Mockery::mock(SimpleBlock::clazz())->shouldReceive('compileCode')->once()->andReturn('$__specification__assertCount += 3;')->mock(); $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)); $spec->setCleanupBlock($cleanupBlock); $result = $spec->run(); $this->assertEquals(18, $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); } }