Skip to content

Instantly share code, notes, and snippets.

@Shelob9
Last active May 30, 2018 16:39
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save Shelob9/0c984d81ad0fcbe4a5db407463c335cb to your computer and use it in GitHub Desktop.
<?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);
}
<?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();
}
}
}
<?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));
}
}
<?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();
}
}
}
<?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