/** * @test */ function it_validates_nested_data_objects_for_required_nested_invalid_data() { $data = new TestNestedDataObject(); $this->assertFalse($data->validate(), "validation should fail for empty"); $nestedData = new TestDataObject(['irrelevant' => 'nothing']); $this->assertFalse($nestedData->validate(), "validation should fail for empty nested (on its own)"); $data->nested = $nestedData; $this->assertFalse($data->validate(), "validation should fail for empty nested (when nested)"); // the messages should indicate that the name field is missing from the nested object $messages = $data->messages(); $this->assertCount(2, $messages, "should be two messages in the bag (1 for nested, 1 for nested content"); $this->assertRegExp('#name .*required#i', $messages->get('nested.name')[0], 'Incorrect or missing message for nested name'); $this->assertEquals('validation.dataobject', $messages->get('nested')[0], 'Incorrect or missing message for nested main'); // Note that the validation.dataobject message is the custom message for which there is // no translation in this test! }
/** * @test */ function it_takes_dot_notation_to_get_nested_data_at_depth() { $data = new Helpers\TestDataObject(['top' => 'test', 'contents' => new Helpers\TestDataObject(['mass' => 'testing', 'assignment' => 2242]), 'more' => [new Helpers\TestDataObject(['a' => 'b'])], 'array' => ['normal' => 'nested']]); $this->assertEquals('test', $data->getNested('top'), 'Incorrect value for top level attribute'); $this->assertEquals('nested', $data->getNested('array.normal'), 'Incorrect value for nested array'); $this->assertEquals('testing', $data->getNested('contents.mass'), 'Incorrect value for recursive nested DataObject'); $this->assertEquals('b', $data->getNested('more.0.a'), 'Incorrect value for recursive nested DataObject in array'); $this->assertEquals('DEF', $data->getNested('more.1.4.3.hop', 'DEF'), 'Expecting default for wrong key'); }