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

Commit e1264fc

Browse files
authored
Merge pull request #6 from karriereat/feature/remove-template-data
remove demo data and add StateManager
2 parents ff8e938 + 54a4515 commit e1264fc

File tree

11 files changed

+77
-142
lines changed

11 files changed

+77
-142
lines changed

config/mocky.php

Lines changed: 0 additions & 5 deletions
This file was deleted.

example/mocks/users/list-users.json

Lines changed: 0 additions & 68 deletions
This file was deleted.

example/mocks/users/single-user.json

Lines changed: 0 additions & 15 deletions
This file was deleted.

example/tests/user-simple.json

Lines changed: 0 additions & 12 deletions
This file was deleted.

public/.htaccess

Lines changed: 0 additions & 3 deletions
This file was deleted.

public/index.php

Lines changed: 0 additions & 9 deletions
This file was deleted.

readme.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,15 @@
1010

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

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

19+
or add mocky as a dependency to your project.
1520
```
16-
composer create-project karriere/mocky /destination/path
21+
composer require karriere/mocky
1722
```
1823

1924
## Configuration

src/Mocky.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Karriere\Mocky;
44

5-
use Karriere\Mocky\Models\State;
65
use Karriere\Mocky\Router\MockyRouter;
76
use Karriere\Mocky\Router\TestRouter;
87
use Slim\App;
@@ -38,7 +37,7 @@ public function __construct(Configuration $config)
3837
$scope = reset($scope);
3938
}
4039

41-
$state = State::load($scope);
40+
$state = (new StateManager($config))->load($scope);
4241

4342
(new TestRouter($this->config->directory))->setup($this->app, $state);
4443
(new MockyRouter($this->config->directory))->setup($this->app, $state);

src/Models/State.php

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,34 @@
22

33
namespace Karriere\Mocky\Models;
44

5+
use Karriere\Mocky\Path;
6+
57
class State
68
{
7-
const STATE_PATH = __DIR__.'/../../state/';
8-
9+
/**
10+
* @var string
11+
*/
12+
private $statePath;
13+
14+
/**
15+
* @var string
16+
*/
917
private $activeTest;
1018

19+
/**
20+
* @var array
21+
*/
1122
private $testRoutes = [];
12-
private $testScope;
13-
14-
public static function load($scope)
15-
{
16-
$state = self::create('', $scope);
17-
18-
if (file_exists(self::STATE_PATH.$scope.'.state')) {
19-
$state = unserialize(file_get_contents(self::STATE_PATH.$scope.'.state'));
20-
}
21-
22-
return $state;
23-
}
2423

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

30-
private static function create($testName, $testScope)
29+
public function __construct($statePath, $scope)
3130
{
32-
$instance = new self();
33-
$instance->activeTest = $testName;
34-
$instance->testScope = $testScope;
35-
36-
return $instance;
31+
$this->statePath = $statePath;
32+
$this->testScope = $scope;
3733
}
3834

3935
public function getActiveTest()
@@ -46,7 +42,7 @@ public function setActiveTest($testName, $testScope)
4642
$this->activeTest = $testName;
4743
$this->testScope = $testScope;
4844
$this->testRoutes = [];
49-
self::store($this);
45+
$this->store();
5046
}
5147

5248
public function getIteration($route)
@@ -59,8 +55,14 @@ public function getIteration($route)
5955

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

62-
self::store($this);
58+
$this->store();
6359

6460
return $iteration;
6561
}
62+
63+
private function store()
64+
{
65+
$file = Path::join($this->statePath, $this->testScope.'.state');
66+
file_put_contents($file, serialize($this));
67+
}
6668
}

src/StateManager.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace Karriere\Mocky;
4+
5+
use Karriere\Mocky\Models\State;
6+
7+
class StateManager
8+
{
9+
/**
10+
* @var Configuration
11+
*/
12+
private $configuration;
13+
14+
public function __construct(Configuration $configuration)
15+
{
16+
$this->configuration = $configuration;
17+
18+
$this->initializeStateDirectory();
19+
}
20+
21+
public function load($scope)
22+
{
23+
$statePath = $this->configuration->statePath;
24+
$stateFile = Path::join($statePath, $scope.'.state');
25+
26+
if (file_exists($stateFile)) {
27+
$state = unserialize(file_get_contents($stateFile));
28+
} else {
29+
$state = new State($statePath, $scope);
30+
}
31+
32+
return $state;
33+
}
34+
35+
private function initializeStateDirectory()
36+
{
37+
if (!file_exists($this->configuration->statePath)) {
38+
mkdir($this->configuration->statePath);
39+
}
40+
}
41+
}

0 commit comments

Comments
 (0)