Skip to content

Commit

Permalink
Updated ActiveRecords get methods to be callable as static methods
Browse files Browse the repository at this point in the history
I know this is wrong but it's what makes it magical
  • Loading branch information
Zizaco committed May 18, 2015
1 parent 1a3c332 commit d133be2
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 14 deletions.
13 changes: 8 additions & 5 deletions src/Mongolid/ActiveRecord.php
Expand Up @@ -9,7 +9,7 @@
* have methods to interact with the database. It means that 'save', 'insert',
* 'update', 'where', 'first' and 'all' are available within every instance.
* The Mongolid\Schema that describes the entity will be generated on the go
* based on the properties bellow.
* based on the $fields.
*
* @package Mongolid
*/
Expand Down Expand Up @@ -79,7 +79,8 @@ public function update()
*/
public function where($query)
{
return $this->getDataMapper()->where($query);
return Ioc::make(get_called_class())
->getDataMapper()->where($query);
}

/**
Expand All @@ -89,7 +90,8 @@ public function where($query)
*/
public function all()
{
return $this->getDataMapper()->all();
return Ioc::make(get_called_class())
->getDataMapper()->all();
}

/**
Expand All @@ -99,7 +101,8 @@ public function all()
*/
public function first($query)
{
return $this->getDataMapper()->first($query);
return Ioc::make(get_called_class())
->getDataMapper()->first($query);
}

/**
Expand Down Expand Up @@ -127,7 +130,7 @@ protected function getSchema()
*
* @return DataMapper
*/
protected function getDataMapper()
public function getDataMapper()
{
$dataMapper = new DataMapper;
$dataMapper->collection = $this->collection;
Expand Down
12 changes: 3 additions & 9 deletions tests/Mongolid/ActiveRecordTest.php
Expand Up @@ -31,8 +31,6 @@ public function testShouldSave()
$dataMapper = m::mock();

// Act
$entity->shouldAllowMockingProtectedMethods();

$entity->shouldReceive('getDataMapper')
->andReturn($dataMapper);

Expand All @@ -52,8 +50,6 @@ public function testShouldInsert()
$dataMapper = m::mock();

// Act
$entity->shouldAllowMockingProtectedMethods();

$entity->shouldReceive('getDataMapper')
->andReturn($dataMapper);

Expand All @@ -73,8 +69,6 @@ public function testShouldUpdate()
$dataMapper = m::mock();

// Act
$entity->shouldAllowMockingProtectedMethods();

$entity->shouldReceive('getDataMapper')
->andReturn($dataMapper);

Expand All @@ -96,7 +90,7 @@ public function testShouldGetWithWhereQuery()
$cursor = m::mock();

// Act
$entity->shouldAllowMockingProtectedMethods();
Ioc::instance(get_class($entity), $entity);

$entity->shouldReceive('getDataMapper')
->andReturn($dataMapper);
Expand All @@ -118,7 +112,7 @@ public function testShouldGetAll()
$cursor = m::mock();

// Act
$entity->shouldAllowMockingProtectedMethods();
Ioc::instance(get_class($entity), $entity);

$entity->shouldReceive('getDataMapper')
->andReturn($dataMapper);
Expand All @@ -140,7 +134,7 @@ public function testShouldGetFirstWithQuery()
$cursor = m::mock();

// Act
$entity->shouldAllowMockingProtectedMethods();
Ioc::instance(get_class($entity), $entity);

$entity->shouldReceive('getDataMapper')
->andReturn($dataMapper);
Expand Down

0 comments on commit d133be2

Please sign in to comment.