mirror of
https://github.com/silverstripe/silverstripe-multiform
synced 2024-10-22 11:05:49 +02:00
Added basics for testing, and some test classes for future test operations
This commit is contained in:
parent
54164a129f
commit
e4f51e07c2
@ -2,17 +2,24 @@
|
||||
|
||||
class MultiFormSessionTest extends SapphireTest {
|
||||
|
||||
/**
|
||||
* Set up the instance of MultiFormSession, writing
|
||||
* a record to the database for this test. We persist
|
||||
* the object in our tests by assigning $this->session
|
||||
*/
|
||||
function setUp() {
|
||||
$this->session = new MultiFormSession();
|
||||
$this->session->write();
|
||||
}
|
||||
|
||||
/**
|
||||
* Test generation of a new session.
|
||||
*
|
||||
* @TODO Write some more advanced tests for MultiFormSession.
|
||||
*/
|
||||
function testSessionGeneration() {
|
||||
$session = new MultiFormSession();
|
||||
$session->write();
|
||||
|
||||
$this->assertTrue($session->ID != 0);
|
||||
$this->assertTrue($session->ID > 0);
|
||||
|
||||
$session->delete();
|
||||
$this->assertTrue($this->session->ID != 0);
|
||||
$this->assertTrue($this->session->ID > 0);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -28,6 +35,13 @@ class MultiFormSessionTest extends SapphireTest {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete the MultiFormSession record that we created.
|
||||
*/
|
||||
function tearDown() {
|
||||
$this->session->delete();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
64
tests/MultiFormTest.php
Normal file
64
tests/MultiFormTest.php
Normal file
@ -0,0 +1,64 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* MultiFormTest
|
||||
*
|
||||
* @TODO create some behavioural test cases, such as examining what occurs after
|
||||
* submitting the "previous", "next" form actions that a user would normally
|
||||
* be doing.
|
||||
*
|
||||
* For testing purposes, we have some test MultiForm classes:
|
||||
*
|
||||
* - MultiFormTestClass (subclass of MultiForm)
|
||||
* - MultiFormTestStepOne (subclass of MultiFormStep - the first step)
|
||||
* - MultiFormTestStepTwo (subclass of MultiFormStep - the second step)
|
||||
* - MultiFormTestStepThree (subclass of MultiFormStep - the third step)
|
||||
*
|
||||
* These test classes should be used for testing the operation of a "real"
|
||||
* instance of this multiform step system. Also, as a note here: every instance
|
||||
* of MultiFormStep, which is every step in a form, requires a db/build, as it
|
||||
* is a subclass of DataObject. This is a bit of a pain, but it's required for
|
||||
* the database to store the step data for each step, which is very important!
|
||||
*
|
||||
* So, if you're going to create some new tests, and want to use some test classes,
|
||||
* make sure to use the ones mentioned above.
|
||||
*/
|
||||
class MultiFormTest extends SapphireTest {
|
||||
|
||||
/**
|
||||
* Set up the instance of MultiForm, writing a record
|
||||
* to the database for this test. We persist the object
|
||||
* in our tests by assigning $this->session
|
||||
*/
|
||||
function setUp() {
|
||||
$this->form = new MultiFormTestClass(new Controller(), 'Form', new FieldSet(), new FieldSet());
|
||||
$this->form->init();
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests initialising a new instance of a test class.
|
||||
*
|
||||
* @TODO Write some decent tests! The current assertions are very basic, and are
|
||||
* nowhere near touching on the more advanced concepts of MultiForm, such
|
||||
* as the form actions (prev/next), session handling, and step handling
|
||||
* through {@link MultiFormStep->getPreviousStep()} and
|
||||
* {@link MultiFormStep->getNextStep()} for example.
|
||||
*/
|
||||
function testInitialisingForm() {
|
||||
$this->assertTrue(is_numeric($this->form->getCurrentStep()->ID) && ($this->form->getCurrentStep()->ID > 0));
|
||||
$this->assertTrue(is_numeric($this->form->session->ID) && ($this->form->session->ID > 0));
|
||||
$this->assertTrue($this->form->getStartStep() == 'MultiFormTestStepOne');
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the session data that was created. Note: This should delete all the
|
||||
* dependencies such as MultiFormStep instances that are related directly to
|
||||
* this session. These directives can be found on {@link MultiFormSession->onBeforeWrite()}
|
||||
*/
|
||||
function tearDown() {
|
||||
$this->form->session->delete();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
17
tests/code/MultiFormTestClass.php
Normal file
17
tests/code/MultiFormTestClass.php
Normal file
@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
class MultiFormTestClass extends MultiForm {
|
||||
|
||||
protected static $start_step = 'MultiFormTestStepOne';
|
||||
|
||||
/**
|
||||
* Accessor method to $start_step
|
||||
* @return string
|
||||
*/
|
||||
function getStartStep() {
|
||||
return $this->stat('start_step');
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
17
tests/code/MultiFormTestStepOne.php
Normal file
17
tests/code/MultiFormTestStepOne.php
Normal file
@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
class MultiFormTestStepOne extends MultiFormStep {
|
||||
|
||||
protected static $next_steps = 'MultiFormTestStepTwo';
|
||||
|
||||
function getFields() {
|
||||
return new FieldSet(
|
||||
new TextField('FirstName', 'First name'),
|
||||
new TextField('Surname', 'Surname'),
|
||||
new EmailField('Email', 'Email address')
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
15
tests/code/MultiFormTestStepThree.php
Normal file
15
tests/code/MultiFormTestStepThree.php
Normal file
@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
class MultiFormTestStepThree extends MultiFormStep {
|
||||
|
||||
protected static $is_final_step = true;
|
||||
|
||||
function getFields() {
|
||||
return new FieldSet(
|
||||
new TextField('Test', 'Anything else you\'d like to tell us?')
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
15
tests/code/MultiFormTestStepTwo.php
Normal file
15
tests/code/MultiFormTestStepTwo.php
Normal file
@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
class MultiFormTestStepTwo extends MultiFormStep {
|
||||
|
||||
protected static $next_steps = 'MultiFormTestStepThree';
|
||||
|
||||
function getFields() {
|
||||
return new FieldSet(
|
||||
new TextareaField('Comments', 'Tell us a bit about yourself...')
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
Loading…
Reference in New Issue
Block a user