ApiTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. namespace Tests\tripal_jbrowse_mgmt;
  3. use StatonLab\TripalTestSuite\DBTransaction;
  4. use StatonLab\TripalTestSuite\TripalTestCase;
  5. use Faker\Factory;
  6. class ApiTest extends TripalTestCase {
  7. // Uncomment to auto start and rollback db transactions per test method.
  8. use DBTransaction;
  9. /**
  10. * Tests the Settings API.
  11. *
  12. * Specifically tripal_jbrowse_mgmt_save_settings()
  13. * and tripal_jbrowse_mgmt_get_settings().
  14. *
  15. * @dataProvider settingsProvider
  16. */
  17. public function testSettings($test_settings) {
  18. tripal_jbrowse_mgmt_save_settings($test_settings);
  19. $retrieved = tripal_jbrowse_mgmt_get_settings();
  20. // Check that all the expected settings are available.
  21. $expected = ['bin_path', 'link', 'data_dir', 'menu_template'];
  22. foreach ($expected as $expected_key) {
  23. $this->assertArrayHasKey($expected_key, $retrieved,
  24. "Retrieved settings do not contain $expected_key");
  25. // Check that the settings match if provided in test.
  26. if (isset($test_settings[$expected_key])) {
  27. $this->assertEquals(
  28. $test_settings[$expected_key],
  29. $retrieved[$expected_key],
  30. "The retrieved value for $expected_key does not match what we set."
  31. );
  32. }
  33. }
  34. }
  35. // Associated Data Provider for testing settings.
  36. public function settingsProvider() {
  37. $faker = Factory::create();
  38. $sets = [];
  39. // No settings.
  40. $sets[] = [[]];
  41. // Full Fake Settings.
  42. $sets[] = [[
  43. 'bin_path' => 'test/fake/path',
  44. 'link' => 'test/fake/path',
  45. 'data_dir' => 'test/fake/path',
  46. 'menu_template' => [],
  47. ]];
  48. return $sets;
  49. }
  50. /**
  51. * Test Instance Create-Retrieve-Update-Delete.
  52. *
  53. * @dataProvider instanceProvider
  54. */
  55. public function testInstanceCRUD($testdata) {
  56. putenv("TRIPAL_SUPPRESS_ERRORS=TRUE");
  57. // Check we cannot create a JBrowse instance without an organism.
  58. $noOrganism = $testdata;
  59. unset($noOrganism['organism_id']);
  60. $id = tripal_jbrowse_mgmt_create_instance($noOrganism);
  61. $this->assertFalse($id,
  62. "Created an instance without an organism_id!?!");
  63. // Now try to create an instance with all the data.
  64. $id = tripal_jbrowse_mgmt_create_instance($testdata);
  65. $this->assertNotFalse($id,
  66. "Unable to create instance.");
  67. // Try to retrieve the instance we just created.
  68. $retrieved_instance = tripal_jbrowse_mgmt_get_instance($id);
  69. $this->assertNotFalse($id, "We did not retrieve an instance?");
  70. $this->assertEquals($id, $retrieved_instance->id,
  71. "We retrieved a different instance then we asked for?");
  72. $this->assertEquals(
  73. $testdata['organism_id'], $retrieved_instance->organism_id,
  74. "Retreived the same instance but the organism is not correct?");
  75. // Change the title and test the instance was updated.
  76. $new_title = $testdata;
  77. $testdata['title'] = 'NEW FAKE TITLE ' . uniqid();
  78. $success = tripal_jbrowse_mgmt_update_instance($id, $new_title);
  79. $this->assertNotFalse($success, 'Unable to update instance title.');
  80. $retrieved_instance = tripal_jbrowse_mgmt_get_instance($id);
  81. $this->assertEquals(
  82. $new_title['title'], $retrieved_instance->title,
  83. "The title was not updated.");
  84. // Finally delete him!
  85. $success = tripal_jbrowse_mgmt_delete_instance($retrieved_instance);
  86. $this->assertNotFalse($success, 'Unable to delete this instance.');
  87. $one_more_time = tripal_jbrowse_mgmt_get_instance($id);
  88. $this->assertFalse($one_more_time);
  89. // and delete with numeric id.
  90. $success = tripal_jbrowse_mgmt_delete_instance($id);
  91. $this->assertNotFalse($success, 'Unable to delete this instance.');
  92. // and catch an exception during delete.
  93. try {
  94. $success = tripal_jbrowse_mgmt_delete_instance('FAKESTRING');
  95. // If we get here without an exception then this test should fail.
  96. $this->assertTrue(FALSE, "Shouldn't be able to extract instance to delete.");
  97. } catch (\Exception $e) {
  98. // Not worried since this is expected!
  99. }
  100. putenv("TRIPAL_SUPPRESS_ERRORS");
  101. }
  102. // Associated Data Provider for testing instances.
  103. public function instanceProvider() {
  104. $faker = Factory::create();
  105. $sets = [];
  106. $organism = factory('chado.organism')->create();
  107. // Full Fake Instance Details.
  108. $sets[] = [[
  109. 'organism_id' => $organism->organism_id,
  110. 'title' => $faker->words(3, TRUE),
  111. 'description' => $faker->sentence(25, TRUE),
  112. 'created_at' => $faker->unixTime(),
  113. 'file' => '/path/to/fake/file',
  114. ]];
  115. return $sets;
  116. }
  117. }