Skip to content

Commit

Permalink
tweak api, match spec exactly
Browse files Browse the repository at this point in the history
  • Loading branch information
Arty Buldauskas committed Feb 22, 2018
1 parent 512e36d commit 6f6ed8e
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 10 deletions.
9 changes: 6 additions & 3 deletions src/Renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -250,15 +250,18 @@ protected function createJobs()
*/
protected function prepareRequest($jobs)
{
$parsedJobs = $jobs;
foreach ($this->plugins as $plugin) {
$jobs = $plugin->prepareRequest($jobs);
// Pass both jobs we are working with an original, incoming jobs so that every plugin has a chance
// to see _all_ original jobs.
$parsedJobs = $plugin->prepareRequest($parsedJobs, $jobs);
}

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

return [$shouldSend, $jobs];
return [$shouldSend, $parsedJobs];
}
}
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' => []]);
}
}
}
7 changes: 6 additions & 1 deletion tests/RendererTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 6f6ed8e

Please sign in to comment.