public function testRenderFieldWithPrimitives() { $spec = (new FormSpec())->withFieldType('first_name', Boa::string())->withFieldType('last_name', Boa::string())->withFieldDefault('first_name', 'Bob')->withFieldType('age', Boa::integer())->withFieldType('focus', Boa::boolean())->withFieldType('awesome', Boa::boolean())->withFieldDefault('awesome', true)->withFieldType('price', Boa::float()); $presenter = new FormSpecPresenter($spec); $this->assertEquals((new Input(['id' => 'first_name', 'class' => 'form-control', 'type' => 'text', 'value' => 'Bob']))->render(), $presenter->renderField('first_name')->render()); $this->assertEquals((new Input(['id' => 'last_name', 'class' => 'form-control', 'type' => 'text']))->render(), $presenter->renderField('last_name')->render()); $this->assertEquals((new Input(['id' => 'age', 'class' => 'form-control', 'type' => 'number']))->render(), $presenter->renderField('age')->render()); $this->assertEquals((new Input(['id' => 'focus', 'class' => 'form-control', 'type' => 'checkbox']))->render(), $presenter->renderField('focus')->render()); $this->assertEquals((new Input(['id' => 'awesome', 'class' => 'form-control', 'type' => 'checkbox', 'checked' => null]))->render(), $presenter->renderField('awesome')->render()); $this->assertEquals((new Input(['id' => 'price', 'class' => 'form-control', 'type' => 'number']))->render(), $presenter->renderField('price')->render()); }
public function testComplexSpec() { $graph = new SpecGraph(); $graph->add('input', [], Spec::define(['sleepy' => Boa::boolean(), 'tennis_balls' => Boa::integer(), 'message' => Boa::either(Boa::string(), Boa::integer())], [], ['message'])); $graph->add('allowedMessage', ['input'], Spec::define(['message' => [Boa::in(['hi', 'how are you?', 'you dumb']), Boa::in(['hi', 'how are you?', 'you are smart'])]], [], ['message'])); $graph->add('validBallCount', ['input'], Spec::define(['tennis_balls' => Boa::between(1, 10)])); $graph->add('additionalBallProps', ['validBallCount'], Spec::define(['ball_color' => [Boa::string(), Boa::in(['blue', 'red', 'yellow'])]], [], ['ball_color'])); $result = $graph->check(['sleepy' => true, 'tennis_balls' => 3, 'message' => 'hi', 'ball_color' => 'blue']); $this->assertTrue($result->passed()); $result2 = $graph->check(['sleepy' => 1, 'tennis_balls' => 3]); $this->assertEqualsMatrix([[true, $result2->failed()], [1, count($result2->getFailed())], [['message'], $result2->getMissing()]]); $result3 = $graph->check(['sleepy' => true, 'tennis_balls' => -30, 'message' => 'hello']); $this->assertEqualsMatrix([[true, $result3->failed()], [2, count($result3->getFailed())], [[], $result3->getMissing()]]); $result4 = $graph->check(['sleepy' => true, 'tennis_balls' => 3, 'message' => 'how are you?']); $this->assertEqualsMatrix([[true, $result4->failed()], [0, count($result4->getFailed())], [['ball_color'], $result4->getMissing()]]); $result5 = $graph->check(['sleepy' => true, 'tennis_balls' => 3, 'message' => 'how are you?', 'ball_color' => 'liquid_gold']); $this->assertEqualsMatrix([[true, $result5->failed()], [1, count($result5->getFailed())], [[], $result5->getMissing()]]); }
public function testFireWithValidSpec() { $laravelJob = m::mock(LaravelJob::class); $laravelJob->shouldReceive('delete')->atLeast()->once(); $job = new Job(); $job->state = JobState::QUEUED; $job->data = json_encode(['omg' => false, 'why_not' => 'because']); $handler = m::mock(BaseTask::class); $handler->shouldReceive('fire')->with($job, m::type(JobSchedulerInterface::class))->atLeast()->once(); $handler->shouldReceive('getSpec')->andReturn(Spec::define(['omg' => Boa::boolean()], ['yes' => 'please'], ['why_not'])); $impersonator = new Impersonator(); $impersonator->mock(JobRepositoryInterface::class, function (MockInterface $mock) use($job) { $mock->shouldReceive('find')->with(1337)->atLeast()->once()->andReturn($job); $mock->shouldReceive('started')->with($job, m::type('string'))->atLeast()->once(); $mock->shouldReceive('complete')->with($job)->atLeast()->once(); }); $impersonator->mock(HandlerResolverInterface::class, function (MockInterface $mock) use($job, $handler) { $mock->shouldReceive('resolve')->with($job)->atLeast()->once()->andReturn($handler); }); /** @var RunTaskCommand $command */ $command = $impersonator->make(RunTaskCommand::class); $command->fire($laravelJob, ['job_id' => 1337]); }
/** * Get the current offers for an ASIN from the Research API. * * @param string $asin * @param bool $noPaapi * * @return OffersResponse */ public function getOffers($asin, $noPaapi = false) { Arguments::contain(Boa::string(), Boa::boolean())->check($asin, $noPaapi); $query = ['format' => 'pretty']; if ($noPaapi) { $query['nopaapi'] = true; } return (new OffersResponseFactory())->makeFromResponse($this->client->get(vsprintf('/v1/offers/%s', [$asin]), ['query' => $query])); }
/** * Return the first value if the condition is true, otherwise, return the * second. * * @param bool $biased * @param mixed|Closure $one * @param mixed|Closure $other * * @return mixed */ public static function firstBias($biased, $one, $other) { Arguments::define(Boa::boolean(), Boa::any(), Boa::any())->check($biased, $one, $other); if ($biased) { return static::thunk($one); } return static::thunk($other); }