-
Notifications
You must be signed in to change notification settings - Fork 347
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #306 from bburnichon/feature/scope-factory
Introduce Scope Factory
- Loading branch information
Showing
4 changed files
with
182 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the League\Fractal package. | ||
* | ||
* (c) Phil Sturgeon <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace League\Fractal; | ||
|
||
use League\Fractal\Resource\ResourceInterface; | ||
|
||
class ScopeFactory implements ScopeFactoryInterface | ||
{ | ||
/** | ||
* @param Manager $manager | ||
* @param ResourceInterface $resource | ||
* @param string|null $scopeIdentifier | ||
* @return Scope | ||
*/ | ||
public function createScopeFor(Manager $manager, ResourceInterface $resource, $scopeIdentifier = null) | ||
{ | ||
return new Scope($manager, $resource, $scopeIdentifier); | ||
} | ||
|
||
/** | ||
* @param Manager $manager | ||
* @param Scope $parentScopeInstance | ||
* @param ResourceInterface $resource | ||
* @param string|null $scopeIdentifier | ||
* @return Scope | ||
*/ | ||
public function createChildScopeFor(Manager $manager, Scope $parentScopeInstance, ResourceInterface $resource, $scopeIdentifier = null) | ||
{ | ||
$scopeInstance = $this->createScopeFor($manager, $resource, $scopeIdentifier); | ||
|
||
// This will be the new children list of parents (parents parents, plus the parent) | ||
$scopeArray = $parentScopeInstance->getParentScopes(); | ||
$scopeArray[] = $parentScopeInstance->getScopeIdentifier(); | ||
|
||
$scopeInstance->setParentScopes($scopeArray); | ||
|
||
return $scopeInstance; | ||
} | ||
} |
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,39 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the League\Fractal package. | ||
* | ||
* (c) Phil Sturgeon <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace League\Fractal; | ||
|
||
use League\Fractal\Resource\ResourceInterface; | ||
|
||
/** | ||
* ScopeFactoryInterface | ||
* | ||
* Creates Scope Instances for resources | ||
*/ | ||
interface ScopeFactoryInterface | ||
{ | ||
/** | ||
* @param Manager $manager | ||
* @param ResourceInterface $resource | ||
* @param string|null $scopeIdentifier | ||
* @return Scope | ||
*/ | ||
public function createScopeFor(Manager $manager, ResourceInterface $resource, $scopeIdentifier = null); | ||
|
||
/** | ||
* @param Manager $manager | ||
* @param Scope $parentScope | ||
* @param ResourceInterface $resource | ||
* @param string|null $scopeIdentifier | ||
* @return Scope | ||
*/ | ||
public function createChildScopeFor(Manager $manager, Scope $parentScope, ResourceInterface $resource, $scopeIdentifier = null); | ||
} |
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,81 @@ | ||
<?php | ||
|
||
namespace League\Fractal\Test; | ||
|
||
use League\Fractal\Manager; | ||
use League\Fractal\Resource\ResourceInterface; | ||
use League\Fractal\Scope; | ||
use League\Fractal\ScopeFactory; | ||
|
||
class ScopeFactoryTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
public function testItImplementsScopeFactoryInterface() | ||
{ | ||
$this->assertInstanceOf('League\\Fractal\\ScopeFactoryInterface', $this->createSut()); | ||
} | ||
|
||
public function testItCreatesScopes() | ||
{ | ||
$sut = $this->createSut(); | ||
|
||
$manager = $this->createManager(); | ||
$resource = $this->createResource(); | ||
$scopeIdentifier = 'foo_identifier'; | ||
|
||
$scope = $sut->createScopeFor($manager, $resource, $scopeIdentifier); | ||
|
||
$this->assertInstanceOf('League\\Fractal\\Scope', $scope); | ||
$this->assertSame($resource, $scope->getResource()); | ||
$this->assertSame($scopeIdentifier, $scope->getScopeIdentifier()); | ||
} | ||
|
||
public function testItCreatesScopesWithParent() | ||
{ | ||
$manager = $this->createManager(); | ||
|
||
$scope = new Scope($manager, $this->createResource(), 'parent_identifier'); | ||
$scope->setParentScopes([ | ||
'parent_scope', | ||
]); | ||
|
||
$resource = $this->createResource(); | ||
$scopeIdentifier = 'foo_identifier'; | ||
|
||
$expectedParentScopes = [ | ||
'parent_scope', | ||
'parent_identifier', | ||
]; | ||
|
||
$sut = $this->createSut(); | ||
$scope = $sut->createChildScopeFor($manager, $scope, $resource, $scopeIdentifier); | ||
|
||
$this->assertInstanceOf('League\\Fractal\\Scope', $scope); | ||
$this->assertSame($resource, $scope->getResource()); | ||
$this->assertSame($scopeIdentifier, $scope->getScopeIdentifier()); | ||
$this->assertEquals($expectedParentScopes, $scope->getParentScopes()); | ||
} | ||
|
||
/** | ||
* @return ScopeFactory | ||
*/ | ||
private function createSut() | ||
{ | ||
return new ScopeFactory(); | ||
} | ||
|
||
/** | ||
* @return Manager | ||
*/ | ||
private function createManager() | ||
{ | ||
return $this->getMock('League\\Fractal\\Manager'); | ||
} | ||
|
||
/** | ||
* @return ResourceInterface | ||
*/ | ||
private function createResource() | ||
{ | ||
return $this->getMock('League\\Fractal\\Resource\\ResourceInterface'); | ||
} | ||
} |