Skip to content

Commit

Permalink
BREAKING: Change prepareRequest to a reducer (#10)
Browse files Browse the repository at this point in the history
* Change prepareRequest to a reducer

* tweak api, match spec exactly

* typehint prepareRequest

* Revert "typehint prepareRequest"

This reverts commit 79ce391.

* naming, fix a few phpcs warnings
  • Loading branch information
ballercat authored and roippi committed Feb 23, 2018
1 parent 9112a8b commit 53549d6
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 17 deletions.
19 changes: 11 additions & 8 deletions src/Renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -245,21 +245,24 @@ protected function createJobs()
}

/**
* @param $jobs
* Prepare Request
*
* @param array $jobs Jobs
*
* @return array
*/
protected function prepareRequest($jobs)
{
$preparedJobs = array_map(function ($job) {
foreach ($this->plugins as $plugin) {
$job = $plugin->prepareRequest($job);
}
return $job;
}, $jobs);
$preparedJobs = $jobs;
foreach ($this->plugins as $plugin) {
// Pass both jobs we are working with an original, incoming jobs so
// that every plugin has a chance to see _all_ original jobs.
$preparedJobs = $plugin->prepareRequest($preparedJobs, $jobs);
}

$shouldSend = true;
foreach ($this->plugins as $plugin) {
$shouldSend = $shouldSend && $plugin->shouldSendRequest($jobs);
$shouldSend = $shouldSend && $plugin->shouldSendRequest($preparedJobs);
}

return [$shouldSend, $preparedJobs];
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/BasePlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ class BasePlugin implements Plugin
/**
* {@inheritdoc}
*/
public function prepareRequest($request)
public function prepareRequest(array $jobs, array $originalJobs)
{
return $request;
return $jobs;
}

/**
Expand Down
5 changes: 3 additions & 2 deletions src/plugins/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ interface Plugin
public function getViewData($name, array $data);

/**
* @param \WF\Hypernova\Job $request
* @param \WF\Hypernova\Job[] $jobs
* @param \WF\Hypernova\Job[] $originalJobs
* @return \WF\Hypernova\Job
*/
public function prepareRequest($request);
public function prepareRequest(array $jobs, array $originalJobs);

/**
* @param \WF\Hypernova\Job[] $jobs
Expand Down
5 changes: 3 additions & 2 deletions tests/BasePluginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ public function testPrepareRequest()
$plugin = new BasePlugin();

$job = Job::fromArray(['name' => 'foo', 'data' => ['bar' => 'baz']]);
$jobs = [$job];

$this->assertEquals($job, $plugin->prepareRequest($job));
$this->assertEquals($jobs, $plugin->prepareRequest($jobs, [$jobs]));
}

public function testOnError()
Expand Down Expand Up @@ -80,4 +81,4 @@ private function makeJob()
{
return Job::fromArray(['name' => 'foo', 'data' => []]);
}
}
}
11 changes: 8 additions & 3 deletions tests/RendererTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ public function testPrepareRequestCallsPlugin()

$plugin->expects($this->exactly(2))
->method('prepareRequest')
->with($this->equalTo($this->defaultJob))
->willReturn($this->defaultJob);
->with($this->equalTo([$this->defaultJob]))
->willReturn([$this->defaultJob]);

$this->renderer->addPlugin($plugin);
$this->renderer->addPlugin($plugin);
Expand All @@ -120,6 +120,9 @@ public function testShouldSend()
$pluginDontSend = $this->createMock(BasePlugin::class);
$pluginDoSend = $this->createMock(BasePlugin::class);

$pluginDoSend->method('prepareRequest')->will($this->returnArgument(0));
$pluginDontSend->method('prepareRequest')->will($this->returnArgument(0));

$pluginDontSend->expects($this->once())
->method('shouldSendRequest')
->willReturn(false);
Expand All @@ -130,7 +133,9 @@ public function testShouldSend()
$this->renderer->addPlugin($pluginDontSend);
$this->renderer->addPlugin($pluginDoSend);

$this->assertFalse($this->callInternalMethodOfThing($this->renderer, 'prepareRequest', [[$this->defaultJob]])[0]);
$result = $this->callInternalMethodOfThing($this->renderer, 'prepareRequest', [[$this->defaultJob]]);
$this->assertEquals([$this->defaultJob], $result[1]);
$this->assertFalse($result[0]);
}

public function testRenderShouldNotSend()
Expand Down

0 comments on commit 53549d6

Please sign in to comment.