public static function fromArray(array $rules) { foreach ($rules as $name => &$rule) { if ($rule instanceof ValidatorInterface) { continue; } if (is_string($name)) { $args = $rule; } else { $name = $rule; $args = []; } if (!is_array($args)) { $args = [$args]; } $rule = Validator::createRule($name, $args); } return new static($rules); }
public function testFails() { $rule1 = $this->getMock('Sloths\\Validation\\Validator\\ValidatorInterface'); $rule1->expects($this->once())->method('getMessageTemplate')->willReturn('message 1 :foo'); $rule1->expects($this->once())->method('getDataForMessage')->willReturn(['foo' => 'foo']); $rule2 = $this->getMock('Sloths\\Validation\\Validator\\ValidatorInterface'); $rule2->expects($this->once())->method('getMessageTemplate')->willReturn('message 2 :bar'); $rule2->expects($this->once())->method('getDataForMessage')->willReturn(['bar' => 'bar']); $chain1 = $this->getMock('Sloths\\Validation\\Validator\\Chain', ['validate']); $chain1->expects($this->once())->method('validate')->willReturn($rule1); $chain2 = $this->getMock('Sloths\\Validation\\Validator\\Chain', ['validate']); $chain2->expects($this->once())->method('validate')->willReturn(true); $chain3 = $this->getMock('Sloths\\Validation\\Validator\\Chain', ['validate']); $chain3->expects($this->once())->method('validate')->willReturn($rule2); $validator = new Validator(); $validator->addChains(['foo' => $chain1, 'bar' => $chain2, 'baz' => $chain3]); $this->assertFalse($validator->validate([])); $this->assertSame(['foo' => $rule1, 'baz' => $rule2], $validator->fails()); $this->assertSame(['foo' => 'message 1 foo', 'baz' => 'message 2 bar'], $validator->getMessages()); }