Skip to content

Commit

Permalink
Add __debugInfo() methods to more objects.
Browse files Browse the repository at this point in the history
Many of these objects can contain Closures, and I'd like to improve how
they are printed in DebugKit. This feels like the best way to improve
developer experience around these objects without making the solution
too specific to DebugKit.

Refs #6504
  • Loading branch information
markstory committed May 10, 2015
1 parent aff1b8b commit dcc9366
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/Form/Form.php
Expand Up @@ -188,4 +188,19 @@ protected function _execute(array $data)
{ {
return true; return true;
} }

/**
* Get the printable version of a Form instance.
*
* @return array
*/
public function __debugInfo()
{
$special = [
'_schema' => $this->schema()->__debugInfo(),
'_errors' => $this->errors(),
'_validator' => $this->validator()->__debugInfo()
];
return $special + get_object_vars($this);
}
} }
12 changes: 12 additions & 0 deletions src/Form/Schema.php
Expand Up @@ -121,4 +121,16 @@ public function fieldType($name)
} }
return $field['type']; return $field['type'];
} }

/**
* Get the printable version of this object
*
* @return array
*/
public function __debugInfo()
{
return [
'_fields' => $this->_fields
];
}
} }
24 changes: 24 additions & 0 deletions src/Validation/Validator.php
Expand Up @@ -580,4 +580,28 @@ protected function _processRules($field, ValidationSet $rules, $data, $newRecord
} }
return $errors; return $errors;
} }

/**
* Get the printable version of this object.
*
* @return array
*/
public function __debugInfo()
{
$fields = [];
foreach ($this->_fields as $name => $fieldSet) {
$fields[$name] = [
'isPresenceRequired' => $fieldSet->isPresenceRequired(),
'isEmptyAllowed' => $fieldSet->isEmptyAllowed(),
'rules' => array_keys($fieldSet->rules()),
];
}
return [
'_presenceMessages' => $this->_presenceMessages,
'_allowEmptyMessages' => $this->_allowEmptyMessages,
'_useI18n' => $this->_useI18n,
'_providers' => array_keys($this->_providers),
'_fields' => $fields
];
}
} }
14 changes: 14 additions & 0 deletions tests/TestCase/Form/FormTest.php
Expand Up @@ -154,4 +154,18 @@ public function testExecuteValid()


$this->assertTrue($form->execute($data)); $this->assertTrue($form->execute($data));
} }

/**
* test __debugInfo
*
* @return void
*/
public function testDebugInfo()
{
$form = new Form();
$result = $form->__debugInfo();
$this->assertArrayHasKey('_schema', $result);
$this->assertArrayHasKey('_errors', $result);
$this->assertArrayHasKey('_validator', $result);
}
} }
24 changes: 24 additions & 0 deletions tests/TestCase/Form/SchemaTest.php
Expand Up @@ -116,4 +116,28 @@ public function testFieldType()
$this->assertEquals('decimal', $schema->fieldType('numbery')); $this->assertEquals('decimal', $schema->fieldType('numbery'));
$this->assertNull($schema->fieldType('nope')); $this->assertNull($schema->fieldType('nope'));
} }

/**
* test __debugInfo
*
* @return void
*/
public function testDebugInfo()
{
$schema = new Schema();

$schema->addField('name', 'string')
->addField('numbery', [
'type' => 'decimal',
'required' => true
]);
$result = $schema->__debugInfo();
$expected = [
'_fields' => [
'name' => ['type' => 'string', 'length' => null, 'precision' => null],
'numbery' => ['type' => 'decimal', 'length' => null, 'precision' => null],
],
];
$this->assertEquals($expected, $result);
}
} }
40 changes: 40 additions & 0 deletions tests/TestCase/Validation/ValidatorTest.php
Expand Up @@ -769,4 +769,44 @@ public function testCompareWithIntegration()
]; ];
$this->assertNotEmpty($validator->errors($data), 'Validation should fail.'); $this->assertNotEmpty($validator->errors($data), 'Validation should fail.');
} }

/**
* Test debugInfo helper method.
*
* @return void
*/
public function testDebugInfo()
{
$validator = new Validator();
$validator->provider('test', $this);
$validator->add('title', 'not-empty', ['rule' => 'notEmpty']);
$validator->requirePresence('body');
$validator->allowEmpty('published');

$result = $validator->__debugInfo();
$expected = [
'_providers' => ['test'],
'_fields' => [
'title' => [
'isPresenceRequired' => false,
'isEmptyAllowed' => false,
'rules' => ['not-empty'],
],
'body' => [
'isPresenceRequired' => true,
'isEmptyAllowed' => false,
'rules' => [],
],
'published' => [
'isPresenceRequired' => false,
'isEmptyAllowed' => true,
'rules' => [],
],
],
'_presenceMessages' => [],
'_allowEmptyMessages' => [],
'_useI18n' => true,
];
$this->assertEquals($expected, $result);
}
} }

0 comments on commit dcc9366

Please sign in to comment.