Skip to content
This repository has been archived by the owner on Jan 3, 2023. It is now read-only.

Commit

Permalink
Merge pull request #6 from karriereat/feature/remove-template-data
Browse files Browse the repository at this point in the history
remove demo data and add StateManager
  • Loading branch information
fetzi authored Jun 7, 2017
2 parents ff8e938 + 54a4515 commit e1264fc
Show file tree
Hide file tree
Showing 11 changed files with 77 additions and 142 deletions.
5 changes: 0 additions & 5 deletions config/mocky.php

This file was deleted.

68 changes: 0 additions & 68 deletions example/mocks/users/list-users.json

This file was deleted.

15 changes: 0 additions & 15 deletions example/mocks/users/single-user.json

This file was deleted.

12 changes: 0 additions & 12 deletions example/tests/user-simple.json

This file was deleted.

3 changes: 0 additions & 3 deletions public/.htaccess

This file was deleted.

9 changes: 0 additions & 9 deletions public/index.php

This file was deleted.

9 changes: 7 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,15 @@

Mocky is a simple API mocking solution written in PHP based on the [Slim Framework](https://www.slimframework.com/).

## Creating a new mocky API
## Installation
You can either create a new mock api by using the `mocky-template` as a starting point
```
composer create-project karriere/mocky-template /destination/path
```

or add mocky as a dependency to your project.
```
composer create-project karriere/mocky /destination/path
composer require karriere/mocky
```

## Configuration
Expand Down
3 changes: 1 addition & 2 deletions src/Mocky.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Karriere\Mocky;

use Karriere\Mocky\Models\State;
use Karriere\Mocky\Router\MockyRouter;
use Karriere\Mocky\Router\TestRouter;
use Slim\App;
Expand Down Expand Up @@ -38,7 +37,7 @@ public function __construct(Configuration $config)
$scope = reset($scope);
}

$state = State::load($scope);
$state = (new StateManager($config))->load($scope);

(new TestRouter($this->config->directory))->setup($this->app, $state);
(new MockyRouter($this->config->directory))->setup($this->app, $state);
Expand Down
54 changes: 28 additions & 26 deletions src/Models/State.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,34 @@

namespace Karriere\Mocky\Models;

use Karriere\Mocky\Path;

class State
{
const STATE_PATH = __DIR__.'/../../state/';

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

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

/**
* @var array
*/
private $testRoutes = [];
private $testScope;

public static function load($scope)
{
$state = self::create('', $scope);

if (file_exists(self::STATE_PATH.$scope.'.state')) {
$state = unserialize(file_get_contents(self::STATE_PATH.$scope.'.state'));
}

return $state;
}

public static function store($state)
{
file_put_contents(self::STATE_PATH.$state->testScope.'.state', serialize($state));
}
/**
* @var string
*/
private $testScope;

private static function create($testName, $testScope)
public function __construct($statePath, $scope)
{
$instance = new self();
$instance->activeTest = $testName;
$instance->testScope = $testScope;

return $instance;
$this->statePath = $statePath;
$this->testScope = $scope;
}

public function getActiveTest()
Expand All @@ -46,7 +42,7 @@ public function setActiveTest($testName, $testScope)
$this->activeTest = $testName;
$this->testScope = $testScope;
$this->testRoutes = [];
self::store($this);
$this->store();
}

public function getIteration($route)
Expand All @@ -59,8 +55,14 @@ public function getIteration($route)

$this->testRoutes[$route] = $iteration + 1;

self::store($this);
$this->store();

return $iteration;
}

private function store()
{
$file = Path::join($this->statePath, $this->testScope.'.state');
file_put_contents($file, serialize($this));
}
}
41 changes: 41 additions & 0 deletions src/StateManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Karriere\Mocky;

use Karriere\Mocky\Models\State;

class StateManager
{
/**
* @var Configuration
*/
private $configuration;

public function __construct(Configuration $configuration)
{
$this->configuration = $configuration;

$this->initializeStateDirectory();
}

public function load($scope)
{
$statePath = $this->configuration->statePath;
$stateFile = Path::join($statePath, $scope.'.state');

if (file_exists($stateFile)) {
$state = unserialize(file_get_contents($stateFile));
} else {
$state = new State($statePath, $scope);
}

return $state;
}

private function initializeStateDirectory()
{
if (!file_exists($this->configuration->statePath)) {
mkdir($this->configuration->statePath);
}
}
}
Empty file removed state/.gitkeep
Empty file.

0 comments on commit e1264fc

Please sign in to comment.