Project

General

Profile

Actions

Task #72450

closed

Instance path for test instance configuration

Added by Artus Kolanowski almost 9 years ago. Updated about 6 years ago.

Status:
Closed
Priority:
Could have
Assignee:
-
Category:
Tests
Target version:
-
Start date:
2015-12-27
Due date:
% Done:

100%

Estimated time:
TYPO3 Version:
6.2
PHP Version:
Tags:
Complexity:
easy
Sprint Focus:

Description

In a functional test I need to test the result of a mail function. My approach is to use mbox as transport. But mbox needs an absolute path which should be a folder within the test instance. The problem is that the instance path private and it's set after the configuration was written and thus I can't use a path within the test instance. So it would be very helpful to have the instance path as constant in the functional test case. My proposal would be a dynamic generated constant FULL_QUALIFIED_TEST_CASE_CLASS_NAME_PATH which contains the instance path and is available on object instance level (so not static). Additionally the class `\TYPO3\CMS\Core\Tests\FunctionalTestCaseBootstrapUtility` should provide two static public methods:

/**
 * Calculates a "unique" identifier for the given test case class name.
 *
 * @param string $testCaseClassName
 * @return string
 */
public static function getIdentifier($testCaseClassName) {
  return substr(sha1($testCaseClassName), 0, 7);
}
/**
 * Calculates path to TYPO3 CMS test installation the given test case class name.
 *
 * @param string $testCaseClassName
 * @return string
 */
public static function getInstancePath($testCaseClassName) {
  return ORIGINAL_ROOT . 'typo3temp/functional-' . static::getIdentifier($testCaseClassName);
}

If already implemented this so if want I could contribute.


Related issues 1 (0 open1 closed)

Has duplicate TYPO3 Core - Bug #71574: Property "instancePath" not accessible in a TestCaseClosed2015-11-14

Actions
Actions #1

Updated by Marc Bastian Heinrichs almost 9 years ago

  • Status changed from New to Needs Feedback
  • Priority changed from Must have to Could have

Hey, thx for your request, but this is not needed, since PATH_site is already a constant with the path to the test instance in typo3temp. See functional tests with images in sysext impexp. Does this fit your needs, too?

Actions #2

Updated by Artus Kolanowski almost 9 years ago

Other possible variants for the constant name are:

  1. INSTANCE_PATH_<FULL_QUALIFIED_TEST_CASE_NAME>
  2. INSTANCE_ROOT_<FULL_QUALIFIED_TEST_CASE_NAME>
  3. <FULL_QUALIFIED_TEST_CASE_NAME>_INSTANCE_PATH
  4. <FULL_QUALIFIED_TEST_CASE_NAME>_INSTANCE_ROOT
  5. <FULL_QUALIFIED_TEST_CASE_NAME>_ROOT

The only downside is that such a constant will change when the class name of the test case changes and the the full qualified identifier for the test case static::class is a little bit long. But it would you allow to use it directly in the fixture for the instance configuration:

protected $configurationToUseInTestInstance = array(
  'MAIL' => array(
    'transport' => 'mbox',
    'transport_mbox_file' => VENDOR_EXTENSION_TESTS_FUNCTIONAL_TESTCASETEST_INSTANCE_ROOT . '/typo3temp/mail.box'
  )
);

The alternative by using the suggested static members of \TYPO3\CMS\Core\Tests\FunctionalTestCaseBootstrapUtility would look like this:

protected $configurationToUseInTestInstance = array(
  'MAIL' => array(
    'transport' => 'mbox'
  )
);

public function setUp() {
  $this->configurationToUseInTestInstance['MAIL']['transport_mbox_file'] = FunctionalTestCaseBootstrapUtility::getInstancePath(static::class) . '/typo3temp/mail.box';

  parent::setUp();
}

To target the problem with very long constant names we could strip some parts. E.g. the class name is Vendor\Extension\Tests\Functional\Controller\ControllerTest and the constant name would be VENDOR_EXTENSION_TESTS_FUNCTIONAL_CONTROLLER_CONTROLLERTEST_INSTANCE_ROOT we could strip the namespaces Tests and Functional and thus get a constant name like VENDOR_EXTENSION_CONTROLLER_CONTROLLERTEST_INSTANCE_ROOT. Another improvement could be to convert the local class (camel case) name into a underscored version e.g. VENDOR_EXTENSION_CONTROLLER_CONTROLLER_TEST_INSTANCE_ROOT. But this might all be harder to remember when you want to use it.

Actions #3

Updated by Artus Kolanowski almost 9 years ago

Marc Bastian Heinrichs wrote:

Hey, thx for your request, but this is not needed, since PATH_site is already a constant with the path to the test instance in typo3temp. See functional tests with images in sysext impexp. Does this fit your needs, too?

Hey, thx for the quick update! Unfortunately not. Because I need the instance path within $configurationToUseInTestInstance. When I try this with PATH_site I get an:

Use of undefined constant PATH_site [...]

Which makes sense when you take a look into \TYPO3\CMS\Core\Tests\FunctionalTestCase::setUp(). And this is the difference in the scenario here. We need the instance path before setUp() is called to put an absolute path into the configuration of the test instance. Your example uses PATH_site after setUp() in a test.

The changes we have to made to implement this feature are less then 30 Lines and it would be a non breaking change. Take a look into my previous comment. Also the generated constant is not a must have when we still provide at least one public static method for generating such an instance path through FunctionalTestCaseBootstrapUtility.

Actions #4

Updated by Artus Kolanowski almost 9 years ago

I also would say this is a must have. Otherwise you're not able to easily put absolute paths into the instance configuration. And the mail transport mbox is an perfect example for this. Or did I missed another already existing easy solution for this case?

The only easy solution I could imagine would be to use the ORIGINAL_ROOT but this feels wrong because in the described scenario I'm handling with results of the test instance and thus they should be hosted with in it.

Actions #5

Updated by Wouter Wolters almost 9 years ago

How do you run your functional tests? What is the exact command?

Actions #6

Updated by Artus Kolanowski almost 9 years ago

Wouter Wolters wrote:

How do you run your functional tests? What is the exact command?

Here are some excerpts from the test suite for the checkout. Please notice that this uses already an extended API:

/**
 * Test case for checkout with a filled cart
 * 
 * [...]
 */
class CheckoutWithFilledCartTest extends FunctionalTestCase {
  /**
   * @var array
   */
  protected $coreExtensionsToLoad = array(
    'extbase',
    'fluid'
  );

  /**
   * @var array
  */
  protected $testExtensionsToLoad = array(
    'typo3conf/ext/events'
  );

  /**
   * @var array
   */
  protected $configurationToUseInTestInstance = array(
    'MAIL' => array(
      'transport' => 'mbox',
      'transport_mbox_file' => VENDOR_EVENTS_CONTROLLER_CHECKOUT_WITH_FILLED_CART_TEST_INSTANCE_PATH . '/typo3temp/mail.box'
    )
  );

  // [...]

  protected function getMail() {
    return file_get_contents($this->configurationToUseInTestInstance['MAIL']['transport_mbox_file']);
  }

  /**
   * Sets up this test suite
   */
  protected function setUp() {
    parent::setUp();

    $this->importDataSet(ORIGINAL_ROOT . 'typo3/sysext/core/Tests/Functional/Fixtures/pages.xml');
    $this->importDataSet(ORIGINAL_ROOT . 'typo3conf/ext/events/Tests/Functional/Fixtures/events.xml');

    // [...]

    $this->setUpFrontendRootPage(1, array(
      'typo3conf/ext/events/Configuration/TypoScript/setup.txt',
      'typo3conf/ext/events/Tests/Functional/Fixtures/Frontend/JsonRenderer.ts'
    ), array(
      'typo3conf/ext/events/Configuration/TypoScript/constants.txt'
    ));

    // [...]

    $this->frontendResponse = $this->getFrontendResponse(1, 0, 0, 0, true, 0, array(
      'tx_events_cart' => array(
        'action' => 'add',
        'qty' => '2',
        'ticket' => '1'
      )
    ));
  }

  /**
   * Tears down this test suite
   */
  protected function tearDown() {
    unlink($this->configurationToUseInTestInstance['MAIL']['transport_mbox_file']);
  }

  // [...]

  /**
   * @test
   */
  public function supportsTameOrder() {

    // [...]

    // fill out order step
    $response = $this->getFrontendResponse(1, 0, 0, 0, true, 0, array(
      'tx_events_checkout' => array(
        'action' => 'forward',
        'termsAndConditions' => 1
      )
    ), $this->frontendResponse->getCookies());
    $section = $response->getResponseContent()->getSection('Checkout');
    $mail = $this->getMail();

    // [...]

    $this->assertRegExp('/^Subject: Order #1/m', $mail);
    $this->assertRegExp('/^From: noreply@domain.tld/m', $mail);
    $this->assertRegExp('/^To: order@domain.tld/m', $mail);

    // [...]
  }

  // [...]
}

I run this test just with ../vendor/phpunit/phpunit/phpunit -c typo3conf/ext/events/Build/FunctionalTests.xml. The FunctionalTests.xml looks like this:

<phpunit
  backupGlobals="false" 
  backupStaticAttributes="false" 
  bootstrap="../../../../typo3/sysext/core/Build/FunctionalTestsBootstrap.php" 
  colors="true" 
  convertErrorsToExceptions="true" 
  convertWarningsToExceptions="true" 
  forceCoversAnnotation="false" 
  processIsolation="true" 
  stopOnError="false" 
  stopOnFailure="false" 
  stopOnIncomplete="false" 
  stopOnSkipped="false" 
  verbose="true">

  <testsuites>
    <testsuite name="EXT:events">
      <directory>../Tests/Functional/</directory>
    </testsuite>
  </testsuites>
</phpunit>
Actions #7

Updated by Gerrit Code Review almost 9 years ago

  • Status changed from Needs Feedback to Under Review

Patch set 1 for branch master of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at https://review.typo3.org/45482

Actions #8

Updated by Gerrit Code Review almost 9 years ago

Patch set 2 for branch master of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at https://review.typo3.org/45482

Actions #9

Updated by Oliver Hader almost 9 years ago

  • Tracker changed from Feature to Task
  • TYPO3 Version set to 6.2
Actions #10

Updated by Gerrit Code Review almost 9 years ago

Patch set 3 for branch master of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at https://review.typo3.org/45482

Actions #11

Updated by Gerrit Code Review almost 9 years ago

Patch set 1 for branch TYPO3_7-6 of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at https://review.typo3.org/45486

Actions #12

Updated by Gerrit Code Review almost 9 years ago

Patch set 1 for branch TYPO3_6-2 of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at https://review.typo3.org/45487

Actions #13

Updated by Oliver Hader almost 9 years ago

  • Status changed from Under Review to Resolved
  • % Done changed from 0 to 100
Actions #14

Updated by Benni Mack about 6 years ago

  • Status changed from Resolved to Closed
Actions

Also available in: Atom PDF