Page 13 of 15
Re: TDD workshop. Anybody welcome; Data storage abstraction.
Posted: Tue Apr 29, 2008 1:48 am
by matthijs
Ok, I've got
Code: Select all
class ArrayStorageModelTest extends UnitTestCase {
function setUp() {
unlink(TEST_BASE . '/tmp/test.php');
}
function tearDown() {
}
public function testSomething(){
$this->_createtestfile('nothing');
}
private function _createtestFile($data) {
file_put_contents(TEST_BASE . '/tmp/test.php',$data);
}
}
Should we start with:
- have the test setup write an array to the file
- test if instantiating the class reads the file (or test load() directly?
Re: TDD workshop. Anybody welcome; Data storage abstraction.
Posted: Tue Apr 29, 2008 7:10 am
by Chris Corbyn
Sorry for the delay. I have a deadline to finish writing an article so I've been busy with that. Just taking 10 minutes out to chip into this thread again for a moment
First things first, you've got the right idea with the tearDown() method removing a previously created file! However, I think we'll avoid creating a file with a fixed name each time. I had envisaged that if the file does not exist and save() is called the ArrayStorageModel will try to create the file before saving the data. As a result, I think this is more flexible:
Code: Select all
class ArrayStorageModelTest extends UnitTestCase {
public function setUp() {
foreach (glob(TEST_TMP . '/*.*') as $file) {
unlink($file);
}
}
public function testSomething(){
$this->_createTestfile('test.php', '<?php $x = array("foo" => "bar"); ?>');
$model = $this->_createStorageModel('test.php', 'x');
//Make some assertions here
}
private function _createStorageModel($filename, $arrayName) {
return new ArrayStorageModel(TEST_TMP . '/' . $filename, $arrayName);
}
private function _createTestFile($filename, $contents) {
file_put_contents(TEST_TMP . '/' . $filename, $contents);
}
}
NOTE that I also defined a new constant in configure.php for re-use later.
The alternative to doing this is to use the tmp directory only for writing data and to provide some real files for loading. I prefer the approach we're going with however because it's a lot easier to read the tests.
Ok, here's our first test?
Code: Select all
<?php
require_once dirname(__FILE__) . '/../configure.php';
require_once CLASS_BASE . '/ArrayStorageModel.php';
class ArrayStorageModelTest extends UnitTestCase {
public function setUp() {
foreach (glob(TEST_TMP . '/*.*') as $file) {
unlink($file);
}
}
public function testSimpleArrayCanBeLoadedFromFile() {
$this->_createTestFile('test.php', '<?php $data = array("foo" => "bar"); ?>');
$this->assertEqual(
array("foo" => "bar"), $this->_createStorageModel('test.php', 'data')->load(),
'%s: Array loaded from StorageModel should match the array in the file'
);
}
// -- Helpers
private function _createStorageModel($filename, $arrayName) {
return new ArrayStorageModel(TEST_TMP . '/' . $filename, $arrayName);
}
private function _createTestFile($filename, $contents) {
file_put_contents(TEST_TMP . '/' . $filename, $contents);
}
}
Files attached.
Go ahead an provide an implementation
This test is going to be incredibly short

Re: TDD workshop. Anybody welcome; Data storage abstraction.
Posted: Tue Apr 29, 2008 8:54 am
by matthijs
Great, I'm going to check it out. First I've got to do some other stuff as well. I vaguely remember something about clients, making money and a mortgage

Re: TDD workshop. Anybody welcome; Data storage abstraction.
Posted: Tue Apr 29, 2008 10:50 am
by Scrumpy.Gums
Hi, I hope it's OK to join in at this point
The following makes the test SimpleArrayCanBeLoadedFromFiletest pass, although, there may be a nicer way than to include the file.
Code: Select all
<?php
require_once dirname( __FILE__ ) . '/StorageModel.php';
class ArrayStorageModel implements StorageModel {
private $_filename;
private $_arrayName;
public function __construct( $filename, $arrayName ) {
$this->_filename = $filename;
$this->_arrayName = $arrayName;
}
public function load() {
include($this->_filename);
return ${$this->_arrayName};
}
public function save($data) {
}
}
I guess you could write a test for saving the array back to file next?
Re: TDD workshop. Anybody welcome; Data storage abstraction.
Posted: Tue Apr 29, 2008 11:21 am
by matthijs
Or a test which tests that when you try to load a non existing file or non existing array it returns false. And maybe a test which asserts that when you load a faulty array it also returns false.
Re: TDD workshop. Anybody welcome; Data storage abstraction.
Posted: Tue Apr 29, 2008 3:35 pm
by sike
hey guys,
in some weak moment i thought it would be a good idea to setup a subversion server to make scanning the changes between iterations a breeze. So i went over to google code and created a devnetwork-playground project and upped the last version from chris. I will add each of you to the project as long as you are registered at google code.
I hope that my rush is ok for everyone - if not i will delete the whole thing again in a rush
http://code.google.com/p/devnetwork-pla ... e/checkout
cheers
Chris
Re: TDD workshop. Anybody welcome; Data storage abstraction.
Posted: Tue Apr 29, 2008 4:09 pm
by matthijs
That's ok with me chris. Even though it was working quite well so far.
I think the other Chris is sleeping right now so we'll have to wait till he gets up to ask what he wants.
I'll have more time for this thread again next days.
Re: TDD workshop. Anybody welcome; Data storage abstraction.
Posted: Tue Apr 29, 2008 7:22 pm
by Chris Corbyn
Hey Chris, could you add me to the SVN repo access list please?
c.a.corbyn
PS: Don't add the closing ?> tag, and place braces on the same line. We're all using the same conventions to keep the code tidy. Ideally we want something we can release at the end

Re: TDD workshop. Anybody welcome; Data storage abstraction.
Posted: Wed Apr 30, 2008 3:30 am
by sike
Chris Corbyn wrote:Hey Chris, could you add me to the SVN repo access list please?
c.a.corbyn
PS: Don't add the closing ?> tag, and place braces on the same line. We're all using the same conventions to keep the code tidy. Ideally we want something we can release at the end

done
Re: TDD workshop. Anybody welcome; Data storage abstraction.
Posted: Wed Apr 30, 2008 4:15 am
by matthijs
Another thing: can you remove the deeply nested structure? Would make things a lot clearer.
Just /trunk/classes/
and /trunk/tests/ would be best I think
Re: TDD workshop. Anybody welcome; Data storage abstraction.
Posted: Wed Apr 30, 2008 4:15 am
by Scrumpy.Gums
Any chance I could be added too?

My username is alexduller.
P.S I've altered the layout conventions in the code I posted

Re: TDD workshop. Anybody welcome; Data storage abstraction.
Posted: Wed Apr 30, 2008 4:44 am
by arjan.top
please add me too, my username is arjan.top (how strange

)
Re: TDD workshop. Anybody welcome; Data storage abstraction.
Posted: Wed Apr 30, 2008 4:48 am
by sike
Scrumpy.Gums wrote:Any chance I could be added too?

My username is alexduller.
P.S I've altered the layout conventions in the code I posted

done
Re: TDD workshop. Anybody welcome; Data storage abstraction.
Posted: Wed Apr 30, 2008 4:49 am
by sike
arjan.top wrote:please add me too, my username is arjan.top (how strange

)
done
Re: TDD workshop. Anybody welcome; Data storage abstraction.
Posted: Wed Apr 30, 2008 4:51 am
by sike
matthijs wrote:Another thing: can you remove the deeply nested structure? Would make things a lot clearer.
Just /trunk/classes/
and /trunk/tests/ would be best I think
i nested it that way to allow other small projects to live on the same svn.
just checkout
https://devnetwork-playground.googlecod ... shop/trunk and you have what you want (at least i hope so (; )
cheers
Chris