Skip to content

Commit

Permalink
Table unit tests (CO-1716)
Browse files Browse the repository at this point in the history
  • Loading branch information
tzeller committed Mar 8, 2019
1 parent b40c35d commit 8764496
Show file tree
Hide file tree
Showing 10 changed files with 1,631 additions and 0 deletions.
327 changes: 327 additions & 0 deletions app/tests/TestCase/Model/Table/AbstractTableTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,327 @@
<?php
/**
* COmanage Match Abstract Table Test
*
* Portions licensed to the University Corporation for Advanced Internet
* Development, Inc. ("UCAID") under one or more contributor license agreements.
* See the NOTICE file distributed with this work for additional information
* regarding copyright ownership.
*
* UCAID licenses this file to you under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* @link http://www.internet2.edu/comanage COmanage Project
* @package match
* @since COmanage Match v1.0.0
* @license Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
*/

namespace App\Test\TestCase\Model\Table;

use Cake\Log\Log;
use Cake\ORM\Association;
use Cake\ORM\TableRegistry;
use Cake\TestSuite\TestCase;

/**
* Abstract table test class that provides helper methods.
*/
abstract class AbstractTableTestCase extends TestCase {

/**
* Log to debug.
*
* @var bool
*/
private $debug = true;

/**
* Get the table being tested.
*
* @return \Cake\ORM\Table
*/
abstract function getTable();

/**
* Assert that the table being tested belongs to another table.
*
* @param string $classname Class name of table belonged to.
* @param string $association Association name.
*/
public function assertBelongsTo(string $classname, string $association) {
$association = $this->getTable()->getAssociation($association);
$this->assertInstanceOf($classname, $association->getTarget());
$this->assertEquals(Association::MANY_TO_ONE, $association->type());
}

/**
* Assert that the table being tested has many associations to another table.
*
* @param string $classname Class name of table with many associations to.
* @param string $association Association name.
*/
public function assertHasMany(string $classname, string $association) {
$association = $this->getTable()->getAssociation($association);
$this->assertInstanceOf($classname, $association->getTarget());
$this->assertEquals(Association::ONE_TO_MANY, $association->type());
}

/**
* Assert that a field must be a boolean.
*
* @param string $field Name of field.
*/
public function assertFieldBoolean(string $field) {
$data = [
$field => '0',
];
$this->assertValidationSuccess($data);

$data = [
$field => '1',
];
$this->assertValidationSuccess($data);

$data = [
$field => 0,
];
$this->assertValidationSuccess($data);

$data = [
$field => 1,
];
$this->assertValidationSuccess($data);

$data = [
$field => 'true',
];
$this->assertValidationFailure($data);

$data = [
$field => 'false',
];
$this->assertValidationFailure($data);
}

/**
* Assert that a field must be an integer.
*
* @param string $field Name of field.
*/
public function assertFieldInteger(string $field) {
$data = [
$field => 'not-an-integer',
];
$this->assertValidationFailure($data);

$data = [
$field => 0.5,
];
$this->assertValidationFailure($data);

$data = [
$field => '1',
];
$this->assertValidationSuccess($data);

$data = [
$field => 1,
];
$this->assertValidationSuccess($data);
}

/**
* Assert that a field has an allowed value.
*
* @param string $field Name of field.
* @param array $list List of allowed values.
*/
public function assertFieldInList(string $field, array $list) {
$this->assertTrue(is_array($list));

foreach ($list as $value) {
$data = [
$field => $value
];
$this->assertValidationSuccess($data);
}

$data = [
$field => 'not-in-list'
];
$this->assertValidationFailure($data);
}

/**
* Assert that a field is allowed to be empty.
*
* @param string $field Name of field.
*/
public function assertFieldAllowEmpty(string $field) {
$data = [
$field => '',
];
$this->assertValidationSuccess($data);
}

/**
* Assert that a field is not allowed to be empty.
*
* @param string $field Name of field.
*/
public function assertFieldNotEmpty(string $field) {
$data = [
$field => '',
];
$this->assertValidationFailure($data);
}

/**
* Assert that a field has a maximum length.
*
* @param string $field Name of field.
* @param int $maxLength Maximum length of field.
*/
public function assertFieldMaxLength(string $field, int $maxLength) {
$data = [
$field => str_repeat('x', $maxLength),
];
$this->assertValidationSuccess($data);

$data = [
$field => str_repeat('x', $maxLength + 1),
];
$this->assertValidationFailure($data);
}

/**
* Assert that a numeric field is within a range.
*
* @param string $field Name of field.
* @param int $lower Lower value of range.
* @param int|null $upper Upper value of range.
*/
public function assertFieldRange(string $field, int $lower, int $upper = null) {
$data = [
$field => $lower - 1,
];
$this->assertValidationFailure($data);

foreach (range($lower, $upper) as $i) {
$data = [
$field => $i,
];
$this->assertValidationSuccess($data);
}

$data = [
$field => $upper + 1,
];
$this->assertValidationFailure($data);
}

/**
* Assert that a numeric field is within a range whose upper limit is large.
*
* The upper limit is 64 * 1024 * 1024, which fits the PHP default memory_limit of 128M.
*
* @param string $field Name of field.
* @param int $lower Lower value of range.
*/
public function assertFieldRangeUnbounded(string $field, int $lower) {
$data = [
$field => $lower - 1,
];
$this->assertValidationFailure($data);

$data = [
$field => $lower,
];
$this->assertValidationSuccess($data);

$data = [
$field => 64 * 1024 * 1024,
];
$this->assertValidationSuccess($data);
}

/**
* Assert that a field has values which match a regex.
*
* @param string $field Name of field.
* @param string $regex The pattern of allowed values.
*/
public function assertFieldRegex(string $field, string $regex) {
for ($i = 0; $i < 256; $i++) {
$chr = chr($i);
$data = [
$field => $chr,
];
if (preg_match($regex, $chr)) {
$this->assertValidationSuccess($data);
} else {
$this->assertValidationFailure($data);
}
}
}

/**
* Assert that validation fails.
*
* @param array $data
*/
protected function assertValidationFailure(array $data) {
if ($this->debug) {
Log::debug('assertValidationFailure $data ' . var_export($data, true));
}
$table = $this->getTable()->newEntity($data);
if ($this->debug) {
Log::debug('assertValidationFailure error ' . var_export($table->getErrors(), true));
}
$this->assertNotEmpty($table->getErrors());
}

/**
* Assert that validation is successful.
*
* @param array $data
*/
protected function assertValidationSuccess(array $data) {
if ($this->debug) {
Log::debug('assertValidationSuccess $data ' . var_export($data, true));
}
$table = $this->getTable()->newEntity($data);
if ($this->debug) {
Log::debug('assertValidationSuccess error ' . var_export($table->getErrors(), true));
}
$this->assertEmpty($table->getErrors());
}

/**
* Create test matchgrid.
*
* @return \Cake\Datasource\EntityInterface|false
*/
public function createTestMatchgrid() {
$data = [
'table_name' => 'testmatchgrid',
'description' => 'Test Matchgrid',
'status' => 'A',
'referenceid_method' => 'S',
'referenceid_start' => '1001',
'referenceid_prefix' => 'prefix'
];
$matchgrids = TableRegistry::getTableLocator()->get('Matchgrids');
$matchgrid = $matchgrids->newEntity($data);
return $matchgrids->save($matchgrid);
}
}
Loading

0 comments on commit 8764496

Please sign in to comment.