Skip to content
This repository has been archived by the owner on Sep 23, 2022. It is now read-only.

[WIP] Apply json-schema constraints #8

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 0 additions & 16 deletions Generator/FakeDataProvider.php

This file was deleted.

35 changes: 0 additions & 35 deletions Generator/FakeDataProvider/Dumb.php

This file was deleted.

53 changes: 0 additions & 53 deletions Generator/FakeDataProvider/Faker.php

This file was deleted.

2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -16,7 +16,7 @@
},
"autoload": {
"psr-4": {
"Muse\\": "",
"Muse\\": "src/",
"spec\\Muse\\": "spec/"
}
},
Expand Down
59 changes: 55 additions & 4 deletions spec/Generator/FakeDataProvider/FakerSpec.php
Expand Up @@ -12,22 +12,55 @@ function it_is_a_fake_data_provider()
$this->shouldBeAnInstanceOf('Muse\Generator\FakeDataProvider');
}

function it_generate_some_integer()
function it_generates_some_integer()
{
$this->getInteger(1)->shouldBeInteger();
}

function it_generate_some_float()
public function it_generates_some_integer_between_limits()
{
$this->getInteger(2, false, 4, false)->shouldReturnOneOf([2, 3, 4]);
$this->getInteger(2, true, 4, false)->shouldReturnOneOf([3, 4]);
$this->getInteger(2, false, 4, true)->shouldReturnOneOf([2, 3]);
$this->getInteger(2, true, 4, true)->shouldReturn(3);
}

function it_generates_some_float()
{
$this->getFloat(0)->shouldBeFloat();
}

function it_generate_some_string()
function it_generates_some_float_between_limits()
{
$this->getFloat(5, false, 7, false)->shouldBeLowerOrEqualTo(7);
$this->getFloat(5, false, 7, false)->shouldBeUpperOrEqualTo(5);

$this->getFloat(5, false, 7, true)->shouldBeLowerThan(7);
$this->getFloat(5, false, 7, true)->shouldBeUpperOrEqualTo(5);

$this->getFloat(5, true, 7, false)->shouldBeLowerOrEqualTo(7);
$this->getFloat(5, true, 7, false)->shouldBeUpperThan(5);

$this->getFloat(5, true, 7, true)->shouldBeLowerThan(7);
$this->getFloat(5, true, 7, true)->shouldBeUpperThan(5);
}

function it_generates_some_string()
{
$this->getString(255)->shouldBeString();
}

function it_generate_some_boolean()
function it_generates_some_string_following_a_regex()
{
}

public function it_geterates_a_string_with_limit_lengths()
{
$this->getString(2, 5)->shouldNotBeShorterThan(2);
$this->getString(2, 5)->shouldNotBeLongerThan(5);
}

function it_generates_some_boolean()
{
$this->getBoolean()->shouldBeBoolean();
}
Expand All @@ -43,6 +76,24 @@ public function getMatchers()
'returnOneOf' => function ($subject, $key) {
return false !== array_search($subject, $key);
},
'beShorterThan' => function ($string, $max) {
return strlen($string) < $max;
},
'beLongerThan' => function ($string, $min) {
return strlen($string) > $min;
},
'beLowerThan' => function ($num, $max) {
return $num < $max;
},
'beUpperThan' => function ($num, $min) {
return $num > $min;
},
'beLowerOrEqualTo' => function ($num, $max) {
return $num <= $max;
},
'beUpperOrEqualTo' => function ($num, $min) {
return $num >= $min;
},
];
}
}
File renamed without changes.
16 changes: 16 additions & 0 deletions src/Generator/FakeDataProvider.php
@@ -0,0 +1,16 @@
<?php

namespace Muse\Generator;

interface FakeDataProvider
{
public function getInteger($minimum = 0, $exclusiveMinimum = false, $maximum = 1000, $exclusiveMaximum = false, $multipleOf = 1);

public function getFloat($minimum = 0, $exclusiveMinimum = false, $maximum = 1000, $exclusiveMaximum = false, $multipleOf = 1);

public function getString($minLength = 10, $maxLength = 100, $pattern = null, $format = null);

public function getBoolean();

public function getEnum(array $enum);
}
50 changes: 50 additions & 0 deletions src/Generator/FakeDataProvider/Dumb.php
@@ -0,0 +1,50 @@
<?php

namespace Muse\Generator\FakeDataProvider;

use Muse\Generator\FakeDataProvider;

class Dumb implements FakeDataProvider
{
/**
* {@inheritdoc}
*/
public function getInteger($minimum = 0, $exclusiveMinimum = false, $maximum = 1000, $exclusiveMaximum = false, $multipleOf = 1)
{
return 1;
}

/**
* {@inheritdoc}
*/
public function getFloat($minimum = 0, $exclusiveMinimum = false, $maximum = 1000, $exclusiveMaximum = false, $multipleOf = 1)
{
return 1.0;
}

/**
* {@inheritdoc}
*/
public function getString($minLength = 10, $maxLength = 100, $pattern = null, $format = null)
{
return str_pad('foo', 10);
}

/**
* {@inheritdoc}
*/
public function getBoolean()
{
return true;
}

/**
* {@inheritdoc}
*/
public function getEnum(array $enum)
{
if (0 < count($enum)) {
return current($enum);
}
}
}
121 changes: 121 additions & 0 deletions src/Generator/FakeDataProvider/Faker.php
@@ -0,0 +1,121 @@
<?php

namespace Muse\Generator\FakeDataProvider;

use Muse\Generator\FakeDataProvider;
use Faker\Generator;
use Faker\Factory;
use ReverseRegex\Lexer;
use ReverseRegex\Random\SimpleRandom;
use ReverseRegex\Parser;
use ReverseRegex\Generator\Scope;
use DateTime;

class Faker implements FakeDataProvider
{
/**
* @var Faker\Generator
*/
private $faker;

public function __construct(Generator $faker = null)
{
$this->faker = $faker ?: Factory::create();
}

/**
* {@inheritdoc}
*
* @TODO Apply the multipleOf to the result
*/
public function getInteger($minimum = 0, $exclusiveMinimum = false, $maximum = 1000, $exclusiveMaximum = false, $multipleOf = 1)
{
$maximum = $maximum ?: $minimum * 10;

if (true === $exclusiveMinimum && false === $exclusiveMaximum) {
do {
$result = $this->faker->numberBetween($minimum, $maximum);
} while ($result === $minimum);

return $result;
}

if (false === $exclusiveMinimum && true === $exclusiveMaximum) {
do {
$result = $this->faker->numberBetween($minimum, $maximum);
} while ($result === $maximum);

return $result;
}

if (true === $exclusiveMinimum && true === $exclusiveMaximum) {
do {
$result = $this->faker->numberBetween($minimum, $maximum);
} while ($result === $minimum || $result === $maximum);

return $result;
}

return $this->faker->numberBetween($minimum, $maximum);
}

/**
* {@inheritdoc}
*/
public function getFloat($minimum = 0, $exclusiveMinimum = false, $maximum = 1000, $exclusiveMaximum = false, $multipleOf = 1)
{
return floatval($this->getInteger($minimum, $exclusiveMinimum, $maximum, $exclusiveMaximum, $multipleOf));
}

/**
* {@inheritdoc}
*/
public function getString($minLength = 10, $maxLength = null, $pattern = null, $format = null)
{
switch ($format) {
case 'date-time':
return $this->faker->date(DateTime::RFC3339);
case 'email':
return $this->faker->email;
case 'hostname':
return $this->faker->domainName;
case 'ipv4':
return $this->faker->ipv4;
case 'ipv6':
return $this->faker->ipv6;
case 'uri':
return $this->faker->url;
}

if (null !== $pattern) {
// @TODO Generate a string from a regex if possible
}

$maxLength = $maxLength ?: $minLength * 10;
$result = '';

do {
$result .= $this->faker->sentence;
} while (strlen($result) < $maxLength);

return substr($result, 0, $this->faker->numberBetween($minLength, $maxLength));
}

/**
* {@inheritdoc}
*/
public function getBoolean()
{
return $this->faker->boolean;
}

/**
* {@inheritdoc}
*/
public function getEnum(array $enum)
{
if (count($enum) > 0) {
return $this->faker->randomElement($enum);
}
}
}