Exemplo n.º 1
0
 /**
  * Specter JSON Fake Data
  *
  * The route should return json data of Specter format, and this middleware
  * will substitute fake data into it.
  *
  * @param RequestInterface  $request  PSR7 request
  * @param ResponseInterface $response PSR7 response
  * @param callable          $next     Next middleware
  *
  * @return ResponseInterface
  * @throws InvalidArgumentException
  * @throws LogicException
  */
 public function __invoke(RequestInterface $request, ResponseInterface $response, callable $next)
 {
     /**
      * We are a post processor, and so we run the $next immediately
      *
      * @var ResponseInterface $response
      */
     $response = $next($request, $response);
     // Decode the json returned by the route and prepare it for mock data
     // processing.
     $fixture = @json_decode($response->getBody()->getContents(), true);
     if (json_last_error() !== JSON_ERROR_NONE) {
         throw new LogicException('Failed to parse json string. Error: ' . json_last_error_msg());
     }
     // We will not process files without the Specter trigger, and instead
     // return an unchanged response.
     if (!array_key_exists($this->specterTrigger, $fixture)) {
         return $response;
     }
     // Process the fixture data, using a seed in case the designer wants
     // a repeatable result.
     $seed = $request->getHeader('SpecterSeed');
     $specter = new Specter(array_shift($seed));
     $json = $specter->substituteMockData($fixture);
     // Prepare a fresh body stream
     $data = json_encode($json);
     $stream = Psr7\stream_for($data);
     // Return an immutable body in a cloned $request object
     return $response->withBody($stream);
 }
Exemplo n.º 2
0
 /**
  * Handle an incoming request.
  *
  * @param Request $request
  * @param Closure $next
  *
  * @return Response
  * @throws LogicException
  */
 public function handle(Request $request, Closure $next)
 {
     /**
      * @var Response $response
      */
     $response = $next($request);
     $fixture = @json_decode($response->getContent(), true);
     if (json_last_error() !== JSON_ERROR_NONE) {
         throw new LogicException('Failed to parse json string. Error: ' . json_last_error_msg());
     }
     // We will not process files without the Specter trigger, and instead
     // return an unchanged response.
     if (!array_key_exists($this->specterTrigger, $fixture)) {
         return $response;
     }
     // Process the fixture data, using a seed in case the designer wants
     // a repeatable result.
     $seed = $request->header('SpecterSeed', 0);
     $specter = new Specter($seed);
     $json = $specter->substituteMockData($fixture);
     return $response->setContent(json_encode($json));
 }
Exemplo n.º 3
0
 /**
  * Specter should be able to select a related value from a list
  *
  * @test
  * @return void
  */
 public function specterCanSelectRelatedValuesWithStaticValue()
 {
     $seed = 2;
     $specter = new Specter($seed);
     $json = file_get_contents(TEST_FIXTURE_FOLDER . '/related-element.json');
     $fixture = json_decode($json, true);
     $data = $specter->substituteMockData($fixture);
     self::assertEquals($data['type'], 'guest', 'The seed should have made the type == guest');
     self::assertEquals($data['name'], 'Guest User', 'A guest user should have been created');
 }
Exemplo n.º 4
0
<?php

/**
 * Specter command line tool
 *
 * @author    Platform Team <*****@*****.**>
 * @copyright 2016 Help Scout
 */
require __DIR__ . '/../vendor/autoload.php';
use HelpScout\Specter\Specter;
$fixtureFolder = __DIR__ . '/../tests/fixture';
$specter = new Specter();
$json = file_get_contents($fixtureFolder . '/customer.json');
$fixture = @json_decode($json, true);
if (json_last_error() !== JSON_ERROR_NONE) {
    throw new LogicException('Failed to parse json string. Error: ' . json_last_error_msg());
}
var_dump($specter->substituteMockData($fixture));
/* End of file cli.php */