TDD workshop. Anybody welcome; Data storage abstraction.

Discussion of testing theory and practice, including methodologies (such as TDD, BDD, DDD, Agile, XP) and software - anything to do with testing goes here. (Formerly "The Testing Side of Development")

Moderator: General Moderators

matthijs
DevNet Master
Posts: 3360
Joined: Thu Oct 06, 2005 3:57 pm

Re: TDD workshop. Anybody welcome; Data storage abstraction.

Post 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?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: TDD workshop. Anybody welcome; Data storage abstraction.

Post 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 :P
Attachments
data_store.zip
(139.01 KiB) Downloaded 285 times
matthijs
DevNet Master
Posts: 3360
Joined: Thu Oct 06, 2005 3:57 pm

Re: TDD workshop. Anybody welcome; Data storage abstraction.

Post 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 ;)
Scrumpy.Gums
Forum Commoner
Posts: 71
Joined: Thu Aug 30, 2007 2:57 pm
Location: Bristol, UK

Re: TDD workshop. Anybody welcome; Data storage abstraction.

Post 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?
Last edited by Scrumpy.Gums on Wed Apr 30, 2008 4:14 am, edited 1 time in total.
matthijs
DevNet Master
Posts: 3360
Joined: Thu Oct 06, 2005 3:57 pm

Re: TDD workshop. Anybody welcome; Data storage abstraction.

Post 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.
sike
Forum Commoner
Posts: 84
Joined: Wed Aug 02, 2006 8:33 am

Re: TDD workshop. Anybody welcome; Data storage abstraction.

Post 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
matthijs
DevNet Master
Posts: 3360
Joined: Thu Oct 06, 2005 3:57 pm

Re: TDD workshop. Anybody welcome; Data storage abstraction.

Post 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.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: TDD workshop. Anybody welcome; Data storage abstraction.

Post 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 ;)
sike
Forum Commoner
Posts: 84
Joined: Wed Aug 02, 2006 8:33 am

Re: TDD workshop. Anybody welcome; Data storage abstraction.

Post 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
matthijs
DevNet Master
Posts: 3360
Joined: Thu Oct 06, 2005 3:57 pm

Re: TDD workshop. Anybody welcome; Data storage abstraction.

Post 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
Scrumpy.Gums
Forum Commoner
Posts: 71
Joined: Thu Aug 30, 2007 2:57 pm
Location: Bristol, UK

Re: TDD workshop. Anybody welcome; Data storage abstraction.

Post 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 :P
User avatar
arjan.top
Forum Contributor
Posts: 305
Joined: Sun Oct 14, 2007 4:36 am
Location: Hoče, Slovenia

Re: TDD workshop. Anybody welcome; Data storage abstraction.

Post by arjan.top »

please add me too, my username is arjan.top (how strange :D )
sike
Forum Commoner
Posts: 84
Joined: Wed Aug 02, 2006 8:33 am

Re: TDD workshop. Anybody welcome; Data storage abstraction.

Post 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 :P
done
sike
Forum Commoner
Posts: 84
Joined: Wed Aug 02, 2006 8:33 am

Re: TDD workshop. Anybody welcome; Data storage abstraction.

Post by sike »

arjan.top wrote:please add me too, my username is arjan.top (how strange :D )
done
sike
Forum Commoner
Posts: 84
Joined: Wed Aug 02, 2006 8:33 am

Re: TDD workshop. Anybody welcome; Data storage abstraction.

Post 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
Post Reply