pm-convert/tests/AbstractRequestTest.php
Anthony Axenov 3c1871ce1f
Conversion to 2.0 (--v2.0), improvements and fixes (#10)
First of all, now you can convert a collection manually exported from Postman UI.

Until this commit, any collection json had to be inside of root 'collection' object. Postman API returns collections in a such way and that was my case. So any collection exported using UI was mistakenly not detected as correct one.

The second thing that it is now possible to convert collections from v2.1 to v2.0 using --v2.0 flag. Even if they are exported via Postman API, of course.

Also some important refactorings are here.
2023-09-17 23:09:19 +08:00

298 lines
9.2 KiB
PHP

<?php
declare(strict_types=1);
use PHPUnit\Framework\TestCase;
use PmConverter\Exceptions\{
EmptyHttpVerbException,
InvalidHttpVersionException};
class AbstractRequestTest extends TestCase
{
/**
* @covers PmConverter\Converters\Abstract\AbstractRequest
* @covers PmConverter\HttpVersion
* @return void
* @throws InvalidHttpVersionException
*/
public function testHttpVersion(): void
{
$request = new \PmConverter\Converters\Http\HttpRequest();
$request->setHttpVersion(2.0);
$this->assertSame(2.0, $request->getHttpVersion());
}
/**
* @covers PmConverter\Converters\Abstract\AbstractRequest
* @covers PmConverter\Converters\Abstract\AbstractRequest::getVerb()
* @covers PmConverter\HttpVersion
* @return void
* @throws InvalidHttpVersionException
*/
public function testInvalidHttpVersionException(): void
{
$this->expectException(InvalidHttpVersionException::class);
$request = new \PmConverter\Converters\Http\HttpRequest();
$request->setHttpVersion(5);
}
/**
* @covers PmConverter\Converters\Abstract\AbstractRequest
* @covers PmConverter\Converters\Abstract\AbstractRequest::setVerb()
* @covers PmConverter\Converters\Abstract\AbstractRequest::getVerb()
* @return void
* @throws EmptyHttpVerbException
*/
public function testVerb(): void
{
$request = new \PmConverter\Converters\Http\HttpRequest();
$request->setVerb('GET');
$this->assertSame('GET', $request->getVerb());
}
/**
* @covers PmConverter\Converters\Abstract\AbstractRequest
* @covers PmConverter\Converters\Abstract\AbstractRequest::getVerb()
* @return void
* @throws EmptyHttpVerbException
*/
public function testEmptyHttpVerbException(): void
{
$this->expectException(EmptyHttpVerbException::class);
$request = new \PmConverter\Converters\Http\HttpRequest();
$request->getVerb();
}
/**
* @covers PmConverter\Converters\Abstract\AbstractRequest
* @covers PmConverter\Converters\Abstract\AbstractRequest::setUrl()
* @covers PmConverter\Converters\Abstract\AbstractRequest::getUrl()
* @return void
*/
public function testUrl(): void
{
$request = new \PmConverter\Converters\Http\HttpRequest();
$request->setUrl('http://localhost');
$this->assertSame('http://localhost', $request->getUrl());
}
/**
* @covers PmConverter\Converters\Abstract\AbstractRequest
* @covers PmConverter\Converters\Abstract\AbstractRequest::setName()
* @covers PmConverter\Converters\Abstract\AbstractRequest::getName()
* @return void
*/
public function testName(): void
{
$request = new \PmConverter\Converters\Http\HttpRequest();
$request->setName('lorem ipsum');
$this->assertSame('lorem ipsum', $request->getName());
}
/**
* @covers PmConverter\Converters\Abstract\AbstractRequest
* @covers PmConverter\Converters\Abstract\AbstractRequest::setDescription()
* @covers PmConverter\Converters\Abstract\AbstractRequest::getDescription()
* @return void
*/
public function testDescription(): void
{
$request = new \PmConverter\Converters\Http\HttpRequest();
$request->setDescription("lorem ipsum\ndolor sit\namet");
$this->assertSame("lorem ipsum\ndolor sit\namet", $request->getDescription());
}
/**
* @covers PmConverter\Converters\Abstract\AbstractRequest
* @covers PmConverter\Converters\Abstract\AbstractRequest::setBodymode()
* @covers PmConverter\Converters\Abstract\AbstractRequest::getBodymode()
* @return void
*/
public function testBodyMode(): void
{
$request = new \PmConverter\Converters\Http\HttpRequest();
$request->setBodymode('raw');
$this->assertSame('raw', $request->getBodymode());
}
/**
* @covers PmConverter\Converters\Abstract\AbstractRequest
* @covers PmConverter\Converters\Abstract\AbstractRequest::setHeaders()
* @covers PmConverter\Converters\Abstract\AbstractRequest::setHeader()
* @covers PmConverter\Converters\Abstract\AbstractRequest::getHeaders()
* @return void
*/
public function testHeaders(): void
{
$headers = [
(object)[
'key' => 'Header 1',
'value' => 'Value 1',
'disabled' => true,
],
(object)[
'key' => 'Header 2',
'value' => 'Value 2',
'disabled' => false,
],
(object)[
'key' => 'Header 3',
'value' => 'Value 3',
],
];
$expected = [
'Header 1' => [
'value' => 'Value 1',
'disabled' => true,
],
'Header 2' => [
'value' => 'Value 2',
'disabled' => false,
],
'Header 3' => [
'value' => 'Value 3',
'disabled' => false,
],
'Header 4' => [
'value' => 'Value 4',
'disabled' => false,
],
'Header 5' => [
'value' => 'Value 5',
'disabled' => true,
],
];
$request = new \PmConverter\Converters\Http\HttpRequest();
$request->setHeaders($headers)
->setHeader('Header 4', 'Value 4')
->setHeader('Header 5', 'Value 5', true);
$this->assertSame($expected, $request->getHeaders());
}
/**
* @covers PmConverter\Converters\Abstract\AbstractRequest
* @covers PmConverter\Converters\Abstract\AbstractRequest::setAuth()
* @return void
*/
public function testAuthBearer(): void
{
$auth = (object)[
'type' => 'bearer',
'bearer' => [
(object)[
'key' => 'token',
'value' => 'qwerty',
'type' => 'string',
]
]
];
$expected = [
'Authorization' => [
'value' => 'Bearer qwerty',
'disabled' => false,
],
];
$request = new \PmConverter\Converters\Http\HttpRequest();
$request->setAuth($auth);
$this->assertSame($expected, $request->getHeaders());
}
/**
* @covers PmConverter\Converters\Abstract\AbstractRequest
* @covers PmConverter\Converters\Abstract\AbstractRequest::setBodymode()
* @covers PmConverter\Converters\Abstract\AbstractRequest::setHeader()
* @covers PmConverter\Converters\Abstract\AbstractRequest::setBody()
* @covers PmConverter\Converters\Abstract\AbstractRequest::setBodyAsIs()
* @covers PmConverter\Converters\Abstract\AbstractRequest::getBody()
* @return void
*/
public function testJson(): void
{
$body = (object)[
'mode' => 'raw',
'raw' => $expectedBody = '["lorem ipsum dolor sit amet"]',
'options' => (object)[
'raw' => (object)[
'language' => 'json',
]
]
];
$request = new \PmConverter\Converters\Http\HttpRequest();
$request->setBody($body);
$expectedHeaders = [
'Content-Type' => [
'value' => 'application/json',
'disabled' => false,
],
];
$this->assertSame($expectedHeaders, $request->getHeaders());
$this->assertSame($expectedBody, $request->getBody());
}
/**
* @covers PmConverter\Converters\Abstract\AbstractRequest
* @covers PmConverter\Converters\Abstract\AbstractRequest::setBodymode
* @covers PmConverter\Converters\Abstract\AbstractRequest::setHeader
* @covers PmConverter\Converters\Abstract\AbstractRequest::setBody
* @covers PmConverter\Converters\Abstract\AbstractRequest::setFormdataBody
* @covers PmConverter\Converters\Abstract\AbstractRequest::getBody
* @return void
*/
public function testFormdata(): void
{
$body = (object)[
'mode' => 'formdata',
'formdata' => [
(object)[
'key' => 'param1',
'value' => 'value1',
'type' => 'text',
],
(object)[
'key' => 'param2',
'src' => '/tmp/somefile.txt',
'type' => 'file',
],
],
];
$expectedBody = [
'param1' => [
'value' => 'value1',
'type' => 'text',
],
'param2' => [
'value' => '/tmp/somefile.txt',
'type' => 'file',
],
];
$expectedHeaders = [
'Content-Type' => [
'value' => 'multipart/form-data',
'disabled' => false,
],
];
$request = new \PmConverter\Converters\Http\HttpRequest();
$request->setBody($body);
$this->assertSame($expectedHeaders, $request->getHeaders());
$this->assertSame($expectedBody, $request->getBody());
}
}