Skip to content

Commit

Permalink
Merge pull request #1541 from tripal/tv4g9-issue1539-testDirCleanup
Browse files Browse the repository at this point in the history
Restructure Kernel Tests
  • Loading branch information
laceysanderson committed Jun 8, 2023
2 parents 73c091e + 86e776d commit 7ee866f
Show file tree
Hide file tree
Showing 13 changed files with 181 additions and 121 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Drupal\Tests\tripal\Functional\TripalDBX;
namespace Drupal\Tests\tripal\Kernel\TripalDBX;

use Drupal\KernelTests\KernelTestBase;
use Drupal\tripal\TripalDBX\TripalDbxConnection;
Expand Down Expand Up @@ -115,7 +115,7 @@ protected function getConnectionMock(
->expects($this->any())
->method('getTripalDbxClass')
->with('Schema')
->willReturn('\Drupal\Tests\tripal\Functional\TripalDBX\Subclass\TripalDbxSchemaFake');
->willReturn('\Drupal\Tests\tripal\Kernel\TripalDBX\Subclass\TripalDbxSchemaFake');

// Return initialized mock.
return $dbmock;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Drupal\Tests\tripal\Functional\TripalDBX;
namespace Drupal\Tests\tripal\Kernel\TripalDBX;

use Drupal\KernelTests\KernelTestBase;
use Drupal\tripal\TripalDBX\TripalDbxSchema;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php

namespace Drupal\Tests\tripal\Functional\TripalDBX\Subclass;
namespace Drupal\Tests\tripal\Kernel\TripalDBX\Subclass;

use Drupal\tripal\TripalDBX\TripalDbxConnection;
use Drupal\Tests\tripal\Functional\TripalDBX\Subclass\TripalDbxSchemaFake;
use Drupal\Tests\tripal\Kernel\TripalDBX\Subclass\TripalDbxSchemaFake;

/**
* Fake connection class.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Drupal\Tests\tripal\Functional\TripalDBX\Subclass;
namespace Drupal\Tests\tripal\Kernel\TripalDBX\Subclass;

use Drupal\tripal\TripalDBX\TripalDbxSchema;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Drupal\Tests\tripal\Functional\TripalDBX;
namespace Drupal\Tests\tripal\Kernel\TripalDBX;

use Drupal\KernelTests\KernelTestBase;
use Drupal\tripal\TripalDBX\TripalDbx;
Expand Down
97 changes: 0 additions & 97 deletions tripal_biodb/tests/src/Functional/Task/BioTaskBaseTest.php

This file was deleted.

153 changes: 153 additions & 0 deletions tripal_biodb/tests/src/Kernel/Task/BioTaskBaseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
<?php

namespace Drupal\Tests\tripal_biodb\Kernel\Task;

use Drupal\KernelTests\KernelTestBase;
use Drupal\tripal_biodb\Task\BioTaskBase;
use Drupal\Tests\tripal\Kernel\TripalDBX\Subclass\TripalDbxConnectionFake;

/**
* Tests for tasks.
*
* @coversDefaultClass \Drupal\tripal_biodb\Task\BioTaskBase
*
* @group Tripal
* @group Tripal BioDb
* @group Tripal BioDb Task
*/
class BioTaskBaseTest extends KernelTestBase {

/**
* Test members.
*
* "pro*" members are prophesize objects while their "non-pro*" equivqlent are
* the revealed objects.
*/
protected $proConfigFactory;
protected $configFactory;
protected $proConfig;
protected $config;

/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
// Register Tripal DBX service.
$this->enableModules(['tripal']);
$this->enableModules(['tripal_biodb']);
}

/**
* Setup a mock version of the abstract Bi9oTaskBase for testing.
*/
public function getMock() {

// Create a mock for the abstract class
// but specify not to run the contructor + mention this is an abstract class.
$tmock = $this->getMockBuilder(\Drupal\tripal_biodb\Task\BioTaskBase::class)
->setMethods(['getTripalDbxClass'])
->getMockForAbstractClass();
// Ensure when getTripalDbxClass() is asked for the connection class, it returns our fake class.
$tmock
->expects($this->any())
->method('getTripalDbxClass')
->with('Connection')
->willReturn('\Drupal\Tests\tripal\Kernel\TripalDBX\Subclass\TripalDbxConnectionFake');

return $tmock;
}

/**
* Tests constructor: check constructor calls.
*
* @cover ::__construct
* @cover ::initId
* @cover ::getId
* @cover ::getLogger
*/
public function testBioTaskBaseConstructor() {

$tmock = $this->getMock();

// Because we did not disable the constructor in the above mock,
// it should have been run when we created the mock.
// Since the variables set by the constructor are protected properties,
// we cannot test them directly. As such, we will use PHP closures to
// access these properties for testing.
// -- Create a variable to store a copy of this test object for use within the closure.
$that = $this;
// -- Create a closure (i.e. a function tied to a variable) that does not need any parameters.
// Within this function we will want all of the assertions we will use to test the private methods.
// Also, $this within the function will actually be the plugin object that you bind later (mind blown).
$assertConstructorClosure = function () use ($that){
$that->assertIsObject($this->connection,
"The connection object was not set properly by our constructor.");
$that->assertIsObject($this->logger,
"The logger object was not set properly by our constructor.");
$that->assertIsObject($this->locker,
"The locker object was not set properly by our constructor.");
$that->assertIsObject($this->state,
"The state object was not set properly by our constructor.");
$that->assertIsString($this->id,
"The id was not set properly by our constructor.");

$that->assertEquals($this->id, $this->getId(),
"Retrieving the ID did not return the id set in the object.");
$that->assertEquals($this->logger, $this->getLogger(),
"Retrieving the logger did not return the logger set in the object.");
};
// -- Now, bind our assertion closure to the $plugin object. This is what makes the plugin available
// inside the function.
$doAssertConstructorClosure = $assertConstructorClosure->bindTo($tmock, get_class($tmock));
// -- Finally, call our bound closure function to run the assertions on our plugin.
$doAssertConstructorClosure();
}

/**
* Tests setting + preparing input/output schema.
*
* @cover ::setParameters
* @cover ::prepareSchemas
*/
public function testBioTaskBaseParameters() {

$tmock = $this->getMock();

// Check that input/output schema can be set and prepared.
$parameters = [
'input_schemas' => ['insch'],
'output_schemas' => ['outsch'],
];
$tmock->setParameters($parameters);
$that = $this;
$assertParametersClosure = function () use ($that, $parameters){
$that->assertIsArray($this->parameters, "Parameters should be an array.");
$that->assertArrayHasKey('input_schemas', $this->parameters,
"Input schema key not set in parameters array.");
$that->assertArrayHasKey('output_schemas', $this->parameters,
"Output schema key not set in parameters array.");
$that->assertEquals($parameters, $this->parameters,
"The parameters we passed in should match those set withing the object.");

// Check that the prepareSchema function was run for both input and output
// schema and that TripalDbxConnection were successfully initialized.
$that->assertCount(1, $this->inputSchemas,
"We expect there should be one input schema based on the parameters passed in.");
$that->assertContainsOnlyInstancesOf(
\Drupal\tripal\TripalDBX\TripalDbxConnection::class,
$this->inputSchemas,
"All input schema should be prepared as TripalDBXConnection objects but are not."
);
$that->assertCount(1, $this->outputSchemas,
"We expect there should be one output schema based on the parameters passed in.");
$that->assertContainsOnlyInstancesOf(
\Drupal\tripal\TripalDBX\TripalDbxConnection::class,
$this->outputSchemas,
"All output schema should be prepared as TripalDBXConnection objects but are not."
);
};
$doAssertParametersClosure = $assertParametersClosure->bindTo($tmock, get_class($tmock));
$doAssertParametersClosure();
}
}
2 changes: 1 addition & 1 deletion tripal_chado/src/Task/ChadoUpgrader.php
Original file line number Diff line number Diff line change
Expand Up @@ -614,7 +614,7 @@ protected function setupReferenceSchema() {
. $version
. '.sql'
;
$success = $this->executeSqlFile(
$success = $ref_schema->executeSqlFile(
$file_path,
['chado' => $ref_schema->getQuotedSchemaName(),]
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<?php
namespace Drupal\Tests\tripal_chado\Functional;
namespace Drupal\Tests\tripal_chado\Kernel;

use Drupal\KernelTests\KernelTestBase;
use Drupal\tripal\TripalDBX\TripalDbx;
use Drupal\tripal_chado\Database\ChadoConnection;
use Drupal\Tests\tripal_chado\Functional\ChadoTestTrait;

/**
* This is a base class for Chado tests.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php

namespace Drupal\Tests\tripal_chado\Functional\Task;
namespace Drupal\Tests\tripal_chado\Kernel\Task;

use Drupal\Tests\tripal_chado\Functional\ChadoTestKernelBase;
use Drupal\Tests\tripal_chado\Kernel\ChadoTestKernelBase;
use Drupal\tripal_chado\Task\ChadoCloner;


Expand All @@ -16,7 +16,7 @@
* @group Tripal Chado Task
* @group Tripal Chado Cloner
*/
class ChadoClonerFunctionalTest extends ChadoTestKernelBase {
class ChadoClonerTest extends ChadoTestKernelBase {

/**
* Tests task.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php

namespace Drupal\Tests\tripal_chado\Functional\Task;
namespace Drupal\Tests\tripal_chado\Kernel\Task;

use Drupal\Tests\tripal_chado\Functional\ChadoTestKernelBase;
use Drupal\Tests\tripal_chado\Kernel\ChadoTestKernelBase;
use Drupal\tripal_chado\Task\ChadoInstaller;


Expand All @@ -16,7 +16,7 @@
* @group Tripal Chado Task
* @group Tripal Chado Installer
*/
class ChadoInstallerFunctionalTest extends ChadoTestKernelBase {
class ChadoInstallerTest extends ChadoTestKernelBase {

/**
* Tests task.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php

namespace Drupal\Tests\tripal_chado\Functional\Task;
namespace Drupal\Tests\tripal_chado\Kernel\Task;

use Drupal\Tests\tripal_chado\Functional\ChadoTestKernelBase;
use Drupal\Tests\tripal_chado\Kernel\ChadoTestKernelBase;
use Drupal\tripal_chado\Task\ChadoRenamer;


Expand All @@ -16,7 +16,7 @@
* @group Tripal Chado Task
* @group Tripal Chado Renamer
*/
class ChadoRenamerFunctionalTest extends ChadoTestKernelBase {
class ChadoRenamerTest extends ChadoTestKernelBase {

/**
* Tests task.
Expand Down

0 comments on commit 7ee866f

Please sign in to comment.