Skip to content

Commit

Permalink
Merge pull request #16 from statonlab/correct_auto_setup
Browse files Browse the repository at this point in the history
Correct auto setup
  • Loading branch information
bradfordcondon authored Mar 21, 2018
2 parents 60f903e + 1240d99 commit 22ffa92
Show file tree
Hide file tree
Showing 12 changed files with 80 additions and 73 deletions.
8 changes: 8 additions & 0 deletions src/Mocks/TripalTestCaseMock.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php
namespace StatonLab\TripalTestSuite\Mocks;

use StatonLab\TripalTestSuite\TripalTestCase;

class TripalTestCaseMock extends TripalTestCase
{
}
30 changes: 25 additions & 5 deletions src/SetUp.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ public function run()
$this->root = $root;
$this->vendor_root = __DIR__ . '/../';

$position = strrpos($root,'/') + 1;
$module_name = substr($root,$position);
$position = strrpos($root, '/') + 1;
$module_name = substr($root, $position);

$this->module_name = $module_name;
print("\nmodule: ". $module_name . " \n");
$this->setUpTests();
$this->addTravis();

$this->append_to_gitignore();
}


Expand Down Expand Up @@ -79,16 +79,36 @@ protected function addTravis()
copy($vendor_root . "stubs/travis.yml", $root . "/.travis.yml");
//Replace the MODULE_NAME variable with the module name
$this->replace_string_in_file($root . "/.travis.yml", "MODULE_NAME", $this->module_name);
}
}

/**
*
* Check if theres a gitignore in the project root. if no, create and append vendor folder.
* If yes, check if vendor is in there and append it if it isnt.
*/

protected function append_to_gitignore()
{
$root = $this->root;
$filepath = $root . "/.gitignore";

if (!file_exists($filepath)) {
$fh = fopen($filepath, 'w');
fwrite($fh, "\nvendor/\n");
} else {
if (strpos(file_get_contents($filepath), "vendor/") == false) {
$fh = fopen($filepath, 'a');
fwrite($fh, "\nvendor/\n");
}
}
}

protected function replace_string_in_file($filename, $string_to_replace, $replace_with){
protected function replace_string_in_file($filename, $string_to_replace, $replace_with)
{
$content=file_get_contents($filename);
$content_chunks=explode($string_to_replace, $content);
$content=implode($replace_with, $content_chunks);
file_put_contents($filename, $content);
}

}
2 changes: 1 addition & 1 deletion src/TripalTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use PHPUnit\Framework\TestCase;
use StatonLab\TripalTestSuite\Exceptions\TripalTestSuiteException;

class TripalTestCase extends TestCase
abstract class TripalTestCase extends TestCase
{
/**
* @var bool
Expand Down
12 changes: 4 additions & 8 deletions stubs/TripalExampleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,9 @@

use StatonLab\TripalTestSuite\TripalTestCase;

class MyTest extends TripalTestCase {


public function exampleTest() {

$this->assertTrue(true);

}
class TripalTestCase extends TripalTestCase {

public function testExampleTest() {
$this->assertTrue(true);
}
}
11 changes: 1 addition & 10 deletions stubs/bootstrap.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,2 @@
<?php
load_php_files('TripalTestSuite');
load_php_files('TripalTestSuite/Exceptions');

function load_php_files($dir) {
$dir = __DIR__.'/'.$dir;

foreach (glob($dir.'/*.php') as $file) {
require_once $file;
}
}
require_once __DIR__.'/../vendor/autoload.php';
8 changes: 7 additions & 1 deletion stubs/phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="phpunit.xsd"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
bootstrap="tests/bootstrap.php"
verbose="true"
colors="true">
colors="true">
<testsuites>
<testsuite name="default">
<directory suffix="Test.php">./tests/</directory>
<exclude>./tests/bootstrap.php</exclude>
</testsuite>
</testsuites>
</phpunit>
4 changes: 2 additions & 2 deletions stubs/travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ before_script:
- docker pull statonlab/tripal3

script:
- docker run -it -d --rm --name tripal -v "$(pwd)":/var/www/html/sites/all/modules/custom/MODULE_NAME statonlab/tripal3
- docker run -it -d --rm --name tripal -v "$(pwd)":/modules/MODULE_NAME statonlab/tripal3
- sleep 30 #We pause here so drush won't fail
- docker exec -it tripal drush pm-enable -y MODULE_NAME
- docker exec -it tripal bash -c 'cd /var/www/html/sites/all/modules/custom/MODULE_NAME/ &&composer install && DRUPAL_ROOT="/var/www/html" ./vendor/bin/phpunit'
- docker exec -it tripal bash -c 'cd /modules/MODULE_NAME/ && composer install && DRUPAL_ROOT="/var/www/html" ./vendor/bin/phpunit'
40 changes: 17 additions & 23 deletions tests/SetUpTest.php
Original file line number Diff line number Diff line change
@@ -1,40 +1,34 @@
<?php

declare(strict_types=1);

use PHPUnit\Framework\TestCase;

require_once(__DIR__ . '/../src/SetUp.php');
require_once __DIR__.'/../src/SetUp.php';

final class SetUpTest extends TestCase
class SetUpTest extends TestCase
{
private $set_up;

/**
* Initialize.
*/
protected function setUp()
{
$this->set_up = new StatonLab\TripalTestSuite\SetUp();
}
protected function tearDown()
{
$this->set_up = null;
}

/**
*Assert that the Test Set Up constructs
*
**/
* Assert that the Test Set Up constructs
*/
public function testAdd()
{
$set_up = $this->set_up;
$this->assertInstanceOf("StatonLab\TripalTestSuite\SetUp", $set_up);
$this->assertInstanceOf("StatonLab\TripalTestSuite\SetUp", $this->set_up);
}

/**
* Tests that the setup method creates all expected folders and files exist.
* We also include a $cleanup variable to signal if the created file should be deleted after (the example env file)
* Or if it shoul be left (The tests folder, where this test lives!)
*/

* Tests that the setup method creates all expected folders and files exist.
* We also include a $cleanup variable to signal if the created file should be deleted after (the example env file)
* Or if it should be left (The tests folder, where this test lives!)
*/
public function testCreateTestDirectory()
{
$dir = __DIR__;
Expand All @@ -43,11 +37,11 @@ public function testCreateTestDirectory()
$set_up->run();

$tests = [
[$dir . "/TripalExampleTest.php", true, "The example test was not created"],
[$dir . "/example.env", true, "The example environment file was not created."],
[$dir . "/../.travis.yml", false ,"The travis.yml file was not created"],
[$dir . "/bootstrap.php", false, "The bootstrap file was not copied to test."]
];
[$dir."/TripalExampleTest.php", true, "The example test was not created"],
[$dir."/example.env", true, "The example environment file was not created."],
[$dir."/../.travis.yml", false, "The travis.yml file was not created"],
[$dir."/bootstrap.php", false, "The bootstrap file was not copied to test."],
];

foreach ($tests as $file_to_check) {
$file = $file_to_check[0];
Expand Down
11 changes: 1 addition & 10 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,2 @@
<?php
load_php_files('TripalTestSuite');
load_php_files('TripalTestSuite/Exceptions');

function load_php_files($dir) {
$dir = __DIR__.'/'.$dir;

foreach (glob($dir.'/*.php') as $file) {
require_once $file;
}
}
require_once __DIR__.'/../vendor/autoload.php';
16 changes: 8 additions & 8 deletions tests/test_module/tests/Feature/BootstrapTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
namespace Test\Feature;

use PHPUnit\Framework\TestCase;
use StatonLab\TripalTestSuite\TripalTestCase;
use StatonLab\TripalTestSuite\Mocks\TripalTestCaseMock;

class BootstrapTest extends TestCase {
/**
Expand All @@ -11,8 +11,8 @@ class BootstrapTest extends TestCase {
* @throws \ReflectionException
* @test
*/
public function should_find_drupal_root() {
$test_case = new TripalTestCase();
public function shouldFindDrupalRoot() {
$test_case = new TripalTestCaseMock();
$method = $this->getMethod('_getDrupalRoot');
$drupal_root = $method->invoke($test_case);

Expand All @@ -26,8 +26,8 @@ public function should_find_drupal_root() {
* @throws \ReflectionException
* @test
*/
public function should_find_env_file_successfully() {
$test_case = new TripalTestCase();
public function shouldFindEnvFileSuccessfully() {
$test_case = new TripalTestCaseMock();

$method = $this->getMethod('_getEnvironmentFilePath');
$env_file_path = $method->invoke($test_case);
Expand All @@ -43,8 +43,8 @@ public function should_find_env_file_successfully() {
* @throws \ReflectionException
* @test
*/
public function should_successfully_bootstrap_drupal() {
$test_case = new TripalTestCase();
public function shouldSuccessfullyBootstrapDrupal() {
$test_case = new TripalTestCaseMock();
$method = $this->getMethod('_bootstrapDrupal');
$method->invoke($test_case);

Expand All @@ -69,7 +69,7 @@ public function should_successfully_bootstrap_drupal() {
*/
protected function getMethod($method_name, $class_name = '') {
if(empty($class_name)) {
$class_name = TripalTestCase::class;
$class_name = TripalTestCaseMock::class;
}

$reflection = new \ReflectionClass($class_name);
Expand Down
10 changes: 5 additions & 5 deletions tests/test_module/tests/Feature/DBTransactionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use PHPUnit\Framework\TestCase;
use StatonLab\TripalTestSuite\DBTransaction;
use StatonLab\TripalTestSuite\TripalTestCase;
use StatonLab\TripalTestSuite\Mocks\TripalTestCaseMock;

class DBTransactionTest extends TestCase
{
Expand All @@ -17,21 +17,21 @@ class DBTransactionTest extends TestCase
*/
protected function setUp()
{
$test_case = new TripalTestCase();
$test_case = new TripalTestCaseMock();
$method = $this->getMethod('_bootstrapDrupal');
$method->invoke($test_case);
}

/** @test */
public function should_start_a_transaction()
public function shouldStartATransaction()
{
$this->DBTransactionSetUp();
$this->assertInstanceOf(\DatabaseTransaction::class, $this->_transaction);
$this->DBTransactionTearDown();
}

/** @test */
public function should_fail_to_find_record_after_transaction_has_ended()
public function shouldFailToFindRecordAfterTransactionHasEnded()
{
$this->DBTransactionSetUp();

Expand Down Expand Up @@ -68,7 +68,7 @@ public function should_fail_to_find_record_after_transaction_has_ended()
protected function getMethod($method_name, $class_name = '')
{
if (empty($class_name)) {
$class_name = TripalTestCase::class;
$class_name = TripalTestCaseMock::class;
}

$reflection = new \ReflectionClass($class_name);
Expand Down
1 change: 1 addition & 0 deletions tests/test_module/tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
load_php_files('TripalTestSuite');
load_php_files('TripalTestSuite/Exceptions');
load_php_files('TripalTestSuite/Mocks');

function load_php_files($dir) {
$dir = __DIR__.'/'.$dir;
Expand Down

0 comments on commit 22ffa92

Please sign in to comment.