Skip to content

Commit

Permalink
Add new tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ssitdikov committed Jan 2, 2018
1 parent 67eb4bd commit 4a54342
Show file tree
Hide file tree
Showing 2 changed files with 206 additions and 0 deletions.
136 changes: 136 additions & 0 deletions tests/Page/ContentTypesTest.php
@@ -0,0 +1,136 @@
<?php
/**
* User: Salavat Sitdikov
*/

namespace SSitdikov\TelegraphAPI\Tests\Page;

use PHPUnit\Framework\TestCase;
use SSitdikov\TelegraphAPI\Type\ContentType\ImageType;
use SSitdikov\TelegraphAPI\Type\ContentType\LinkType;
use SSitdikov\TelegraphAPI\Type\ContentType\ParagraphType;
use SSitdikov\TelegraphAPI\Type\ContentType\YoutubeType;

class ContentTypesTest extends TestCase
{

/**
* @test
*/
public function linkType()
{
$link = new LinkType();
$link->setText('Test link');
$link->setHref('https://github.com');

$this->assertJson(json_encode([
'tag' => 'a',
'attrs' => ['href' => 'https://github.com'],
'children' => ['Test link']
]), json_encode($link));
}

/**
* @test
*/
public function imageType()
{
$image = new ImageType();
$image->setSrc('https://github.com/logo.png');

$this->assertJson(json_encode([
'tag' => 'img',
'attrs' => [
'src' => 'https://github.com/logo.png',
]
]), json_encode($image));

$image->setCaption('Test caption');

$this->assertJson(json_encode([
'tag' => 'figure',
'children' => [
[
'tag' => 'img',
'attrs' => ['src' => 'https://github.com/logo.png'],
],
[
'tag' => 'figcaption',
'children' => [
'Test caption'
]
]
]
]), json_encode($image));
}

/**
* @test
*/
public function paragraphType()
{
$paragraph = new ParagraphType();
$paragraph->setText('Test');

$this->assertJson(
json_encode([
'tag' => 'p',
'children' => [
'Test'
]
]),
json_encode($paragraph)
);

$paragraph->addContentElement(
$paragraph
);

$this->assertJson(
json_encode([
'tag' => 'p',
'children' => [
'Test',
[
'tag' => 'p',
'children' => [
'Test'
]
]
]
]),
json_encode($paragraph)
);
}

/**
* @test
*/
public function youtubeType()
{
$youtube = new YoutubeType();
$youtube->setSrc('test');
$youtube->setCaption('Caption');

$this->assertJson(
json_encode([
'tag' => 'figure',
'children' => [
[
'tag' => 'iframe',
'attrs' => [
'src' => 'test',
]
],
[
'tag' => 'figcaption',
'children' => [
'Caption'
]
]
]
]),
json_encode($youtube)
);
}
}
70 changes: 70 additions & 0 deletions tests/Page/CreatePageRequestTest.php
@@ -0,0 +1,70 @@
<?php
/**
* User: Salavat Sitdikov
*/

namespace SSitdikov\TelegraphAPI\Tests\Page;

use SSitdikov\TelegraphAPI\Request\CreatePageRequest;
use PHPUnit\Framework\TestCase;
use SSitdikov\TelegraphAPI\Request\RequestInterface;
use SSitdikov\TelegraphAPI\Type\Account;
use SSitdikov\TelegraphAPI\Type\ContentType\LinkType;
use SSitdikov\TelegraphAPI\Type\Page;

class CreatePageRequestTest extends TestCase
{

/**
* @test
*/
public function simpleCreatePageRequest()
{
$title = md5(random_bytes(16));
$accessToken = md5(random_bytes(16));
$content = [];

$page = new Page();
$page->setTitle($title);
$page->setContent($content);

$account = new Account();
$account->setAccessToken($accessToken);

$request = new CreatePageRequest($page, $account);

$this->assertEquals(RequestInterface::POST, $request->getMethod());
$this->assertEquals('createPage', $request->getUrlRequest());

$expectedParams = [
'json' => [
'access_token' => $accessToken,
'title' => $title,
'content' => $content,
]
];
$this->assertEquals( $expectedParams, $request->getParams());
$authorUrl = md5(random_bytes(16));
$authorName = md5(random_bytes(16));

$page->setAuthorUrl($authorUrl);
$page->setAuthorName($authorName);

$request = new CreatePageRequest($page, $account);

$request->isReturnContent();
$expectedParams = [
'json' => [
'access_token' => $accessToken,
'title' => $title,
'content' => $content,
'author_name' => $authorName,
'author_url' => $authorUrl,
'return_content' => true
]
];

$this->assertEquals($expectedParams, $request->getParams());
}

}

0 comments on commit 4a54342

Please sign in to comment.