chriskite / phactory

A Database Factory for PHP Unit Tests
http://phactory.org
MIT License
140 stars 39 forks source link

Ability to set a mongo record value to something other than a string #24

Closed drock closed 12 years ago

drock commented 12 years ago

The _evalSequence function of the Blueprint class forces all values of documents to be a string. This pull request adds a check to that so that if a value is not a string it won't be eval'd.

This is needed because as Phactory stands now, there is no way to create a document with a value being an array of scalars such as

$phactory->define('user',array('tags'=>array('one','two','three')));

In the above case an embedded document does not work as you end up with an array of nested arrays otherwise.

ramsey commented 12 years ago

I was playing around with this branch locally, since I need the same functionality, and I found that $n within values of the arrays does not evaluate, as I would expect.

For example, with the following code:

$phactory->define('user', array(
    '_id' => '$n',
    'name' => 'John Doe $n',
    'tags' => array(
        'one$n',
        'two$n',
        'three$n'
    )
));

I expect a MongoDB data structure that looks something like this:

{
    "_id" : 0,
    "name" : "John Doe 0",
    "tags" : [
        "one0",
        "two0",
        "three0"
    ]
}

But, instead, I get a data structure that looks like this:

{
    "_id" : 0,
    "name" : "John Doe 0",
    "tags" : [
        "one$n",
        "two$n",
        "three$n"
    ]
}
drock commented 12 years ago

Good catch, the eval that interpreted the $n was not applied recursively originally and now needs to be since we can have arrays as data values.

ramsey commented 12 years ago

Looks good. Thanks!

drock commented 12 years ago

Any chance of this getting merged in?

ramsey commented 12 years ago

:+1: from me on this!

jimbojsb commented 12 years ago

Merged, tagged, etc.

On Tue, Nov 6, 2012 at 12:13 PM, Ben Ramsey notifications@github.comwrote:

[image: :+1:] from me on this!

— Reply to this email directly or view it on GitHubhttps://github.com/chriskite/phactory/pull/24#issuecomment-10121061.