Skip to content

Commit

Permalink
Bug with passing behaviors, configured in mapping, to documents
Browse files Browse the repository at this point in the history
  • Loading branch information
sokil committed Jan 6, 2015
1 parent 78ca4e9 commit a128659
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 18 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 1.11.7 (2014-01-06)
* Bug with passing behaviors, configured in mapping, to documents

## 1.11.6 (2014-01-06)
* Refactor document validation exceptions. Exception `\Sokil\Mongo\Document\Exception\Validate` deprecated. Use `\Sokil\Mongo\Document\InvalidDocumentException`.
* Allow to get matched params on regex mapping.
Expand Down
11 changes: 6 additions & 5 deletions src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -285,9 +285,9 @@ public function createDocument(array $data = null)

/* @var $document \Sokil\Mongo\Document */
$document = new $className($this, $data, array(
'stored' => false,
'versioning' => $this->isVersioningEnabled(),
'behaviors' => $this->getOption('behaviors'),
'stored' => false,
'versioning' => $this->isVersioningEnabled(),
'behaviors' => $this->getOption('behaviors'),
));

// store document to identity map
Expand Down Expand Up @@ -323,8 +323,9 @@ public function getStoredDocumentInstanceFromArray(array $data, $useDocumentPool
// init document instance
$className = $this->getDocumentClassName($data);
$document = new $className($this, $data, array(
'stored' => true,
'versioning' => $this->isVersioningEnabled(),
'stored' => true,
'versioning' => $this->isVersioningEnabled(),
'behaviors' => $this->getOption('behaviors'),
));

// store document in cache
Expand Down
56 changes: 43 additions & 13 deletions tests/DocumentBehaviorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,6 @@

namespace Sokil\Mongo;

class SomeBehavior extends \Sokil\Mongo\Behavior
{
public function return42()
{
return 42;
}

public function getOwnerParam($name)
{
return $this->getOwner()->get($name);
}
}

class DocumentBehaviorTest extends \PHPUnit_Framework_TestCase
{
/**
Expand Down Expand Up @@ -117,4 +104,47 @@ public function testBehaviorInMapping()

$this->assertEquals('42', $document->return42());
}

public function testBehaviorInCursor()
{
$db = $this->collection->getDatabase();
$db->map('col', array(
'behaviors' => array(
'get42' => new SomeBehavior(),
),
));

$collection = $db->getCollection('col');

$collection->insertMultiple(
array(
array('key' => 'value1'),
array('key' => 'value2'),
array('key' => 'value3'),
array('key' => 'value4'),
array('key' => 'value5'),
),
false
);

foreach($collection->find() as $document) {
$this->assertEquals(42, $document->return42());
}
}
}

/**
* Behavior
*/
class SomeBehavior extends \Sokil\Mongo\Behavior
{
public function return42()
{
return 42;
}

public function getOwnerParam($name)
{
return $this->getOwner()->get($name);
}
}

0 comments on commit a128659

Please sign in to comment.