Skip to content

Commit

Permalink
Added explicit tests for _convertFieldsToType
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremyharris committed May 25, 2018
1 parent 0b70d47 commit 2f55cb8
Showing 1 changed file with 67 additions and 0 deletions.
67 changes: 67 additions & 0 deletions tests/TestCase/Model/Behavior/VersionBehaviorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
use Cake\ORM\Entity;
use Cake\ORM\TableRegistry;
use Cake\TestSuite\TestCase;
use InvalidArgumentException;
use Josegonzalez\Version\Model\Behavior\VersionBehavior;
use Josegonzalez\Version\Model\Behavior\Version\VersionTrait;
use ReflectionObject;

class TestEntity extends Entity
{
Expand Down Expand Up @@ -431,4 +433,69 @@ public function testVersionConvertsType()
$version = $article->version($article->version_id);
$this->assertInternalType('int', $version->author_id);
}

/**
* tests _convertFieldsToType
*
* @return void
*/
public function testConvertFieldsToType()
{
$table = TableRegistry::get('Articles', [
'entityClass' => 'Josegonzalez\Version\Test\TestCase\Model\Behavior\TestEntity',
]);
$schema = $table->getSchema();
$schema->setColumnType('settings', 'json');
$table->setSchema($schema);
$behavior = new VersionBehavior($table);

$reflection = new ReflectionObject($behavior);
$method = $reflection->getMethod('_convertFieldsToType');
$method->setAccessible(true);

$data = ['test' => 'array'];
$fields = [
'settings' => json_encode($data),
'author_id' => '1',
'body' => 'text',
];
$fields = $method->invokeArgs($behavior, [$fields, 'toPHP']);
$this->assertInternalType('array', $fields['settings']);
$this->assertSame($data, $fields['settings']);
$this->assertInternalType('int', $fields['author_id']);
$this->assertInternalType('string', $fields['body']);

$data = ['test' => 'array'];
$fields = [
'settings' => ['test' => 'array'],
'author_id' => 1,
'body' => 'text',
];
$fields = $method->invokeArgs($behavior, [$fields, 'toDatabase']);
$this->assertInternalType('string', $fields['settings']);
$this->assertSame(json_encode($data), $fields['settings']);
$this->assertInternalType('int', $fields['author_id']);
$this->assertInternalType('string', $fields['body']);
}

/**
* tests passing an invalid direction to _convertFieldsToType
*
* @return void
*/
public function testConvertFieldsToTypeInvalidDirection()
{
$this->expectException(InvalidArgumentException::class);

$table = TableRegistry::get('Articles', [
'entityClass' => 'Josegonzalez\Version\Test\TestCase\Model\Behavior\TestEntity',
]);
$behavior = new VersionBehavior($table);

$reflection = new ReflectionObject($behavior);
$method = $reflection->getMethod('_convertFieldsToType');
$method->setAccessible(true);

$method->invokeArgs($behavior, [[], 'invalidDirection']);
}
}

0 comments on commit 2f55cb8

Please sign in to comment.