Skip to content

Commit

Permalink
Add first tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ssitdikov committed Jan 1, 2018
1 parent 52986ad commit 67eb4bd
Show file tree
Hide file tree
Showing 7 changed files with 315 additions and 6 deletions.
17 changes: 17 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
language: php

php:
- '7.0'
- '7.1'
- '7.2'

install: composer install

script:
- mkdir build/logs -p
- phpunit

after_success:
- travis_retry php vendor/bin/coveralls -v
- bash <(curl -s https://codecov.io/bash)
- php vendor/bin/codacycoverage clover build/logs/clover.xml
47 changes: 47 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
backupGlobals="false"
backupStaticAttributes="false"
beStrictAboutChangesToGlobalState="true"
beStrictAboutOutputDuringTests="true"
beStrictAboutTestsThatDoNotTestAnything="true"
bootstrap="vendor/autoload.php"
colors="true"
columns="max"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnError="false"
stopOnFailure="false"
stopOnRisky="false"
verbose="true"
>
<php>
<ini name="error_reporting" value="E_ALL" />
</php>

<testsuites>
<testsuite name="PHP Coveralls component test suite">
<directory>./tests/</directory>
</testsuite>
</testsuites>

<filter>
<whitelist>
<directory>./src</directory>
<exclude>
<directory>./build</directory>
<directory>./tests</directory>
<directory>./vendor</directory>
</exclude>
</whitelist>
</filter>

<logging>
<log type="coverage-clover" target="build/logs/clover.xml"/>
</logging>
</phpunit>
10 changes: 5 additions & 5 deletions src/Client/TelegraphClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,6 @@ public function __construct(Client $client)
$this->client = $client;
}

public function createAccount(CreateAccountRequest $createAccountRequest): Account
{
return $this->doRequest($createAccountRequest);
}

private function doRequest(RequestInterface $request)
{
$response = $this->client->request(
Expand All @@ -43,6 +38,11 @@ private function doRequest(RequestInterface $request)
return $request->handleResponse($response);
}

public function createAccount(CreateAccountRequest $createAccountRequest): Account
{
return $this->doRequest($createAccountRequest);
}

public function editAccountInfo(EditAccountInfoRequest $editAccountRequest): Account
{
return $this->doRequest($editAccountRequest);
Expand Down
1 change: 0 additions & 1 deletion src/Request/CreateAccountRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ public function handleResponse(ResponseInterface $response): Account
switch ($json->error) {
case ('SHORT_NAME_REQUIRED'):
throw new ShortNameRequiredException();
break;
}
throw new \Exception($json->error);
}
Expand Down
40 changes: 40 additions & 0 deletions tests/Account/CreateAccountRequestTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace SSitdikov\TelegraphAPI\Tests\Account;

use SSitdikov\TelegraphAPI\Request\CreateAccountRequest;
use PHPUnit\Framework\TestCase;
use SSitdikov\TelegraphAPI\Request\RequestInterface;
use SSitdikov\TelegraphAPI\Type\Account;

class CreateAccountRequestTest extends TestCase
{

/**
* @test
*/
public function simpleCreateAccount()
{
$account = new Account();
$shortName = md5(random_bytes(16));
$authorName = md5(random_bytes(16));
$authorUrl = md5(random_bytes(16));
$account->setShortName($shortName);
$account->setAuthorName($authorName);
$account->setAuthorUrl($authorUrl);
$request = new CreateAccountRequest($account);

$this->assertEquals(RequestInterface::POST, $request->getMethod());
$this->assertEquals('createAccount', $request->getUrlRequest());
$expectedParams = [
'json' => [
'short_name' => $shortName,
'author_name' => $authorName,
'author_url' => $authorUrl,
]
];
$this->assertEquals($expectedParams, $request->getParams());
}


}
37 changes: 37 additions & 0 deletions tests/Account/GetPageRequestTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace SSitdikov\TelegraphAPI\Tests\Account;

use SSitdikov\TelegraphAPI\Request\GetAccountInfoRequest;
use PHPUnit\Framework\TestCase;
use SSitdikov\TelegraphAPI\Request\RequestInterface;
use SSitdikov\TelegraphAPI\Type\Account;

class GetPageRequestTest extends TestCase
{

/**
* @test
*/
public function simpleGetAccountInfo()
{
$account = new Account();
$accessToken = md5(random_bytes(16));
$account->setAccessToken($accessToken);
$request = new GetAccountInfoRequest($account);
$request->setAccountFields(['short_name', 'author_name', 'author_url', 'auth_url', 'page_count']);

$this->assertEquals(RequestInterface::POST, $request->getMethod());
$this->assertEquals('getAccountInfo', $request->getUrlRequest());
$expectedParams = [
'json' => [
'access_token' => $accessToken,
'fields' => [
'short_name', 'author_name', 'author_url', 'auth_url', 'page_count'
]
]
];
$this->assertEquals($expectedParams, $request->getParams());
}

}
169 changes: 169 additions & 0 deletions tests/Client/TelegraphClientTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
<?php
/**
* User: Salavat Sitdikov
*/

namespace SSitdikov\TelegraphAPI\Tests\Client;

use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Response;
use PHPUnit\Framework\MockObject\MockObject;
use SSitdikov\TelegraphAPI\Client\TelegraphClient;
use PHPUnit\Framework\TestCase;
use SSitdikov\TelegraphAPI\Exception\ShortNameRequiredException;
use SSitdikov\TelegraphAPI\Request\CreateAccountRequest;
use SSitdikov\TelegraphAPI\Request\GetAccountInfoRequest;
use SSitdikov\TelegraphAPI\Type\Account;

class TelegraphClientTest extends TestCase
{

/**
* @var Account
*/
private $account;
/**
* @var MockObject
*/
private $client;

public function setUp()
{
$shortName = md5(time());
$this->account = new Account();
$this->account->setShortName($shortName);

$this->client = $this->getMockBuilder(Client::class)->getMock();

}

/**
* @test
*/
public function createAccount()
{

$accessToken = md5(random_bytes(16));
$authUrl = md5(random_bytes(16));

$responseContent = [
'ok' => true,
'result' => [
'short_name' => $this->account->getShortName(),
'author_name' => '',
'author_url' => '',
'auth_url' => $authUrl,
'access_token' => $accessToken,
]
];
$response = new Response(200, [], json_encode($responseContent));
$this->client->expects($this->once())->method('request')->willReturn($response);

$telegraph = new TelegraphClient($this->client);
$account = $telegraph->createAccount(
new CreateAccountRequest($this->account)
);

$this->assertEquals($account->getAccessToken(), $accessToken);
$this->assertEquals($account->getAuthUrl(), $authUrl);
}

/**
* @test
* @depends createAccount
*/
public function createAccountError()
{
$responseContent = [
'ok' => false,
'error' => 'SomeStringError'
];
$response = new Response(200, [], json_encode($responseContent));
$this->client->expects($this->once())->method('request')->willReturn($response);

$telegraph = new TelegraphClient($this->client);

$this->expectException(\Exception::class);
$telegraph->createAccount(
new CreateAccountRequest($this->account)
);
}

/**
* @test
* @depends createAccountError
*/
public function createAccountErrorNotRequiredFields()
{
$responseContent = [
'ok' => false,
'error' => 'SHORT_NAME_REQUIRED'
];

$response = new Response(200, [], json_encode($responseContent));
$this->client->expects($this->once())->method('request')->willReturn($response);

$telegraph = new TelegraphClient($this->client);

$this->expectException(ShortNameRequiredException::class);
$telegraph->createAccount(
new CreateAccountRequest($this->account)
);
}

/**
* @test
*/
public function getAccountInfo()
{
$authUrl = md5(random_bytes(16));
$accessToken = md5($authUrl . random_bytes(16));
$pageCount = random_int(100, 9999);
$responseContent = [
'ok' => true,
'result' => [
'short_name' => $this->account->getShortName(),
'author_name' => '',
'author_url' => '',
'auth_url' => $authUrl,
'access_token' => $accessToken,
'page_count' => $pageCount,
]
];

$response = new Response(200, [], json_encode($responseContent));
$this->client->expects($this->once())->method('request')->willReturn($response);

$telegraph = new TelegraphClient($this->client);

$account = $telegraph->getAccountInfo(
new GetAccountInfoRequest($this->account)
);

$this->assertEquals($pageCount, $account->getPageCount());
}

/**
* @test
* @depends getAccountInfo
*/
public function getAccountInfoError()
{
$errorString = md5(random_bytes(16));
$responseContent = [
'ok' => false,
'error' => $errorString
];

$response = new Response(200, [], json_encode($responseContent));
$this->client->expects($this->once())->method('request')->willReturn($response);

$telegraph = new TelegraphClient($this->client);

$this->expectException(\Exception::class);
$telegraph->getAccountInfo(
new GetAccountInfoRequest($this->account)
);
}

}

0 comments on commit 67eb4bd

Please sign in to comment.