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

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 »

arjan.top wrote:chris I think your code is not ok

For example if you have this array:

Code: Select all

 
array(
      'foo' => array(
        'bar' => array(
          'zip' => 'some value'
          )
        ),
        'bar2' => array(
          'zip2' => 'some other value'
        )
      )
 
... and you want foo/bar2/zip2
There's no foo/bar2/zip2 in your code :? There's "foo/bar/zip" and "bar2/zip2", but they are on separate branches. For foo/bar2/zip2 to exist $array['foo']['bar2']['zip2'] needs to exist.
matthijs
DevNet Master
Posts: 3360
Joined: Thu Oct 06, 2005 3:57 pm

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

Post by matthijs »

hey, but in your test you ask for

Code: Select all

$dataStore->get('bar2/zip2')
, while in the other test it is

Code: Select all

$dataStore->get('foo/bar/zip')
If you try

Code: Select all

$dataStore->get('foo/bar2/zip2')
you get a failing test.

[edit]To prevent confusion, I mean;

Code: Select all

   public function testGettingValueOnSecondPrimaryBranch() {
        $storageModel = $this->_createStorageModel();
        $storageModel->setReturnValue('load', array(
            'foo' => array(
                'bar' => array(
                    'zip' => 'some value'
                    )
                ),
            'bar2' => array(
                'zip2' => 'some other value'
                )
            ));
        $dataStore = $this->_createDataStore($storageModel);
        $this->assertEqual('some other value', $dataStore->get('foo/bar2/zip2'));
  }
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 »

Of course I do. It should fail. Where is foo/bar2/zip2 in that array? :? ;)

The only paths in that array are foo/bar/zip AND bar2/zip2.
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 »

Code: Select all

 
$this->assertEqual('some other value', $dataStore->get('bar2/zip2'));
 
foo is missing here


EDIT:
oops new page already, missed some posts ...
Last edited by arjan.top on Sun Apr 27, 2008 4:56 am, edited 1 time in total.
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 »

Just look at it in it's array form. To get foo/bar2/zip2 we'd need:

Code: Select all

array(
  'foo' => array(
    'bar2' => array(
      'zip2' => 'something'
      )
    ),
    'bar' => array(
      'zip' => 'some value'
      )
    )
  ),
  'bar2' => array(
    'zip2' => 'some other value'
    )
  );
:) The test is incorrect ;)

EDIT | Please see my first post on Page 9 of this discussion ;)
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 »

Yes I see, I made I mistake :oops:
matthijs
DevNet Master
Posts: 3360
Joined: Thu Oct 06, 2005 3:57 pm

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

Post by matthijs »

Arggh, I saw that too just now :oops:
Somehow it's very difficult to count and match brackets...
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 »

arjan.top wrote:Yes I see, I made I mistake :oops:
That's fine. We need to keep thinking of things which aren't right and then adding a test to prove it. Just happens in this case that the thing you though wasn't right was a result of a little confusion :) 3 heads are better than 1.
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 »

matthijs wrote:Arggh, I saw that too just now :oops:
Somehow it's very difficult to count and match brackets...
Exactly the reason I always write multidimensional array as indented nests :) The trick is to look up and down vertically.

Look at the veritcal alignment of 'foo' in the array, then look directly down from it; you hit 'bar2', so that mean it's in the same dimension :)
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 »

matthijs wrote:Arggh, I saw that too just now :oops:
Somehow it's very difficult to count and match brackets...
I just looked at how brackets are aligned and I added array in the wrong place :banghead:
matthijs
DevNet Master
Posts: 3360
Joined: Thu Oct 06, 2005 3:57 pm

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

Post by matthijs »

So, if everybody's got his or her brackets aligned again, what's next? :)
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 »

Ok, all together now!

:banghead: :banghead: :banghead: :banghead: :banghead:

:lol: :lol:

Ok, time to continue.... ;)
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 »

Right. Sit down and stop talking class! :twisted:

How's about we make sure that the array we're passing to save() is what we want to pass? So far we haven't validated what is passed to it. This is a new one for you probably matthijs since it's an extra parameter on $storageModel->expectOnce('save', ...).

Just two tests will do. One for a single value in a one dimensional array, then one for our more complex multidimensional array.
matthijs
DevNet Master
Posts: 3360
Joined: Thu Oct 06, 2005 3:57 pm

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

Post by matthijs »

Maybe you have to explain a bit more about what you expect save to do. I don't entirely understand it. Isn't the storageModel supposed to validate the data?

It's probably simple, but maybe my idea of how the classes work doesn't match up with yours.
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 »

matthijs wrote:Maybe you have to explain a bit more about what you expect save to do. I don't entirely understand it. Isn't the storageModel supposed to validate the data?

It's probably simple, but maybe my idea of how the classes work doesn't match up with yours.
Definitely worth further explanation. We all need to be clear what we're doing. In my opinion, save() should accept an array in exactly the same format as the array provided by load(). When that array is modified (by set() or remove()) the array passed to save() should reflect that modification.

We don't care what happens to the internal array ($this->_store) when set() or remove() are called. We don't even care that such an internal array exists. What we do care about is that save() receives integral data.

I can write a really simple first test if you like? Really simple!

Code: Select all

 public function testSavingSingleValuePassesSimpleArray() {
    $storageModel = $this->_createStorageModel();
    $storageModel->expectOnce('save', array( array('zip' => 'button') ));
    $dataStore = $this->_createDataStore($storageModel);
    $dataStore->set('zip', 'button');
    $dataStore->save();
  }
To explain that second parameter on expectOnce(). It takes an array of expected parameters. So in this case case expect one parameter which is an array itself (hence the doubly nested array which looks confusing).
Post Reply