Skip to content

We are vertigo #63

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

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"bin-dir": "bin/"
},
"require": {
"rhubarbphp/rhubarb": "1.0.x-dev@dev"
"rhubarbphp/rhubarb": "*"
},
"require-dev": {
"rhubarbphp/custard" : "^1.0.4",
Expand Down
2 changes: 1 addition & 1 deletion src/Collections/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ public function filter(Filter $filter)
* Where repository specific optimisation is available this will be leveraged to run the batch
* update at the data source rather than iterating over the items.
*
* @param Array $propertyPairs An associative array of key value pairs to update
* @param array $propertyPairs An associative array of key value pairs to update
* @param bool $fallBackToIteration If the repository can't perform the action directly, perform the update by iterating over all the models in the collection. You should only pass true if you know that the collection doesn't meet the criteria for an optimised update and the iteration of items won't cause problems
* iterating over all the models in the collection. You should only pass true
* if you know that the collection doesn't meet the criteria for an optimised
Expand Down
12 changes: 12 additions & 0 deletions src/Custard/BulkScenario.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Rhubarb\Stem\Custard;

/**
* Bulk scenarios are ignored unless the bulk flag -b is provided to the seed-data command.
*
* Bulk scenarios provide larger results sets for testing performance, paging, filtering etc.
*/
class BulkScenario extends Scenario
{
}
33 changes: 32 additions & 1 deletion src/Custard/CommandHelpers/GetterOrSetterMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,40 @@ public static function fromReflectionMethod(\ReflectionMethod $method, $existing
$methodName = $method->getName();

if (stripos($methodName, 'get') === 0) {
$parameters = $method->getParameters();

$wrapper->readable = true;
foreach ($parameters as $parameter) {
if (!$parameter->allowsNull()) {
// If a "get" method has any non-nullable parameters, it's not a property getter
$wrapper->readable = false;
break;
}
}

if (!$wrapper->readable) {
return false;
}
} elseif (stripos($methodName, 'set') === 0) {
$wrapper->writable = true;
$parameters = $method->getParameters();
$paramCount = count($parameters);

if ($paramCount > 0) {
// A "set" method must take a parameter to be a property setter
$wrapper->writable = true;

for ($i = 1; $i < $paramCount; $i++) {
if (!$parameter->allowsNull()) {
// If a "set" method has more than 1 non-nullable parameter, it's not a property setter
$wrapper->writable = false;
break;
}
}
}

if (!$wrapper->writable) {
return false;
}
} else {
// Neither a getter nor a setter
return false;
Expand Down
27 changes: 27 additions & 0 deletions src/Custard/DescribedDemoDataSeederInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Rhubarb\Stem\Custard;

use Symfony\Component\Console\Output\Output;

interface DescribedDemoDataSeederInterface extends DemoDataSeederInterface
{
/**
* Describes the test scenarios provided by this seeder.
*
* The $output interface has a number of useful styles predefined:
*
* <critical> Red and bold
* <bold> Bold
* <blink> Blinking (not supported everywhere)
*
* Other options can be defined and the defaults provided by Symfony are
* still present. You can read more at the link below:
*
* https://symfony.com/doc/current/console/coloring.html
*
* @param Output $output
* @return mixed
*/
public function describeDemoData(Output $output);
}
55 changes: 55 additions & 0 deletions src/Custard/Scenario.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace Rhubarb\Stem\Custard;

use Symfony\Component\Console\Output\OutputInterface;

class Scenario
{
/**
* @var callable $seedScenario
*/
private $seedScenario;

/**
* @var ScenarioDescription $scenarioDescription
*/
private $scenarioDescription;

/**
* @var string
*/
private $name;

/**
* Scenario constructor.
* @param string $name of the scenario
* @param callable $seedScenario callback, a ScenarioDescription will be given as the first parameter
*
*/
public function __construct(string $name, callable $seedScenario)
{
$this->seedScenario = $seedScenario;
$this->scenarioDescription = new ScenarioDescription();
$this->name = $name;
}

/**
* @param OutputInterface $output
* Run the data seeder and describe what happened to $output
*/
public function run(OutputInterface $output)
{
$seedScenario = $this->seedScenario;
$seedScenario($this->scenarioDescription);
$this->scenarioDescription->describe($output);
}

/**
* @return string
*/
public function getName(): string
{
return $this->name;
}
}
77 changes: 77 additions & 0 deletions src/Custard/ScenarioDataSeeder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

namespace Rhubarb\Stem\Custard;

use Symfony\Component\Console\Output\OutputInterface;

abstract class ScenarioDataSeeder implements DemoDataSeederInterface
{
private static $scenarioCount = 1;

private static $alreadyRan = [];

/**
* @return array|DemoDataSeederInterface[]
*/
protected function getPreRequisiteSeeders()
{
return [];
}

/**
* A hook for logic to run before each scenario
* @param Scenario $scenario
*/
protected function beforeScenario(Scenario $scenario)
{
}

/**
* A hook for logic to run after each scenario
* @param Scenario $scenario
*/
protected function afterScenario(Scenario $scenario)
{
}

public function seedData(OutputInterface $output, $includeBulk = false)
{
$class = get_class($this);
if (in_array($class, self::$alreadyRan)) {
return;
}

self::$alreadyRan[] = $class;

foreach ($this->getPreRequisiteSeeders() as $seeder) {
if (is_string($seeder)) {
$seeder = new $seeder();
}

if (!($seeder instanceof DemoDataSeederInterface)) {
throw new \InvalidArgumentException(get_class($seeder) . " does not extend DemoDataSeederInterface.");
}

$seeder->seedData($output, $includeBulk);
}

foreach ($this->getScenarios() as $scenario) {
if ($includeBulk || !($scenario instanceof BulkScenario)) {
$this->beforeScenario($scenario);

$output->writeln("");
$output->writeln("<comment>Scenario " . self::$scenarioCount . ": <bold>" . $scenario->getName() . '</bold></comment>');
$output->writeln(str_repeat('-', 11 + strlen(self::$scenarioCount) + strlen($scenario->getName())));
$scenario->run($output);
self::$scenarioCount++;

$this->afterScenario($scenario);
}
}
}

/**
* @return Scenario[]
*/
abstract function getScenarios(): array;
}
32 changes: 32 additions & 0 deletions src/Custard/ScenarioDescription.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Rhubarb\Stem\Custard;
use Symfony\Component\Console\Output\OutputInterface;

class ScenarioDescription
{
/**
* @var string[] message lines to write
*/
private $lines = [];

/**
* @param string $line to write
* @param string $spacer indentation
* @return $this for the fluent pattern
*/
public function writeLine(string $line, $spacer = " ")
{
$this->lines[] = $spacer . $line;
return $this;
}

/**
* @param OutputInterface $output
* Write all oif the lines
*/
public function describe(OutputInterface $output)
{
$output->writeln($this->lines);
}
}
Loading