Skip to content

Commit

Permalink
Add constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
demyan112rv committed Jul 6, 2023
1 parent 2d33411 commit 5758e97
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 67 deletions.
13 changes: 4 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ $response->setConfig([
'headers' => ['Content-Type' => 'application/json'],
'body' => ['foo' => 'bar']
])->addBehavior(
(new Behavior())
->setType(Behavior::TYPE_WAIT)
(new Behavior(Behavior::TYPE_WAIT))
->setConfig((new Behavior\Config\Wait())->setValue(500))
);
```
Expand Down Expand Up @@ -78,15 +77,11 @@ $stub->addResponse($response)->addPredicate($predicate);
use Demyan112rv\MountebankPHP\Imposter;
use Demyan112rv\MountebankPHP\Mountebank;

$imposter = new Imposter();
$imposter->setName('Test imposter')
->setPort(1234)
->setProtocol(Imposter::PROTOCOL_HTTP)
->addStub($stub);
$imposter = new Imposter('Test imposter', 1234, Imposter::PROTOCOL_HTTP);
$imposter->addStub($stub);

// Mountbank config client
$mb = new Mountebank(new \GuzzleHttp\Client());
$mb->setHost('http://localhost')->setPort(2525);
$mb = new Mountebank(new \GuzzleHttp\Client(), 'http://localhost', 2525);

// Add new imposter
$response = $mb->addImposter($imposter);
Expand Down
6 changes: 2 additions & 4 deletions examples/create.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,8 @@
])
);

$imposter = new Imposter();
$imposter->setName('Test imposter')
->setPort(4444)
->setProtocol(Imposter::PROTOCOL_HTTP)
$imposter = new Imposter('Test imposter', 4444, Imposter::PROTOCOL_HTTP);
$imposter
->addStub($stub);

$mb = new Mountebank(new \GuzzleHttp\Client(), 'http://localhost', 2525);
Expand Down
27 changes: 22 additions & 5 deletions src/Imposter.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
*/
class Imposter
{
const PROTOCOL_HTTP = 'http';
const PROTOCOL_HTTPS = 'https';
const PROTOCOL_TCP = 'tcp';
const PROTOCOL_SMTP = 'smtp';
const PROTOCOL_GRAPHQL = 'graphql';
public const PROTOCOL_HTTP = 'http';
public const PROTOCOL_HTTPS = 'https';
public const PROTOCOL_TCP = 'tcp';
public const PROTOCOL_SMTP = 'smtp';
public const PROTOCOL_GRAPHQL = 'graphql';

/**
* The port to run the imposter on.
Expand Down Expand Up @@ -77,6 +77,14 @@ class Imposter
*/
private ?string $schema = null;

public function __construct(string $name, int $port, string $protocol)
{
$this->name = $name;
$this->port = $port;
$this->setProtocol($protocol);
}


/**
* @return string[]
*/
Expand All @@ -96,6 +104,9 @@ public function getPort(): int
return $this->port;
}

/**
* @deprecated
*/
public function setPort(int $port): Imposter
{
$this->port = $port;
Expand All @@ -107,6 +118,9 @@ public function getProtocol(): string
return $this->protocol;
}

/**
* @deprecated
*/
public function setProtocol(string $protocol): Imposter
{
if (!\in_array($protocol, static::getProtocols())) {
Expand All @@ -121,6 +135,9 @@ public function getName(): string
return $this->name;
}

/**
* @deprecated
*/
public function setName(string $name): Imposter
{
$this->name = $name;
Expand Down
6 changes: 3 additions & 3 deletions src/Mountebank.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
*/
class Mountebank
{
const URI_IMPOSTERS = 'imposters';
const URI_CONFIG = 'config';
const URI_LOGS = 'logs';
public const URI_IMPOSTERS = 'imposters';
public const URI_CONFIG = 'config';
public const URI_LOGS = 'logs';

private ClientInterface $client;

Expand Down
25 changes: 14 additions & 11 deletions src/Predicate.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@
*/
class Predicate
{
const OPERATOR_EQUALS = 'equals';
const OPERATOR_DEEP_EQUALS = 'deepEquals';
const OPERATOR_CONTAINS = 'contains';
const OPERATOR_START_WITH = 'startsWith';
const OPERATOR_END_WITH = 'endsWith';
const OPERATOR_MATCHES = 'matches';
const OPERATOR_EXISTS = 'exists';
const OPERATOR_NOT = 'not';
const OPERATOR_OR = 'or';
const OPERATOR_AND = 'and';
const OPERATOR_INJECT = 'inject';
public const OPERATOR_EQUALS = 'equals';
public const OPERATOR_DEEP_EQUALS = 'deepEquals';
public const OPERATOR_CONTAINS = 'contains';
public const OPERATOR_START_WITH = 'startsWith';
public const OPERATOR_END_WITH = 'endsWith';
public const OPERATOR_MATCHES = 'matches';
public const OPERATOR_EXISTS = 'exists';
public const OPERATOR_NOT = 'not';
public const OPERATOR_OR = 'or';
public const OPERATOR_AND = 'and';
public const OPERATOR_INJECT = 'inject';

private string $operator;

Expand Down Expand Up @@ -89,6 +89,9 @@ public function getOperator(): string
return $this->operator;
}

/**
* @deprecated
*/
public function setOperator(string $operator): self
{
if (!\in_array($operator, static::getOperators())) {
Expand Down
6 changes: 3 additions & 3 deletions src/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
*/
class Response
{
const TYPE_IS = 'is';
const TYPE_PROXY = 'proxy';
const TYPE_INJECT = 'inject';
public const TYPE_IS = 'is';
public const TYPE_PROXY = 'proxy';
public const TYPE_INJECT = 'inject';

/**
* @var string
Expand Down
20 changes: 10 additions & 10 deletions src/Response/Behavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
*/
class Behavior
{
const TYPE_WAIT = 'wait';
const TYPE_REPEAT = 'repeat';
const TYPE_COPY = 'copy';
const TYPE_LOOKUP = 'lookup';
const TYPE_DECORATE = 'decorate';
const TYPE_SHELL_TRANSFORM = 'shellTransform';
public const TYPE_WAIT = 'wait';
public const TYPE_REPEAT = 'repeat';
public const TYPE_COPY = 'copy';
public const TYPE_LOOKUP = 'lookup';
public const TYPE_DECORATE = 'decorate';
public const TYPE_SHELL_TRANSFORM = 'shellTransform';

private string $type;

Expand Down Expand Up @@ -50,12 +50,12 @@ public function getType(): string
return $this->type;
}

/**
* @deprecated
*/
public function setType(string $type): Behavior
{
if (!\in_array($type, static::getTypes())) {
throw new \InvalidArgumentException();
}
$this->type = $type;

return $this;
}

Expand Down
21 changes: 6 additions & 15 deletions tests/Unit/FormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,8 @@ public function testHttps(): void
$stub = new Stub();
$stub->addResponse($response)->addPredicate($predicate);

$imposter = new Imposter();
$imposter->setName('Test imposter')
->setPort(1234)
->setProtocol(Imposter::PROTOCOL_HTTPS)
->addStub($stub)
$imposter = new Imposter('Test imposter', 1234, Imposter::PROTOCOL_HTTPS);
$imposter->addStub($stub)
->setKey('key')
->setCert('cert')
->setMutualAuth(true)
Expand Down Expand Up @@ -123,11 +120,8 @@ public function testHttp(): void
$stub = new Stub();
$stub->addResponse($response)->addPredicate($predicate);

$imposter = new Imposter();
$imposter->setName('Test imposter')
->setPort(1234)
->setProtocol(Imposter::PROTOCOL_HTTP)
->addStub($stub)
$imposter = new Imposter('Test imposter', 1234, Imposter::PROTOCOL_HTTP);
$imposter->addStub($stub)
->setDefaultResponse(new Response(Response::TYPE_IS))
->setAllowCORS(true)
->setRecordRequests(true);
Expand All @@ -146,11 +140,8 @@ public function testHttp(): void

public function testSchemaAttached(): void
{
$imposter = new Imposter();
$imposter->setName('Test imposter')
->setPort(1234)
->setProtocol(Imposter::PROTOCOL_HTTP)
->setSchema('Test Schema');
$imposter = new Imposter('Test imposter', 1234, Imposter::PROTOCOL_HTTP);
$imposter->setSchema('Test Schema');

$formatter = new Formatter($imposter);
$array = $formatter->toArray();
Expand Down
10 changes: 3 additions & 7 deletions tests/Unit/ImposterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,8 @@ class ImposterTest extends TestCase
{
public function testFill(): void
{
$imposter = new Imposter();
$imposter->setName('Test imposter')
->setPort(1234)
->setProtocol(Imposter::PROTOCOL_HTTP)
->setSchema('Test schema')
$imposter = new Imposter('Test imposter', 1234, Imposter::PROTOCOL_HTTP);
$imposter->setSchema('Test schema')
->setStubs([new Stub()])
->setKey('key')
->setCert('cert')
Expand Down Expand Up @@ -47,7 +44,6 @@ public function testFill(): void
public function testWrongType(): void
{
$this->expectException(\InvalidArgumentException::class);
$imposter = new Imposter();
$imposter->setProtocol('Wrong protocol');
new Imposter('Test imposter', 1234, 'Wrong protocol');
}
}

0 comments on commit 5758e97

Please sign in to comment.