From f21427d7fa309dcc6229cca79c9f73f60564a7b3 Mon Sep 17 00:00:00 2001 From: Daniel Hensby Date: Sat, 7 Mar 2015 10:32:17 +0000 Subject: [PATCH] DOCS Explaining test suite nesting --- dev/SapphireTest.php | 2 +- .../06_Testing/00_Unit_Testing.md | 38 ++++++++++++++----- 2 files changed, 29 insertions(+), 11 deletions(-) diff --git a/dev/SapphireTest.php b/dev/SapphireTest.php index c0de44d29..aacb86c18 100644 --- a/dev/SapphireTest.php +++ b/dev/SapphireTest.php @@ -347,7 +347,7 @@ class SapphireTest extends PHPUnit_Framework_TestCase { Injector::unnest(); Config::unnest(); - if($this->extensionsToReapply || $this->extensionsToRemove || $this->extraDataObjects) { + if(!empty($this->extensionsToReapply) || !empty($this->extensionsToRemove) || !empty($this->extraDataObjects)) { $this->resetDBSchema(); } } diff --git a/docs/en/02_Developer_Guides/06_Testing/00_Unit_Testing.md b/docs/en/02_Developer_Guides/06_Testing/00_Unit_Testing.md index 1771e640b..7afd1894c 100644 --- a/docs/en/02_Developer_Guides/06_Testing/00_Unit_Testing.md +++ b/docs/en/02_Developer_Guides/06_Testing/00_Unit_Testing.md @@ -182,18 +182,10 @@ end of each test. $page->publish('Stage', 'Live'); } - // reset configuration for the test. - Config::nest(); + // set custom configuration for the test. Config::inst()->update('Foo', 'bar', 'Hello!'); } - public function tearDown() { - // restores the config variables - Config::unnest(); - - parent::tearDown(); - } - public function testMyMethod() { // .. } @@ -223,6 +215,32 @@ individual test case. // .. } } + +### Config and Injector Nesting + +A powerful feature of both [`Config`](/developer_guides/configuration/configuration/) and [`Injector`](/developer_guides/extending/injector/) is the ability to "nest" them so that you can make changes that can easily be discarded without having to manage previous values. + +The testing suite makes use of this to "sandbox" each of the unit tests as well as each suite to prevent leakage between tests. + +If you need to make changes to `Config` (or `Injector) for each test (or the whole suite) you can safely update `Config` (or `Injector`) settings in the `setUp` or `tearDown` functions. + +It's important to remember that the `parent::setUp();` functions will need to be called first to ensure the nesting feature works as expected. + + :::php + function setUpOnce() { + parent::setUpOnce(); + //this will remain for the whole suite and be removed for any other tests + Config::inst()->update('ClassName', 'var_name', 'var_value'); + } + + function testFeatureDoesAsExpected() { + //this will be reset to 'var_value' at the end of this test function + Config::inst()->update('ClassName', 'var_name', 'new_var_value'); + } + + function testAnotherFeatureDoesAsExpected() { + Config::inst()->get('ClassName', 'var_name'); // this will be 'var_value' + } ## Generating a Coverage Report @@ -266,4 +284,4 @@ some `thirdparty/` directories add the following to the `phpunit.xml` configurat * [api:TestRunner] * [api:SapphireTest] -* [api:FunctionalTest] \ No newline at end of file +* [api:FunctionalTest]