Browse Source

Merge pull request #300 from statonlab/travis_integration

Travis integration
Stephen Ficklin 7 years ago
parent
commit
190a792e84
7 changed files with 152 additions and 0 deletions
  1. 3 0
      .gitignore
  2. 37 0
      .travis.yml
  3. 14 0
      README.md
  4. 5 0
      composer.json
  5. 11 0
      phpunit.xml
  6. 43 0
      tests/TripalJobsTest.test
  7. 39 0
      tests/bootstrap.php

+ 3 - 0
.gitignore

@@ -1 +1,4 @@
 .DS_Store
+composer.lock
+vendor/*
+tests/.env

+ 37 - 0
.travis.yml

@@ -0,0 +1,37 @@
+language: php
+
+services:
+  - docker
+
+sudo: required
+
+before_script:
+  - docker pull statonlab/drupal7
+
+script:
+  # Set branch name
+  - export REPO=$(if [ "$TRAVIS_PULL_REQUEST" == "false" ]; then echo $TRAVIS_REPO_SLUG; else echo $TRAVIS_PULL_REQUEST_SLUG; fi)
+  - export BRANCH=$(if [ "$TRAVIS_PULL_REQUEST" == "false" ]; then echo $TRAVIS_BRANCH; else echo $TRAVIS_PULL_REQUEST_BRANCH; fi)
+  #  Travis does a shallow clone and we need a full clone to test Tripal v2 to v3 upgrade
+  - cd .. && rm -rf tripal && git clone https://github.com/$REPO.git tripal && cd tripal
+  - git checkout $BRANCH
+  # Test tripal 3 installation
+  - docker run -it -d --rm --name tripal3 -v "$(pwd)":/modules/tripal statonlab/drupal7
+  - sleep 15
+  - docker exec -it tripal3 drush en -y tripal tripal_chado tripal_chado_views tripal_ds tripal_ws
+  # Prepare Chado
+  - docker exec -it tripal3 drush eval "module_load_include('inc', 'tripal_chado', 'includes/tripal_chado.install'); tripal_chado_load_drush_submit('Install Chado v1.3');"
+  - docker exec -it tripal3 drush trp-run-jobs --username=admin
+  # Prepare Drupal
+  - docker exec -it tripal3 drush eval "module_load_include('inc', 'tripal_chado', 'includes/setup/tripal_chado.setup'); tripal_chado_prepare_drush_submit();"
+  - docker exec -it tripal3 drush trp-run-jobs --username=admin
+  # Run PHPUnit tests
+  - docker exec -it tripal3 bash -c "cd /modules/tripal && composer install && DRUPAL_ROOT=/var/www/html ./vendor/bin/phpunit"
+  # Test Tripal v2 to v3 upgrade steps
+  - git checkout 7.x-2.x
+  - docker run -it -d --rm --name tripal2 -v "$(pwd)":/modules/tripal statonlab/drupal7
+  - sleep 15
+  - docker exec -it tripal2 drush en -y tripal_core
+  - docker exec -it tripal2 drush pm-disable tripal_core -y
+  - git checkout $BRANCH
+  - docker exec -it tripal2 drush en -y tripal

+ 14 - 0
README.md

@@ -1,3 +1,5 @@
+[![7.x-3.x Build Status](https://travis-ci.org/tripal/tripal.svg?branch=7.x-3.x)](https://travis-ci.org/tripal/tripal)
+
 ![alt tag](https://raw.githubusercontent.com/tripal/tripal/7.x-3.x/tripal/theme/images/tripal_logo.png)
 
 Tripal is a toolkit for construction of online biological (genetics, genomics,
@@ -111,3 +113,15 @@ precise customizations as required by the community. A well-developed
 Tripal API provides a uniform set of variables and functions for 
 accessing any and all data within the Chado database. See the Tripal 3.x
 Developer's Handbook for additional details.
+
+
+# Development Testing
+
+To run PHP unit tests on your local system, simply create a `.env` file in your `/Tests/` directory that defines the `DRUPAL_ROOT` variable, for example 
+
+```
+DRUPAL_ROOT=/var/www/html
+```
+Then run PHPUnit from your root Tripal directory.
+
+PHPUnit tests will also be run in the Travis CI build.

+ 5 - 0
composer.json

@@ -0,0 +1,5 @@
+{
+  "require-dev": {
+    "phpunit/phpunit": "^7"
+  }
+}

+ 11 - 0
phpunit.xml

@@ -0,0 +1,11 @@
+<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:noNamespaceSchemaLocation="phpunit.xsd"
+         bootstrap="tests/bootstrap.php"
+         verbose="true"
+         colors="true">
+    <testsuites>
+        <testsuite name="default">
+            <directory suffix=".test">./tests/</directory>
+        </testsuite>
+    </testsuites>
+</phpunit>

+ 43 - 0
tests/TripalJobsTest.test

@@ -0,0 +1,43 @@
+<?php
+
+use PHPUnit\Framework\TestCase;
+
+class TripalJobsTest extends TestCase {
+
+  /**
+   * Holds the job id to delete when done.
+   *
+   * @var int|bool
+   */
+  protected $job_id;
+
+  /**
+   * Tests the ability to create a tripal job.
+   *
+   * @test
+   */
+  public function should_create_a_tripal_job() {
+    $this->job_id = tripal_add_job('Test adding jobs', 'test_module', 'test_callback', [], 1);
+    $this->assertTrue(is_numeric($this->job_id));
+  }
+
+  /**
+   * Tests whether creating a duplicate job is possible.
+   *
+   * @test
+   */
+  public function should_refuse_to_create_duplicate_job() {
+    $job = tripal_add_job('Test adding jobs', 'test_module', 'test_callback', [], 1, 10, TRUE);
+    $this->assertEmpty($job);
+  }
+
+  /**
+   * Run clean up
+   */
+  public function __destruct() {
+    // Clean up any data inserted to the DB
+    if ($this->job_id) {
+      db_query('DELETE FROM {tripal_jobs} WHERE job_id = :id', [':id' => $this->job_id]);
+    }
+  }
+}

+ 39 - 0
tests/bootstrap.php

@@ -0,0 +1,39 @@
+<?php
+// Set environment variables
+test_suite_read_and_set_environment_variables();
+
+// Get Drupal root path
+$drupal_root = getenv('DRUPAL_ROOT');
+define('DRUPAL_ROOT', $drupal_root ?: '/var/www/html');
+
+// Get Drupal bootstrap functions
+require_once DRUPAL_ROOT.'/includes/bootstrap.inc';
+$_SERVER['REMOTE_ADDR'] = '127.0.0.1';
+
+// Bootstrap Drupal.
+$current_dir = getcwd();
+chdir(DRUPAL_ROOT);
+drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
+chdir($current_dir);
+
+/**
+ * Get and set environment variables from .env file if it exists.
+ *
+ * @throws \Exception
+ */
+function test_suite_read_and_set_environment_variables() {
+  $filename = __DIR__.'/.env';
+  if(file_exists($filename)) {
+    $file = fopen($filename, 'r');
+    while ($line = str_replace("\n", '', fgets($file))) {
+      // break line into key value
+      $env = explode('=', $line);
+      if(count($env) === 2) {
+        putenv($line);
+      } else {
+        throw new Exception('Invalid environment line: ' . $line);
+      }
+    }
+    fclose($file);
+  }
+}