-
Notifications
You must be signed in to change notification settings - Fork 4
/
DerivedValueTest.php
62 lines (59 loc) · 2.41 KB
/
DerivedValueTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<?php
class DerivedValueTest extends PHPUnit_Framework_TestCase
{
/**
* In this example, $center is computed to avoid a test bug because someone
* updated $array and forgot the $center variable.
* In short, to remove duplication.
*/
public function testADerivedInputRemovesDuplicationAndImprovesClarity()
{
$array = array('a', 'b', 'center', 'd', 'e');
$center = floor(count($array) / 2);
array_flip($array);
$this->assertEquals('center', $array[$center]);
}
/**
* A data structure HTTP request is created and then invalidated.
* Compare this with redefining the array every time and make sure
* the other keys are still valid.
* I use an array in place of an object for brevity in this explanation.
*/
public function testOneBadAttributeValuesAreBuiltFromValidOnes()
{
$request = $this->createGetRequest(); // hides module and controller details
$request['action'] = 'this-will-cause-a-404';
$this->markTestIncomplete('When calling the SUT, the action should be judged as invalid.');
}
private function createGetRequest()
{
return array(
'module' => 'default',
'controller' => 'index',
'action' => 'index'
);
}
/**
* If we have a "casting-out-nines" test to quickly check our results,
* at least as in a smoke test, we can use a derived expectation.
* Since our test cases are usually many simplified scenarios where
* to exercise the production code, deriving the right result shouldn't
* require as much code as in there. If you have to mirror the production
* code, stop: the test would become too tied with the implementation, to
* the point of reproducing its bugs.
* These tests come handy when the amount of data is huge (e.g. multimedia
* files) and generating a sanity check is by far faster than hardcoding
* everything by hand (the image contains a car at pixel (45; 100) ...).
*/
public function testADerivedExpectationLetsYouAssertWithoutHardcoding()
{
$array = array();
$elements = 4; // our unique parameter
for ($i = 1; $i <= $elements; $i++) {
$array[] = $i;
}
$expectedTotal = $elements * ($elements + 1) / 2; // Gauss formula
$total = array_sum($array);
$this->assertEquals($expectedTotal, $total);
}
}