123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191 |
- <?php
- namespace Tests;
- use PHPUnit\Exception;
- use Faker\Factory;
- use StatonLab\TripalTestSuite\DBTransaction;
- use StatonLab\TripalTestSuite\TripalTestCase;
- module_load_include('inc', 'tripal_chado', 'includes/api/ChadoRecord');
- class ChadoRecordTest extends TripalTestCase {
- use DBTransaction;
- /**
- * Data provider. A variety of chado records.
- *
- * @return array
- */
- public function recordProvider() {
- //table, factory or NULL, record_id or NULL
- $faker = \Faker\Factory::create();
- $analysis = [
- 'name' => $faker->word,
- 'description' => $faker->text,
- 'program' => $faker->word,
- 'programversion' => $faker->word,
- ];
- $organism = [
- 'genus' => $faker->word,
- 'species' => $faker->word,
- 'common_name' => $faker->word,
- ];
- return [
- ['analysis', $analysis],
- ['organism', $organism],
- ];
- }
- /**
- * Tests that the class can be initiated with or without a record specified
- *
- * @group api
- * @group chado
- * @group wip
- * @dataProvider recordProvider
- */
- public function testInitClass($table, $values) {
- $record = new \ChadoRecord($table);
- $this->assertNotNull($record);
- $chado_record = factory('chado.' . $table)->create($values);
- $record_column = $table.'_id';
- $record = new \ChadoRecord($table, $chado_record->$record_column);
- $this->assertNotNull($record);
- }
- /**
- * @group api
- * @group chado
- * @group wip
- * @throws \Exception
- * @dataProvider recordProvider
- */
- public function testGetTable($table, $values) {
- $record = new \ChadoRecord($table);
- $this->assertEquals($table, $record->getTable());
- }
- /**
- * @group wip
- * @group api
- * @group chado
- * @dataProvider recordProvider
- *
- * @throws \Exception
- */
- public function testGetID($table, $values) {
- $chado_record = factory('chado.' . $table)->create();
- $record_column = $table.'_id';
- $id = $chado_record->$record_column;
- $record = new \ChadoRecord($table, $id);
- $returned_id = $record->getID();
- $this->assertEquals($id, $returned_id);
- }
- /**
- * @group api
- * @group wip
- * @group chado
- * @dataProvider recordProvider
- *
- *
- */
- public function testGetValues($table, $values) {
- $chado_record = factory('chado.' . $table)->create($values);
- $record_column = $table.'_id';
- $id = $chado_record->$record_column;
- $record = new \ChadoRecord($table, $id);
- $values = $record->getValues();
- $this->assertNotEmpty($values);
- foreach ($values as $key => $value) {
- $this->assertArrayHasKey($key, $values);
- $this->assertEquals($value, $values[$key]);
- }
- }
- /**
- * @group api
- * @group wip
- * @group chado
- * @dataProvider recordProvider
- *
- */
- public function testGetValue($table, $values) {
- $chado_record = factory('chado.' . $table)->create($values);
- $record_column = $table.'_id';
- $id = $chado_record->$record_column;
- $record = new \ChadoRecord($table, $id);
- foreach ($values as $key => $value) {
- $returned_value = $record->getValue($key);
- $this->assertEquals($value, $returned_value);
- }
- }
- /**
- * @group wip
- * @group chado
- * @group api
- * @dataProvider recordProvider
- */
- public function testFind($table, $values) {
- $chado_record = factory('chado.' . $table)->create($values);
- $record_column = $table.'_id';
- $id = $chado_record->$record_column;
-
- $record = new \ChadoRecord($table);
- $record->setValues($values);
- $found = $record->find();
- $this->assertNotNull($found);
- $this->assertEquals(1, $found);
- }
- /**
- * Check that the find method throws an exception when it cant find anything.
- *
- * @throws \Exception
- */
- public function testFindFail() {
- $table = 'organism';
- $record = new \ChadoRecord($table);
- $record->setValue($table . '_id', 'unfindable');
- $this->expectException(Exception);
- $found = $record->find();
- }
- /**
- * @param $table
- * @param $values
- *
- * @throws \Exception
- */
- public function testSetandGetValue($table, $values) {
- $record = new \ChadoRecord($table);
- $record->setValues($values);
- $vals = $record->getValues();
- foreach ($vals as $val_key => $val) {
- $this->assertEquals($values[$val_key], $val, "The getValues did not match what was provided for setValues");
- }
- }
- }
|