Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make extensible #8

Open
wants to merge 7 commits into
base: gluing-together-more
from

add filter

  • Loading branch information...
Shelob9 committed May 29, 2018
commit 82db8a7e53fb430012dc8345dc2617e4e8af57f3
@@ -0,0 +1,39 @@
<?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));

This comment has been minimized.

Copy link
@hellofromtonya

hellofromtonya Jun 2, 2018

Collaborator

Change this assert to use assertInstanceOf instead of the assertTrue(is_a())

$this->assertInstanceOf(CreatePostsImplementation::class, $contentGetter);
}
}
@@ -5,8 +5,10 @@
use CalderaLearn\RestSearch\ContentGetter\PostsGenerator;
use CalderaLearn\RestSearch\Modes;
use Brain\Monkey\Filters;
use CalderaLearn\RestSearch\Tests\Mock\CreatePostsImplementation;
class ModeControlTests extends TestCase
class ModeControlTest extends TestCase
{
/**
@@ -24,7 +26,7 @@ public function testFactory()
/**
* Test that the right type of ContentGetters come out of factory
*
* @covers \CalderaLearn\RestSearch\Modes::factory()
* @covers \CalderaLearn\RestSearch\Modes::controlMode()
*/
public function testGetMode()
{
@@ -34,6 +36,23 @@ public function testGetMode()
->once()
->andReturn('default');
$this->assertTrue(is_a($modes->controlMode(nulll, $mockRestRequest), PostsGenerator::class));
$this->assertTrue(is_a($modes->controlMode(null, $mockRestRequest), PostsGenerator::class));

This comment has been minimized.

Copy link
@hellofromtonya

hellofromtonya Jun 2, 2018

Collaborator

Change this assert to use assertInstanceOf:

$this->assertInstanceOf(PostsGenerator::class, $modes->controlMode(null, $mockRestRequest));
}
/**
* Test that the filter for controlling mode is running
*
* @covers \CalderaLearn\RestSearch\Modes::controlMode()
*/
public function testGotModeFilterRuns()
{
$modes = new Modes();
$mockRestRequest = \Mockery::mock('\WP_Rest_Request');
$mockRestRequest->shouldReceive('get_param')
->once()
->andReturn('default');
$modes->controlMode(null, $mockRestRequest);
$this->assertTrue(Filters\applied(Modes::FILTER_NAME) > 0);

This comment has been minimized.

Copy link
@hellofromtonya

hellofromtonya Jun 2, 2018

Collaborator

Change the assert to assertGreaterThan:

$this->assertGreaterThan(0, Filters\applied(Modes::FILTER_NAME));
}
}
@@ -6,9 +6,19 @@
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
*
@@ -18,7 +28,12 @@ class Modes
*/
public function controlMode($unused, \WP_REST_Request $request) : ContentGetterContract
{
switch ($request->get_param('mode')) {
$mode = $request->get_param('mode');
$contentGetter = apply_filters(self::FILTER_NAME, null, $mode);
if (is_a($contentGetter, ContentGetterContract::class)) {
return $contentGetter;
}
switch ($mode) {

This comment has been minimized.

Copy link
@hellofromtonya

hellofromtonya Jun 2, 2018

Collaborator

Is the point to this switch to be a placeholder for future extension or even discussion? In its current design, we could just return $this->factory(''); and get rid of the switch code structure.

default:
return $this->factory('');
break;

This comment has been minimized.

Copy link
@hellofromtonya

hellofromtonya Jun 2, 2018

Collaborator

This isn't necessary as we are returning and the break will never be reached.

ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.