Skip to content

Commit

Permalink
Refactoring tasks and reports with breaking changes.
Browse files Browse the repository at this point in the history
  • Loading branch information
maximkott committed Sep 14, 2016
1 parent a5f131a commit 7d6996f
Show file tree
Hide file tree
Showing 11 changed files with 46 additions and 142 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
}
},
"require": {
"weew/http": "^1.7"
"weew/http": "^1.12",
"weew/contracts": "^1.4"
},
"require-dev": {
"phpunit/phpunit": "^4.7",
Expand Down
14 changes: 2 additions & 12 deletions src/Weew/Interop/IReport.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,7 @@

use Weew\Contracts\IArrayable;
use Weew\Contracts\IUnArrayable;
use Weew\Http\IHttpResponse;
use Weew\Http\IHttpResponseable;
use Weew\Http\IUnHttpResponseable;

interface IReport extends IHttpResponseable, IArrayable, IUnArrayable {
/**
* @param IHttpResponse $response
*/
function fromHttpResponse(IHttpResponse $response);

/**
* @return IHttpResponse
*/
function toHttpResponse();
}
interface IReport extends IArrayable, IUnArrayable, IHttpResponseable, IUnHttpResponseable {}
8 changes: 2 additions & 6 deletions src/Weew/Interop/ITask.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,10 @@
use Weew\Contracts\IArrayable;
use Weew\Contracts\IUnArrayable;
use Weew\Http\IHttpRequest;
use Weew\Http\IUnHttpRequestable;
use Weew\Url\IUrl;

interface ITask extends IArrayable, IUnArrayable {
/**
* @param IHttpRequest $request
*/
function fromHttpRequest(IHttpRequest $request);

interface ITask extends IArrayable, IUnArrayable, IUnHttpRequestable {
/**
* @param IUrl $url
*
Expand Down
25 changes: 4 additions & 21 deletions src/Weew/Interop/Report.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,16 @@
namespace Weew\Interop;

use Exception;
use Weew\Contracts\IArrayable;
use Weew\Http\IHttpResponse;
use Weew\Http\IHttpResponseable;

abstract class Report implements IReport {
/**
* Report constructor.
*
* @param IHttpResponse|IHttpResponseable|IArrayable|array|null $data
*/
public function __construct($data = null) {
if ($data instanceof IHttpResponse) {
$this->fromHttpResponse($data);
} else if ($data instanceof IHttpResponseable) {
$this->fromHttpResponse($data->toHttpResponse());
} else if ($data instanceof IArrayable) {
$this->fromArray($data->toArray());
} else if (is_array($data)) {
$this->fromArray($data);
}
}

/**
* @param IHttpResponse $response
*
* @return mixed
* @throws Exception
*/
public function fromHttpResponse(IHttpResponse $response) {
public static function fromHttpResponse(IHttpResponse $response) {
throw new Exception(s(
'Method "%s::fromHttpResponse" is not implemented yet.', get_called_class()
));
Expand All @@ -49,9 +31,10 @@ public function toHttpResponse() {
/**
* @param array $array
*
* @return mixed
* @throws Exception
*/
public function fromArray(array $array) {
public static function fromArray(array $array) {
throw new Exception(s(
'Method "%s::fromArray" is not implemented yet.', get_called_class()
));
Expand Down
25 changes: 4 additions & 21 deletions src/Weew/Interop/Task.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,17 @@
namespace Weew\Interop;

use Exception;
use Weew\Contracts\IArrayable;
use Weew\Http\IHttpRequest;
use Weew\Http\IHttpRequestable;
use Weew\Url\IUrl;

abstract class Task implements ITask {
/**
* Task constructor.
*
* @param IHttpRequest|IHttpRequestable|IArrayable|array|null $data
*/
public function __construct($data = null) {
if ($data instanceof IHttpRequest) {
$this->fromHttpRequest($data);
} else if ($data instanceof IHttpRequestable) {
$this->fromHttpRequest($data->toHttpRequest());
} else if ($data instanceof IArrayable) {
$this->fromArray($data->toArray());
} else if (is_array($data)) {
$this->fromArray($data);
}
}

/**
* @param IHttpRequest $request
*
* @return mixed
* @throws Exception
*/
public function fromHttpRequest(IHttpRequest $request) {
public static function fromHttpRequest(IHttpRequest $request) {
throw new Exception(s(
'Method "%s::fromHttpRequest" is not implemented yet.', get_called_class()
));
Expand All @@ -52,9 +34,10 @@ public function toHttpRequest(IUrl $url) {
/**
* @param array $array
*
* @return mixed
* @throws Exception
*/
public function fromArray(array $array) {
public static function fromArray(array $array) {
throw new Exception(s(
'Method "%s::fromArray" is not implemented yet.', get_called_class()
));
Expand Down
27 changes: 6 additions & 21 deletions tests/Weew/Interop/ReportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
use PHPUnit_Framework_TestCase;
use Tests\Weew\Interop\Stubs\EmptyReport;
use Tests\Weew\Interop\Stubs\FakeReport;
use Tests\Weew\Interop\Stubs\FakeResponseable;
use Weew\Collections\Dictionary;
use Weew\Http\HttpResponse;

class ReportTest extends PHPUnit_Framework_TestCase {
Expand All @@ -16,26 +14,15 @@ public function test_create() {
$this->assertNull($report->getContent());
}

public function test_create_with_response() {
public function test_create_from_response() {
$response = new HttpResponse();
$response->setContent('foo');
$report = new FakeReport($response);
$report = FakeReport::fromHttpResponse($response);
$this->assertEquals('foo', $report->getContent());
}

public function test_create_with_responseable() {
$responsable = new FakeResponseable();
$report = new FakeReport($responsable);
$this->assertEquals('responsable', $report->getContent());
}

public function test_create_with_arrayable() {
$report = new FakeReport(new Dictionary(['data']));
$this->assertEquals(['data'], $report->getContent());
}

public function test_create_with_array() {
$report = new FakeReport(['data']);
public function test_create_from_array() {
$report = FakeReport::fromArray(['data']);
$this->assertEquals(['data'], $report->getContent());
}

Expand All @@ -46,9 +33,8 @@ public function test_throws_to_http_response_is_not_implemented_exception() {
}

public function test_throws_from_http_response_is_not_implemented_exception() {
$task = new EmptyReport();
$this->setExpectedException(Exception::class);
$task->fromHttpResponse(new HttpResponse());
EmptyReport::fromHttpResponse(new HttpResponse());
}

public function test_throws_to_array_is_not_implemented_exception() {
Expand All @@ -58,8 +44,7 @@ public function test_throws_to_array_is_not_implemented_exception() {
}

public function test_throws_from_array_is_not_implemented_exception() {
$task = new EmptyReport();
$this->setExpectedException(Exception::class);
$task->fromArray([]);
EmptyReport::fromArray([]);
}
}
14 changes: 10 additions & 4 deletions tests/Weew/Interop/Stubs/FakeReport.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,18 @@ public function getContent() {
return $this->content;
}

public function fromHttpResponse(IHttpResponse $response) {
$this->content = $response->getContent();
public static function fromHttpResponse(IHttpResponse $response) {
$report = new self();
$report->content = $response->getContent();

return $report;
}

public function fromArray(array $array) {
$this->content = $array;
public static function fromArray(array $array) {
$report = new self();
$report->content = $array;

return $report;
}

public function toHttpResponse() {
Expand Down
15 changes: 0 additions & 15 deletions tests/Weew/Interop/Stubs/FakeRequestable.php

This file was deleted.

15 changes: 0 additions & 15 deletions tests/Weew/Interop/Stubs/FakeResponseable.php

This file was deleted.

14 changes: 10 additions & 4 deletions tests/Weew/Interop/Stubs/FakeTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,21 @@
class FakeTask extends Task {
private $content;

public function fromHttpRequest(IHttpRequest $request) {
$this->content = $request->getContent();
public static function fromHttpRequest(IHttpRequest $request) {
$task = new self();
$task->content = $request->getContent();

return $task;
}

public function toHttpRequest(IUrl $url) {
}

public function fromArray(array $array) {
$this->content = $array;
public static function fromArray(array $array) {
$task = new self();
$task->content = $array;

return $task;
}

public function getContent() {
Expand Down
28 changes: 6 additions & 22 deletions tests/Weew/Interop/TaskTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
use Exception;
use PHPUnit_Framework_TestCase;
use Tests\Weew\Interop\Stubs\EmptyTask;
use Tests\Weew\Interop\Stubs\FakeRequestable;
use Tests\Weew\Interop\Stubs\FakeTask;
use Weew\Collections\Dictionary;
use Weew\Http\HttpRequest;
use Weew\Url\Url;

Expand All @@ -17,28 +15,16 @@ public function test_create() {
$this->assertNull($task->getContent());
}

public function test_create_with_request() {
public function test_create_from_request() {
$request = new HttpRequest();
$request->setContent('foo');

$task = new FakeTask($request);
$task = FakeTask::fromHttpRequest($request);
$this->assertEquals('foo', $task->getContent());
}

public function test_create_with_requestable() {
$requestable = new FakeRequestable();

$task = new FakeTask($requestable);
$this->assertEquals('requestable', $task->getContent());
}

public function test_create_with_arrayable() {
$task = new FakeTask(new Dictionary(['data']));
$this->assertEquals(['data'], $task->getContent());
}

public function test_create_with_array() {
$task = new FakeTask(['data']);
public function test_create_from_array() {
$task = FakeTask::fromArray(['data']);
$this->assertEquals(['data'], $task->getContent());
}

Expand All @@ -49,9 +35,8 @@ public function test_throws_to_http_request_is_not_implemented_exception() {
}

public function test_throws_from_http_request_is_not_implemented_exception() {
$task = new EmptyTask();
$this->setExpectedException(Exception::class);
$task->fromHttpRequest(new HttpRequest());
EmptyTask::fromHttpRequest(new HttpRequest());
}

public function test_throws_to_array_is_not_implemented_exception() {
Expand All @@ -61,8 +46,7 @@ public function test_throws_to_array_is_not_implemented_exception() {
}

public function test_throws_from_array_is_not_implemented_exception() {
$task = new EmptyTask();
$this->setExpectedException(Exception::class);
$task->fromArray([]);
EmptyTask::fromArray([]);
}
}

0 comments on commit 7d6996f

Please sign in to comment.