-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace patchable get_model with a provider #20
- Loading branch information
Showing
5 changed files
with
147 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the ICanBoogie package. | ||
* | ||
* (c) Olivier Laviale <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace ICanBoogie\ActiveRecord; | ||
|
||
/** | ||
* Provides a {@link Model} instance. | ||
*/ | ||
class ModelProvider | ||
{ | ||
/** | ||
* @var callable {@link Model} provider | ||
*/ | ||
static private $provider; | ||
|
||
/** | ||
* Defines the {@link Model} provider. | ||
* | ||
* @param callable $provider | ||
* | ||
* @return callable The previous provider, or `null` if none was defined. | ||
*/ | ||
static public function define(callable $provider) | ||
{ | ||
$previous = self::$provider; | ||
|
||
self::$provider = $provider; | ||
|
||
return $previous; | ||
} | ||
|
||
/** | ||
* Returns the current provider. | ||
* | ||
* @return callable|null | ||
*/ | ||
static public function defined() | ||
{ | ||
return self::$provider; | ||
} | ||
|
||
/** | ||
* Undefine the provider. | ||
*/ | ||
static public function undefine() | ||
{ | ||
self::$provider = null; | ||
} | ||
|
||
/** | ||
* Returns a {@link Model} instance using the provider. | ||
* | ||
* @param string $id Model identifier. | ||
* | ||
* @return Model | ||
* | ||
* @throws ModelNotDefined if the model cannot be provided. | ||
*/ | ||
static public function provide($id) | ||
{ | ||
$provider = self::$provider; | ||
|
||
if (!$provider) | ||
{ | ||
throw new \LogicException("No provider is defined yet. Please define one with `ModelProvider::define(\$provider)`."); | ||
} | ||
|
||
$model = $provider($id); | ||
|
||
if (!$model) | ||
{ | ||
throw new ModelNotDefined($id); | ||
} | ||
|
||
return $model; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,53 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the ICanBoogie package. | ||
* | ||
* (c) Olivier Laviale <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace ICanBoogie; | ||
|
||
use ICanBoogie\ActiveRecord\Helpers; | ||
use ICanBoogie\ActiveRecord\Model; | ||
use ICanBoogie\ActiveRecord\ModelCollection; | ||
use ICanBoogie\ActiveRecord\ModelNotDefined; | ||
use ICanBoogie\ActiveRecord\ModelProvider; | ||
use ICanBoogie\ActiveRecord\RecordNotValid; | ||
use ICanBoogie\ActiveRecord\Schema; | ||
use ICanBoogie\ActiveRecordTest\Sample; | ||
use ICanBoogie\ActiveRecordTest\ValidateCase; | ||
|
||
/** | ||
* @covers \ICanBoogie\ActiveRecord | ||
* @group record | ||
*/ | ||
class ActiveRecordTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
private $sample_model; | ||
|
||
public function setUp() | ||
{ | ||
$sample_model = &$this->sample_model; | ||
|
||
if ($sample_model) | ||
{ | ||
return; | ||
} | ||
|
||
$sample_model = $this->mockModel(); | ||
|
||
Helpers::patch('get_model', function($model_id) use ($sample_model) { | ||
ModelProvider::define(function($model_id) use ($sample_model) { | ||
|
||
if ($model_id === 'sample') | ||
{ | ||
return $sample_model; | ||
} | ||
|
||
throw new ModelNotDefined($sample_model); | ||
return null; | ||
|
||
}); | ||
|
||
$this->sample_model = $sample_model; | ||
} | ||
|
||
public function test_should_resolve_model_id_from_const() | ||
|