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

Change prepareRequest to a reducer #10

Merged
merged 5 commits into from
Feb 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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