patternphpMinor
Test case for a caching library
Viewed 0 times
casecachingfortestlibrary
Problem
This shows a test case for an old caching library that I use for a project. It features simple save/load/delete functions (sadly static calls) but what I want to focus on is the test code written for this class.
In my opinion the unit tests for a class should show how all functions in the class work and what to expect from that class.
Recently we got this great change to show off code, so I'd like to ask you if you can read that test code, understand what the class might do and how you would improve upon it.
```
assertSame(false, DatenCache::load("testNotHereCache"));
}
function testSaveLoadSimple() {
$sDatenSimple = "testStringSaveLoadSimple";
$sDatenSimpleParam = "testStringParam";
$sDatenSimpleParam2 = "testStringParam2";
$sDatenSimpleParam3 = "";
$sDatenSimpleParam4 = 0;
$this->assertSame(true, DatenCache::save("testCacheSimple", false, $sDatenSimple));
$this->assertSame($sDatenSimple, DatenCache::load("testCacheSimple", false));
$this->assertSame(true, DatenCache::save("testCacheLinearParam", "string", $sDatenSimpleParam));
$this->assertSame($sDatenSimpleParam, DatenCache::load("testCacheLinearParam", "string"));
$this->assertSame(true, DatenCache::save("testCacheLinearParam", 5, $sDatenSimpleParam2));
$this->assertSame($sDatenSimpleParam2, DatenCache::load("testCacheLinearParam", 5));
$this->assertSame(true, DatenCache::save("testCacheBoundStringParam", false, $sDatenSimpleParam3));
$this->assertSame($sDatenSimpleParam3, DatenCache::load("testCacheBoundStringParam", false));
$this->assertSame(true, DatenCache::save("testCacheBoundIntParam", false, $sDatenSimpleParam4));
$this->assertSame($sDatenSimpleParam4, DatenCache::load("testCacheBoundIntParam", false));
$oObj = new stdClass();
$oObj->bob = 2;
$this->assertSame(true, DatenCache::save("testCacheBoundObjParam", $oObj, "zwei"));
$this->as
In my opinion the unit tests for a class should show how all functions in the class work and what to expect from that class.
Recently we got this great change to show off code, so I'd like to ask you if you can read that test code, understand what the class might do and how you would improve upon it.
```
assertSame(false, DatenCache::load("testNotHereCache"));
}
function testSaveLoadSimple() {
$sDatenSimple = "testStringSaveLoadSimple";
$sDatenSimpleParam = "testStringParam";
$sDatenSimpleParam2 = "testStringParam2";
$sDatenSimpleParam3 = "";
$sDatenSimpleParam4 = 0;
$this->assertSame(true, DatenCache::save("testCacheSimple", false, $sDatenSimple));
$this->assertSame($sDatenSimple, DatenCache::load("testCacheSimple", false));
$this->assertSame(true, DatenCache::save("testCacheLinearParam", "string", $sDatenSimpleParam));
$this->assertSame($sDatenSimpleParam, DatenCache::load("testCacheLinearParam", "string"));
$this->assertSame(true, DatenCache::save("testCacheLinearParam", 5, $sDatenSimpleParam2));
$this->assertSame($sDatenSimpleParam2, DatenCache::load("testCacheLinearParam", 5));
$this->assertSame(true, DatenCache::save("testCacheBoundStringParam", false, $sDatenSimpleParam3));
$this->assertSame($sDatenSimpleParam3, DatenCache::load("testCacheBoundStringParam", false));
$this->assertSame(true, DatenCache::save("testCacheBoundIntParam", false, $sDatenSimpleParam4));
$this->assertSame($sDatenSimpleParam4, DatenCache::load("testCacheBoundIntParam", false));
$oObj = new stdClass();
$oObj->bob = 2;
$this->assertSame(true, DatenCache::save("testCacheBoundObjParam", $oObj, "zwei"));
$this->as
Solution
Other than long lines, I'm not seeing something too unreadable. I don't advise shortening function names as doing so makes it less clear what the function is doing. Instead, try splitting the function calls being passed as parameters onto different lines by either splitting the line across multiple lines or storing the returned value as a variable and passing that variable in.
Context
StackExchange Code Review Q#47, answer score: 7
Revisions (0)
No revisions yet.