Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from d3no/master
fix bug
- Loading branch information
Showing
71 changed files
with
1,882 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,6 @@ language: php | |
dist: biotic | ||
|
||
php: | ||
- 7.1 | ||
- 7.2 | ||
- 7.3 | ||
- 7.4 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
<?php | ||
|
||
namespace Mocean\Command; | ||
|
||
use GuzzleHttp\Psr7\Request; | ||
use Mocean\Client\ClientAwareInterface; | ||
use Mocean\Client\ClientAwareTrait; | ||
use Mocean\Client\Exception\Exception; | ||
use Mocean\Command\Mc\AbstractMc; | ||
|
||
class Client implements ClientAwareInterface | ||
{ | ||
use ClientAwareTrait; | ||
|
||
private $action = McAction::AUTO; | ||
|
||
/** @var Commander */ | ||
private $commander; | ||
|
||
public function __construct() | ||
{ | ||
$this->commander = new Commander(); | ||
} | ||
|
||
/** | ||
* @param $action action from \Mocean\Command\McAction | ||
* @return self | ||
*/ | ||
public function doAction($action) | ||
{ | ||
$this->action = $action; | ||
return $this; | ||
} | ||
|
||
public function execute($params) | ||
{ | ||
if (isset($params["mocean-command"]) && !($params["mocean-command"] instanceof McBuilder)) { | ||
throw new \InvalidArgumentException('commandBuilder must implement `'.McBuilder::class); | ||
} | ||
|
||
if (isset($params["mocean-event-url"])) { | ||
$this->setEventUrl($params["mocean-event-url"]); | ||
} | ||
if (isset($params["mocean-resp-format"])) { | ||
$this->setResponseFormat($params["mocean-resp-format"]); | ||
} | ||
|
||
$command = $params["mocean-command"]->build(); | ||
|
||
if (count($command) <= 0) { | ||
throw new Exception('No command found in McBuilder.'); | ||
} | ||
|
||
$this->commander->setCommand($command); | ||
|
||
if ($this->action === McAction::AUTO || $this->action === McAction::SEND_TELEGRAM) { | ||
$uri = "/send-message"; | ||
} | ||
|
||
$this->setResponseFormat("json"); | ||
|
||
$params = $this->commander->getRequestData(); | ||
|
||
$request = new Request( | ||
'POST', | ||
$uri, | ||
['content-type' => 'application/x-www-form-urlencoded'] | ||
); | ||
|
||
$request->getBody()->write(http_build_query($params)); | ||
$response = $this->client->send($request); | ||
|
||
$response->getBody()->rewind(); | ||
$data = $response->getBody()->getContents(); | ||
if (!isset($data) || $data === '') { | ||
throw new Exception\Exception('unexpected response from API'); | ||
} | ||
|
||
return Commander::createFromResponse($data, $this->client->version); | ||
} | ||
|
||
public function setEventUrl($url) | ||
{ | ||
$this->commander->setEventUrl($url); | ||
return $this; | ||
} | ||
public function setResponseFormat($type) | ||
{ | ||
$this->commander->setResponseFormat($type); | ||
return $this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
namespace MoceanTest\Command; | ||
|
||
use Mocean\Command\Mc; | ||
use Mocean\Command\McBuilder; | ||
use MoceanTest\AbstractTesting; | ||
use Psr\Http\Message\RequestInterface; | ||
|
||
class ClientTest extends AbstractTesting | ||
{ | ||
public function testExecute() | ||
{ | ||
$inputParams = [ | ||
'mocean-event-url'=> 'https://moceanapi.com', | ||
'mocean-command' => McBuilder::create() | ||
->add( | ||
Mc::tgSendText() | ||
->setFrom("bot_username") | ||
->setTo("123456789") | ||
->setContent("Hello world") | ||
), | ||
'mocean-resp-format' => 'json' | ||
]; | ||
|
||
$mockHttp = $this->makeMockHttpClient(function (RequestInterface $request) use ($inputParams) { | ||
$this->assertEquals('POST', $request->getMethod()); | ||
$this->assertEquals($this->getTestUri('/send-message'), $request->getUri()->getPath()); | ||
$body = $this->getContentFromRequest($request); | ||
$this->assertEquals($inputParams['mocean-event-url'], $body['mocean-event-url']); | ||
$this->assertEquals( | ||
Mc::tgSendText() | ||
->setFrom("bot_username") | ||
->setTo("123456789") | ||
->setContent("Hello world") | ||
->getRequestData(), | ||
|
||
json_decode($body['mocean-command'], true)[0] | ||
); | ||
|
||
return $this->getResponse('command.xml'); | ||
}); | ||
|
||
$client = $this->makeMoceanClientWithMockHttpClient($mockHttp); | ||
|
||
$commandRes = $client->command()->execute($inputParams); | ||
$this->assertInstanceOf(\Mocean\Command\Commander::class, $commandRes); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
namespace Mocean\Command; | ||
|
||
use Mocean\Client\Exception\Exception; | ||
use Mocean\Command\Mc\AbstractMc; | ||
use Mocean\Model\ArrayAccessTrait; | ||
use Mocean\Model\AsRequest; | ||
use Mocean\Model\AsResponse; | ||
use Mocean\Model\ModelInterface; | ||
use Mocean\Model\ObjectAccessTrait; | ||
use Mocean\Model\ResponseTrait; | ||
|
||
class Commander implements ModelInterface, AsRequest, AsResponse | ||
{ | ||
use ObjectAccessTrait, ResponseTrait, ArrayAccessTrait; | ||
|
||
protected $requestData = []; | ||
|
||
public function setEventUrl($eventUrl) | ||
{ | ||
$this->requestData["mocean-event-url"] = $eventUrl; | ||
return $this; | ||
} | ||
|
||
public function setCommand($command) | ||
{ | ||
$this->requestData['mocean-command'] = json_encode($command); | ||
return $this; | ||
} | ||
|
||
public function setResponseFormat($format) | ||
{ | ||
$this->requestData['mocean-resp-format'] = $format; | ||
} | ||
|
||
public function getRequestData() | ||
{ | ||
return $this->requestData; | ||
} | ||
|
||
public static function createFromResponse($responseData, $version) | ||
{ | ||
$commander = new self(null, null); | ||
$commander->setRawResponseData($responseData) | ||
->processResponse($version); | ||
|
||
if (isset($commander['status']) && $commander['status'] !== 0 && $commander['status'] !== '0') { | ||
throw new Exception($commander['err_msg']); | ||
} | ||
|
||
return $commander; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
namespace MoceanTest\Command; | ||
|
||
use MoceanTest\AbstractTesting; | ||
use Mocean\Command\Commander; | ||
|
||
class CommanderTest extends AbstractTesting | ||
{ | ||
public function testRequestDataParams() | ||
{ | ||
$params = [ | ||
'mocean-event-url' => 'testing event url', | ||
'mocean-command' => '"testing mocean command"', | ||
'mocean-resp-format' => 'json', | ||
]; | ||
|
||
|
||
$setterReq = new Commander(); | ||
$setterReq->setEventUrl('testing event url'); | ||
$setterReq->setCommand('testing mocean command'); | ||
$setterReq->setResponseFormat('json'); | ||
|
||
$this->assertEquals($params, $setterReq->getRequestData()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
|
||
namespace Mocean\Command; | ||
|
||
use Mocean\Command\Mc\ContactType; | ||
use Mocean\Command\Mc\SendSMS; | ||
use Mocean\Command\Mc\TgSendPhoto; | ||
use Mocean\Command\Mc\TgSendText; | ||
use Mocean\Command\Mc\TgSendAudio; | ||
use Mocean\Command\Mc\TgSendAnimation; | ||
use Mocean\Command\Mc\TgSendDocument; | ||
use Mocean\Command\Mc\TgSendVideo; | ||
use Mocean\Command\Mc\TgRequestContact; | ||
|
||
|
||
class Mc | ||
{ | ||
public static function tgSendText() | ||
{ | ||
return new TgSendText(); | ||
} | ||
|
||
public static function tgSendAudio() | ||
{ | ||
return new TgSendAudio(); | ||
} | ||
|
||
public static function tgSendAnimation() | ||
{ | ||
return new TgSendAnimation(); | ||
} | ||
|
||
public static function tgSendDocument() | ||
{ | ||
return new TgSendDocument(); | ||
} | ||
|
||
public static function tgSendVideo() | ||
{ | ||
return new TgSendVideo(); | ||
} | ||
|
||
public static function tgSendPhoto() | ||
{ | ||
return new TgSendPhoto(); | ||
} | ||
|
||
public static function tgRequestContact() | ||
{ | ||
return new TgRequestContact(); | ||
} | ||
|
||
public static function sendSMS() | ||
{ | ||
return new SendSMS(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
namespace Mocean\Command\Mc; | ||
|
||
use Mocean\Model\AsRequest; | ||
use Mocean\Client\Exception\Exception; | ||
|
||
abstract class AbstractMc implements AsRequest | ||
{ | ||
protected $requestData; | ||
|
||
abstract public function action(); | ||
|
||
abstract protected function requiredKey(); | ||
|
||
public function getRequestData() | ||
{ | ||
foreach ($this->requiredKey() as $param) { | ||
if (!isset($this->requestData[$param])) { | ||
throw new \InvalidArgumentException('missing expected key `'.$param.'` from '.static::class); | ||
} | ||
} | ||
|
||
return array_merge($this->requestData, [ | ||
'action' => $this->action(), | ||
]); | ||
} | ||
} |
Oops, something went wrong.