Last active
May 30, 2018 16:39
-
-
Save Shelob9/0c984d81ad0fcbe4a5db407463c335cb to your computer and use it in GitHub Desktop.
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
<?php | |
/** @inheritdoc */ | |
public static function getPosts(WP_Query $query): array | |
{ | |
$request = is_object(static::$request) ? static::$request : new \WP_REST_Request(); | |
/** | |
* Runs right before we get the results | |
* | |
* Use to change the contentGetter based on current request | |
* | |
* @param \WP_Query $query | |
* @param \WP_REST_Request $request | |
*/ | |
do_action( 'caldera_learn_rest_search_pre_get_posts', $query, $request ); | |
return static::$contentGetter->getContent($query, $request); | |
} |
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
<?php | |
namespace CalderaLearn\RestSearch; | |
use CalderaLearn\RestSearch\ContentGetter\ContentGetterContract; | |
use CalderaLearn\RestSearch\ContentGetter\PostsGenerator; | |
class Modes | |
{ | |
/** | |
* Controls loading of content generator based on mode | |
* | |
* @param \WP_Query $unused | |
* @param \WP_REST_Request $request | |
* @return ContentGetterContract | |
*/ | |
public function controlMode($unused, \WP_REST_Request $request) : ContentGetterContract | |
{ | |
switch ($request->get_param('mode')) { | |
default: | |
return $this->factory(''); | |
break; | |
} | |
} | |
/** | |
* Factory for contentGetter with cache | |
* | |
* @todo cache | |
* | |
* @param string $type | |
* @return ContentGetterContract | |
*/ | |
public function factory(string $type) : ContentGetterContract | |
{ | |
switch ($type) { | |
case PostsGenerator::class: | |
default: | |
return new PostsGenerator(); | |
} | |
} | |
} |
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
<?php | |
namespace CalderaLearn\RestSearch\Tests\Unit; | |
use CalderaLearn\RestSearch\ContentGetter\PostsGenerator; | |
use CalderaLearn\RestSearch\Modes; | |
class ModeControlTests extends TestCase | |
{ | |
/** | |
* Test that the right type of ContentGetters come out of factory | |
* | |
* @covers \CalderaLearn\RestSearch\Modes::factory() | |
*/ | |
public function testFactory() | |
{ | |
$modes = new Modes(); | |
$this->assertTrue(is_a($modes->factory(''), PostsGenerator::class)); | |
$this->assertTrue(is_a($modes->factory(PostsGenerator::class), PostsGenerator::class)); | |
} | |
/** | |
* Test that the right type of ContentGetters come out of factory | |
* | |
* @covers \CalderaLearn\RestSearch\Modes::factory() | |
*/ | |
public function testGetMode() | |
{ | |
$modes = new Modes(); | |
$mockRestRequest = \Mockery::mock('\WP_Rest_Request'); | |
$mockRestRequest->shouldReceive('get_param') | |
->once() | |
->andReturn('default'); | |
$this->assertTrue(is_a($modes->controlMode(nulll, $mockRestRequest), PostsGenerator::class)); | |
} | |
} |
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
<?php | |
namespace CalderaLearn\RestSearch; | |
use CalderaLearn\RestSearch\ContentGetter\ContentGetterContract; | |
use CalderaLearn\RestSearch\ContentGetter\PostsGenerator; | |
/** | |
* Class Modes | |
* | |
* Controls which ContentGetter is used based on a REST API param "mode" | |
*/ | |
class Modes | |
{ | |
/** | |
* The name of the filter this class uses to control search mode | |
*/ | |
const FILTER_NAME = 'caldera_learn_rest_search_get_mode'; | |
/** | |
* Controls loading of content generator based on mode | |
* | |
* @param \WP_Query $unused | |
* @param \WP_REST_Request $request | |
* @return ContentGetterContract | |
*/ | |
public function controlMode($unused, \WP_REST_Request $request) : ContentGetterContract | |
{ | |
$mode = $request->get_param('mode'); | |
$contentGetter = apply_filters(self::FILTER_NAME, null, $mode); | |
if (is_a($contentGetter, ContentGetterContract::class)) { | |
return $contentGetter; | |
} | |
switch ($mode) { | |
default: | |
return $this->factory(''); | |
break; | |
} | |
} | |
/** | |
* Factory for contentGetter with cache | |
* | |
* @todo cache | |
* | |
* @param string $type | |
* @return ContentGetterContract | |
*/ | |
public function factory(string $type) : ContentGetterContract | |
{ | |
switch ($type) { | |
case PostsGenerator::class: | |
default: | |
return new PostsGenerator(); | |
} | |
} | |
} |
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
<?php | |
namespace CalderaLearn\RestSearch\Tests\Integration; | |
use CalderaLearn\RestSearch\Modes; | |
use CalderaLearn\RestSearch\Tests\Mock\CreatePostsImplementation; | |
/** | |
* Class ModeControlTest | |
* | |
* Demonstrates and tests how the "caldera_learn_rest_search_get_mode" filter can be used to implement search systems. | |
*/ | |
class ModeControlTest extends IntegrationTestCase | |
{ | |
/** | |
* Make sure that filter can be used to make content getter extensible | |
* | |
* @covers \CalderaLearn\RestSearch\Modes::controlMode() | |
*/ | |
public function testGetModeFilterReturns() | |
{ | |
$modes = new Modes(); | |
add_filter(Modes::FILTER_NAME, function ($param, $mode) { | |
$this->assertEquals('mockMode', $mode); | |
if ('mockMode' === $mode) { | |
return new CreatePostsImplementation(); | |
} | |
return $param; | |
}, 10, 2); | |
$mockRestRequest = \Mockery::mock('\WP_Rest_Request'); | |
$mockRestRequest->shouldReceive('get_param') | |
->once() | |
->andReturn('mockMode'); | |
$contentGetter = $modes->controlMode(null, $mockRestRequest); | |
$this->assertTrue(is_a($contentGetter, CreatePostsImplementation::class)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment