This script is plugin_dir/tests/geo_kit_tests.php. It contains all of your tests. The other files are to support this one. (The code was “stolen” from the acts_as_versioned plugin and adapted for the geo_kit plugin.)
<?php error_reporting(E_ALL); defined('DS') ? null : define('DS', DIRECTORY_SEPARATOR); defined('AK_TEST_DATABASE_ON') ? null : define('AK_TEST_DATABASE_ON', true); defined('AK_ACTIVE_RECORD_PROTECT_GET_RECURSION') ? null : define('AK_ACTIVE_RECORD_PROTECT_GET_RECURSION', false); define('AK_BASE_DIR',realpath(dirname(__FILE__).str_repeat(DS.'..',5))); define('GEOKIT_PLUGIN_DIR', AK_BASE_DIR.DS.'app'.DS.'vendor'.DS.'plugins'.DS.'geo_kit'); define('AK_APP_DIR', GEOKIT_PLUGIN_DIR.DS.'test'.DS.'fixtures'.DS.'app'); require_once(AK_BASE_DIR.DS.'test'.DS.'fixtures'.DS.'config'.DS.'config.php'); # This file may not exist. It exists if you had to create one for your # plugin, as I had to for geo_kit require_once(GEOKIT_PLUGIN_DIR.DS.'config'.DS.'config.php'); # This are a plugin code script. Yours, of course, will be different. You may have more than one. $lib_dir = GEOKIT_PLUGIN_DIR.DS.'lib'.DS.'geo_kit'.DS; require_once($lib_dir.'defaults.php'); class GeoKitTestCase extends AkUnitTest { function test_setup() { # Create and populate tables $this->installAndIncludeModels('Company'); $this->installAndIncludeModels('CustomLocation'); $this->installAndIncludeModels('Location'); $this->installAndIncludeModels('Store'); $this->populateTables(array('companies','custom_locations','locations')); $Installer =& new AkInstaller(); } // function test_setup # This function is a modification of a function of the same name in # AK_UNIT_TEST. The changes accomplish two things: # This function looks for the YAML files within the # GEOKIT_PLUGIN_DIR instead of app_home/test/fixtures/data, # where non-plugin testing is done. # It looks for files with the extension "yml" as well as "yaml". # The reason for this is that Rails uses the extension "yml". function populateTables() { $args = func_get_args(); $tables = !empty($args) ? (is_array($args[0]) ? $args[0] : (count($args) > 1 ? $args : Ak::toArray($args))) : array(); foreach ($tables as $table){ $data_dir = GEOKIT_PLUGIN_DIR.DS.'test'.DS.'fixtures'.DS.'data'; $file = $data_dir.DS.$table.'.yaml'; if(!file_exists($file)){ $file = $data_dir.DS.$table.'.yml'; if(!file_exists($file)) { continue; } } $class_name = AkInflector::classify($table); if($this->instantiateModel($class_name)) { $items = Ak::convert('yaml','array',file_get_contents($file)); foreach ($items as $item) { $this->{$class_name}->create($item); } } } } // function populateTables() # This is the first of the tests for geo_kit. # Tests for another plugin will, of course, be different. function test_get_defaults() { $default = new GeoKitDefaults; $units = $default->get('units'); $formula = $default->get('formula'); $this->assertEqual($units, 'miles'); $this->assertEqual($formula,'sphere'); } } // class GeoKitTestCase $use_sessions = true; ak_test('GeoKitTestCase', $use_sessions); ?>
This script resides in plugin_dir/test/fixtures/app/.
<?php require_once(AK_BASE_DIR.DS.'app'.DS.substr(strrchr(__FILE__, DS), 1)); ?>
These reside in plugin_dir/test/fixtures/app/installers/.
The word company in the name of the first file is the singlular form of the table name. The file's contents are those of a typical installer. As you might expect, there needs to be an installer file for each model. Nothing new here.
There is a one-to-many relationship between the company installer and both the location and custom_location installers. This means that the latter two have a company_id field.
<?php class CompanyInstaller extends AkInstaller { function up_1() { $this->createTable('companies', " id, name "); } function down_1() { $this->dropTable('companies'); } } ?>
These are standard Akelos model scripts that describe the models used in testing. They are written in plugin_dir/test/fixtures/app/models. We're listing the scripts to show the fixtures variable as well as the data associations, has_many and belongs_to, which apply to the geo_kit plugin:
<?php class Company extends ActiveRecord { var $fixtures = 'companies.yml'; var $has_many = array('locations','custom_locations'); } ?>
<?php class Location extends ActiveRecord { var $fixtures = 'locations.yml'; var $belongs_to = 'company'; } ?>
<?php class CustomLocation extends ActiveRecord { var $fixtures = 'custom_locations.yml'; var $belongs_to = 'company'; } ?>
<?php class Store extends ActiveRecord { var $fixtures = 'stores.yml'; } ?>
These are configuration files used in testing. They are written in plugin_dir/test/fixtures/app/config.
If the plugin installation changes them, as the geo_kit installation does, these files should be what the app_home/config files would be after the installation. The basic two files are config.php and boot.php. (boot.php is needed because it's included by config.php.)
These YAML files will be used to provide test data to be put into the test tables prior to each test. There is nothing significant to the plugin about the YAML files. An example of one file, companies.yml (or, companies.yaml), should suffice:
starbucks: id: 1 name: Starbucks barnes_and_noble: id: 2 name: Barnes & Noble