comanage-dev - [comanage-dev] input on format of tests
Subject: COmanage Developers List
List archive
- From: Scott Koranda <>
- To: comanage-dev <>
- Subject: [comanage-dev] input on format of tests
- Date: Tue, 9 Jun 2015 10:58:55 -0500
Hi,
The files CoTest.php and CoFixture.php are attached.
CoTest.php contains the tests for the Co.php model and
CoFixture.php contains code to create a fixture that is used
by the tests.
Class CoTest includes two tests now, one for each of the
methods afterSave() and setup() that are defined for the class
Co, excluding inherited methods.
I am taking the approach that for unit testing we want to just
test the code we have written--we are not trying to test that
database saves work, or schema imports work, and so on. So the
tests just attempt to make sure that each of those two
functions is invoked and invoked correctly.
More specifically, the setup() method for Co right now only
"...consists of setting up the default values for extended
types." So the approach I am taking is that I test to make
sure that when setup() is invoked on a Co this line of code
$this->CoExtendedType->addDefaults($coId)
is executed. I do that by checking that the Co ends up being
linked to an associated CoExtendedType model instance, but I
do NOT test that the CoExtendedType model instance has a
particular value, ie. defaults. I figure that should go in the
unit test for CoExtendedType and not for Co.
The comments in the test code attempt to explain this.
I would like to get your feedback on
- the general approach
- the syntax and comment structure of the test
- the syntax and comment structure of the fixture
before I engage in writting a lot of tests.
Thanks,
Scott
P.S. I will write wiki instructions for how you take a
COmanage deployment and prepare it to run these tests.
<?php
/**
* COmanage Registry CO Model Test
*
* Copyright (C) 2015 University Corporation for Advanced Internet
Development, Inc.
*
* Licensed 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.
*
* @copyright Copyright (C) 2015 University Corporation for Advanced
Internet Development, Inc.
* @link http://www.internet2.edu/comanage COmanage Project
* @package registry
* @since COmanage Registry v0.9.4
* @license Apache License, Version 2.0
(http://www.apache.org/licenses/LICENSE-2.0)
* @version $Id$
*/
App::uses('Co', 'Model');
class CoTest extends CakeTestCase {
public $fixtures = array('app.co');
public function setUp() {
parent::setUp();
$this->Co = ClassRegistry::init('Co');
}
/**
* Test afterSave method of class Co.
*
* We test the afterSave method by creating and saving a new CO.
* To check that afterSave is correctly invoked we check that
* for the new CO the associated model CoExtendedType is created.
* We do not check the actual contents of the CoExtendedType instance
* here and leave that to the unit test for the CoExtendedType model.
*
* @since COmanage Registry v0.9.4
*/
public function testAfterSave() {
// Data for a new CO.
$data = array();
$data['Co']['name'] = 'A new CO';
$data['Co']['description'] = 'Description for A new CO';
$data['Co']['status'] = 'A';
// Save the data. The method afterSave() should be automatically invoked.
$this->Co->save($data);
// Query to find the Co just created and use containable behavior
// to include CoExtendedType if linked to the Co.
$params = array();
$params['conditions'] = array();
$params['conditions']['Co.name'] = 'A new CO';
$params['contain'] = array();
$params['contain'][] = 'CoExtendedType';
$co = $this->Co->find('first', $params);
// We just test for presence of the Co and CoExtendedType keys.
$keys = array_keys($co);
ksort($keys);
$result = $keys;
$expected = array();
$expected[] = 'Co';
$expected[] = 'CoExtendedType';
$this->assertEquals($expected, $result);
}
/**
* Test setup method of class Co.
*
* We test the setup method by checking that the associated model
* CoExtendedType is created. We do not check the actual contents
* of the CoExtendedType instance here and leave that to the
* unit test for the CoExtendedType model.
*
* @since COmanage Registry v0.9.4
*/
public function testSetup() {
// Invoke the setup() method for Co with id 2 from the fixture.
$this->Co->setup(2);
// Query to find the Co with id 2 and use containable behavior
// to include CoExtendedType if linked to the Co.
$params = array();
$params['conditions'] = array();
$params['conditions']['Co.id'] = 2;
$params['contain'] = array();
$params['contain'][] = 'CoExtendedType';
$co = $this->Co->find('first', $params);
// We just test for presence of the Co and CoExtendedType keys.
$keys = array_keys($co);
ksort($keys);
$result = $keys;
$expected = array();
$expected[] = 'Co';
$expected[] = 'CoExtendedType';
$this->assertEquals($expected, $result);
}
}
<?php
/**
* COmanage Registry CO Fixture
*
* Copyright (C) 2015 University Corporation for Advanced Internet
Development, Inc.
*
* Licensed 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.
*
* @copyright Copyright (C) 2015 University Corporation for Advanced
Internet Development, Inc.
* @link http://www.internet2.edu/comanage COmanage Project
* @package registry
* @since COmanage Registry v0.9.4
* @license Apache License, Version 2.0
(http://www.apache.org/licenses/LICENSE-2.0)
* @version $Id$
*/
class CoFixture extends CakeTestFixture {
// Import schema for the model from the default database.
// The fixture data itself will be written to test and
// not default.
public $import = array('model' => 'Co', 'connection' => 'default');
public function init() {
$records = array();
// Mimic the internal CO always created during deployment.
$arecord = array();
$arecord['id'] = 1;
$arecord['name'] = 'COmanage';
$arecord['description'] = 'COmanage Registry Internal CO';
$arecord['status'] = 'A';
$arecord['created'] = date('Y-m-d H:i:s');
$arecord['modified'] = date('Y-m-d H:i:s');
$records[] = $arecord;
// A second CO.
$arecord = array();
$arecord['id'] = 2;
$arecord['name'] = 'Test CO 1';
$arecord['description'] = 'Description for Test CO 1';
$arecord['status'] = 'A';
$arecord['created'] = date('Y-m-d H:i:s');
$arecord['modified'] = date('Y-m-d H:i:s');
$records[] = $arecord;
$this->records = $records;
parent::init();
}
}
- [comanage-dev] input on format of tests, Scott Koranda, 06/09/2015
Archive powered by MHonArc 2.6.16.