2014-11-06 02:44:52 +03:00
|
|
|
<?php
|
|
|
|
|
2016-07-22 01:01:47 +03:00
|
|
|
/**
|
2017-06-22 00:55:08 +03:00
|
|
|
* PHP version 5.4
|
2016-07-22 01:01:47 +03:00
|
|
|
*
|
|
|
|
* API client response test class
|
|
|
|
*
|
|
|
|
* @category RetailCrm
|
|
|
|
* @package RetailCrm
|
|
|
|
* @author RetailCrm <integration@retailcrm.ru>
|
|
|
|
* @license https://opensource.org/licenses/MIT MIT License
|
2019-08-30 14:10:52 +03:00
|
|
|
* @link https://help.retailcrm.ru/Developers/ApiVersion5
|
2016-07-22 01:01:47 +03:00
|
|
|
*/
|
|
|
|
|
2014-11-06 02:44:52 +03:00
|
|
|
namespace RetailCrm\Tests\Response;
|
|
|
|
|
|
|
|
use RetailCrm\Test\TestCase;
|
|
|
|
use RetailCrm\Response\ApiResponse;
|
|
|
|
|
2016-07-22 01:01:47 +03:00
|
|
|
/**
|
|
|
|
* Class ApiResponseTest
|
|
|
|
*
|
|
|
|
* @category RetailCrm
|
|
|
|
* @package RetailCrm
|
|
|
|
* @author RetailCrm <integration@retailcrm.ru>
|
|
|
|
* @license https://opensource.org/licenses/MIT MIT License
|
2019-08-30 14:10:52 +03:00
|
|
|
* @link https://help.retailcrm.ru/Developers/ApiVersion5
|
2016-07-22 01:01:47 +03:00
|
|
|
*/
|
2014-11-06 02:44:52 +03:00
|
|
|
class ApiResponseTest extends TestCase
|
|
|
|
{
|
|
|
|
/**
|
2017-06-22 00:55:08 +03:00
|
|
|
* @group response
|
2014-11-06 02:44:52 +03:00
|
|
|
*/
|
|
|
|
public function testSuccessConstruct()
|
|
|
|
{
|
|
|
|
$response = new ApiResponse(200);
|
2017-06-22 00:55:08 +03:00
|
|
|
static::assertInstanceOf(
|
2014-11-06 02:44:52 +03:00
|
|
|
'RetailCrm\Response\ApiResponse',
|
|
|
|
$response,
|
|
|
|
'Response object created'
|
|
|
|
);
|
|
|
|
|
2020-02-12 14:07:27 +03:00
|
|
|
$response = (new ApiResponse(201, '{ "success": true }'))->asJsonResponse();
|
2017-06-22 00:55:08 +03:00
|
|
|
static::assertInstanceOf(
|
2014-11-06 02:44:52 +03:00
|
|
|
'RetailCrm\Response\ApiResponse',
|
|
|
|
$response,
|
|
|
|
'Response object created'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-06-22 00:55:08 +03:00
|
|
|
* @group response
|
2016-03-12 01:54:33 +03:00
|
|
|
* @expectedException \RetailCrm\Exception\InvalidJsonException
|
2014-11-06 02:44:52 +03:00
|
|
|
*/
|
|
|
|
public function testJsonInvalid()
|
|
|
|
{
|
2020-02-12 14:07:27 +03:00
|
|
|
(new ApiResponse(400, '{ "asdf": }'))->asJsonResponse();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @group response
|
|
|
|
*/
|
|
|
|
public function testJsonInvalidNoDeserialize()
|
|
|
|
{
|
|
|
|
$response = new ApiResponse(400, '{ "asdf": }');
|
|
|
|
static::assertInstanceOf(
|
|
|
|
'RetailCrm\Response\ApiResponse',
|
|
|
|
$response,
|
|
|
|
'Response object created'
|
|
|
|
);
|
2014-11-06 02:44:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-06-22 00:55:08 +03:00
|
|
|
* @group response
|
2014-11-06 02:44:52 +03:00
|
|
|
*/
|
|
|
|
public function testStatusCodeGetting()
|
|
|
|
{
|
|
|
|
$response = new ApiResponse(200);
|
2017-06-22 00:55:08 +03:00
|
|
|
static::assertEquals(
|
2014-11-06 02:44:52 +03:00
|
|
|
200,
|
|
|
|
$response->getStatusCode(),
|
|
|
|
'Response object returns the right status code'
|
|
|
|
);
|
|
|
|
|
2020-02-12 14:07:27 +03:00
|
|
|
$response = (new ApiResponse(460, '{ "success": false }'))->asJsonResponse();
|
2017-06-22 00:55:08 +03:00
|
|
|
static::assertEquals(
|
2014-11-06 02:44:52 +03:00
|
|
|
460,
|
|
|
|
$response->getStatusCode(),
|
|
|
|
'Response object returns the right status code'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-06-22 00:55:08 +03:00
|
|
|
* @group response
|
2014-11-06 02:44:52 +03:00
|
|
|
*/
|
|
|
|
public function testIsSuccessful()
|
|
|
|
{
|
|
|
|
$response = new ApiResponse(200);
|
2017-06-22 00:55:08 +03:00
|
|
|
static::assertTrue(
|
2014-11-06 02:44:52 +03:00
|
|
|
$response->isSuccessful(),
|
|
|
|
'Request was successful'
|
|
|
|
);
|
|
|
|
|
2020-02-12 14:07:27 +03:00
|
|
|
$response = (new ApiResponse(460, '{ "success": false }'))->asJsonResponse();
|
2017-06-22 00:55:08 +03:00
|
|
|
static::assertFalse(
|
2014-11-06 02:44:52 +03:00
|
|
|
$response->isSuccessful(),
|
|
|
|
'Request was failed'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-06-22 00:55:08 +03:00
|
|
|
* @group response
|
2014-11-06 02:44:52 +03:00
|
|
|
*/
|
|
|
|
public function testMagicCall()
|
|
|
|
{
|
2020-02-12 14:07:27 +03:00
|
|
|
$response = (new ApiResponse(201, '{ "success": true }'))->asJsonResponse();
|
2017-06-22 00:55:08 +03:00
|
|
|
static::assertEquals(
|
2014-11-06 02:44:52 +03:00
|
|
|
true,
|
2017-06-22 00:55:08 +03:00
|
|
|
$response->isSuccessful(),
|
2014-11-06 02:44:52 +03:00
|
|
|
'Response object returns property value throw magic method'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-06-22 00:55:08 +03:00
|
|
|
* @group response
|
2014-11-06 02:44:52 +03:00
|
|
|
* @expectedException \InvalidArgumentException
|
|
|
|
*/
|
|
|
|
public function testMagicCallException1()
|
|
|
|
{
|
|
|
|
$response = new ApiResponse(200);
|
2018-02-20 17:50:45 +03:00
|
|
|
/* @noinspection PhpUndefinedMethodInspection */
|
2014-11-06 02:44:52 +03:00
|
|
|
$response->getSome();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-06-22 00:55:08 +03:00
|
|
|
* @group response
|
2014-11-06 02:44:52 +03:00
|
|
|
* @expectedException \InvalidArgumentException
|
|
|
|
*/
|
|
|
|
public function testMagicCallException2()
|
|
|
|
{
|
2020-02-12 14:07:27 +03:00
|
|
|
$response = (new ApiResponse(201, '{ "success": true }'))->asJsonResponse();
|
2018-02-20 17:50:45 +03:00
|
|
|
/* @noinspection PhpUndefinedMethodInspection */
|
2014-11-06 02:44:52 +03:00
|
|
|
$response->getSomeSuccess();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-06-22 00:55:08 +03:00
|
|
|
* @group response
|
2014-11-06 02:44:52 +03:00
|
|
|
*/
|
|
|
|
public function testMagicGet()
|
|
|
|
{
|
2020-02-12 14:07:27 +03:00
|
|
|
$response = (new ApiResponse(201, '{ "success": true }'))->asJsonResponse();
|
2017-06-22 00:55:08 +03:00
|
|
|
static::assertEquals(
|
2014-11-06 02:44:52 +03:00
|
|
|
true,
|
|
|
|
$response->success,
|
|
|
|
'Response object returns property value throw magic get'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-06-22 00:55:08 +03:00
|
|
|
* @group response
|
2014-11-06 02:44:52 +03:00
|
|
|
* @expectedException \InvalidArgumentException
|
|
|
|
*/
|
|
|
|
public function testMagicGetException1()
|
|
|
|
{
|
|
|
|
$response = new ApiResponse(200);
|
2018-02-20 17:50:45 +03:00
|
|
|
/* @noinspection PhpUndefinedFieldInspection */
|
2014-11-06 02:44:52 +03:00
|
|
|
$response->some;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-06-22 00:55:08 +03:00
|
|
|
* @group response
|
2014-11-06 02:44:52 +03:00
|
|
|
* @expectedException \InvalidArgumentException
|
|
|
|
*/
|
|
|
|
public function testMagicGetException2()
|
|
|
|
{
|
2020-02-12 14:07:27 +03:00
|
|
|
$response = (new ApiResponse(201, '{ "success": true }'))->asJsonResponse();
|
2018-02-20 17:50:45 +03:00
|
|
|
/* @noinspection PhpUndefinedFieldInspection */
|
2014-11-06 02:44:52 +03:00
|
|
|
$response->someSuccess;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-06-22 00:55:08 +03:00
|
|
|
* @group response
|
2014-11-06 02:44:52 +03:00
|
|
|
*/
|
|
|
|
public function testArrayGet()
|
|
|
|
{
|
2020-02-12 14:07:27 +03:00
|
|
|
$response = (new ApiResponse(201, '{ "success": true }'))->asJsonResponse();
|
2017-06-22 00:55:08 +03:00
|
|
|
static::assertEquals(
|
2014-11-06 02:44:52 +03:00
|
|
|
true,
|
|
|
|
$response['success'],
|
|
|
|
'Response object returns property value throw magic array get'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-06-22 00:55:08 +03:00
|
|
|
* @group response
|
2014-11-06 02:44:52 +03:00
|
|
|
* @expectedException \InvalidArgumentException
|
|
|
|
*/
|
|
|
|
public function testArrayGetException1()
|
|
|
|
{
|
|
|
|
$response = new ApiResponse(200);
|
|
|
|
$response['some'];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-06-22 00:55:08 +03:00
|
|
|
* @group response
|
2014-11-06 02:44:52 +03:00
|
|
|
* @expectedException \InvalidArgumentException
|
|
|
|
*/
|
|
|
|
public function testArrayGetException2()
|
|
|
|
{
|
2020-02-12 14:07:27 +03:00
|
|
|
$response = (new ApiResponse(201, '{ "success": true }'))->asJsonResponse();
|
2014-11-06 02:44:52 +03:00
|
|
|
$response['someSuccess'];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-06-22 00:55:08 +03:00
|
|
|
* @group response
|
2014-11-06 02:44:52 +03:00
|
|
|
*/
|
|
|
|
public function testArrayIsset()
|
|
|
|
{
|
2020-02-12 14:07:27 +03:00
|
|
|
$response = (new ApiResponse(201, '{ "success": true }'))->asJsonResponse();
|
2014-11-06 02:44:52 +03:00
|
|
|
|
2017-06-22 00:55:08 +03:00
|
|
|
static::assertTrue(
|
2014-11-06 02:44:52 +03:00
|
|
|
isset($response['success']),
|
|
|
|
'Response object returns property existing'
|
|
|
|
);
|
|
|
|
|
2017-06-22 00:55:08 +03:00
|
|
|
static::assertFalse(
|
2014-11-06 02:44:52 +03:00
|
|
|
isset($response['suess']),
|
|
|
|
'Response object returns property existing'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-06-22 00:55:08 +03:00
|
|
|
* @group response
|
2014-11-06 02:44:52 +03:00
|
|
|
* @expectedException \BadMethodCallException
|
|
|
|
*/
|
|
|
|
public function testArraySetException1()
|
|
|
|
{
|
2020-02-12 14:07:27 +03:00
|
|
|
$response = (new ApiResponse(201, '{ "success": true }'))->asJsonResponse();
|
2014-11-06 02:44:52 +03:00
|
|
|
$response['success'] = 'a';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-06-22 00:55:08 +03:00
|
|
|
* @group response
|
2014-11-06 02:44:52 +03:00
|
|
|
* @expectedException \BadMethodCallException
|
|
|
|
*/
|
|
|
|
public function testArraySetException2()
|
|
|
|
{
|
2020-02-12 14:07:27 +03:00
|
|
|
$response = (new ApiResponse(201, '{ "success": true }'))->asJsonResponse();
|
2014-11-06 02:44:52 +03:00
|
|
|
$response['sssssssuccess'] = 'a';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-06-22 00:55:08 +03:00
|
|
|
* @group response
|
2014-11-06 02:44:52 +03:00
|
|
|
* @expectedException \BadMethodCallException
|
|
|
|
*/
|
|
|
|
public function testArrayUnsetException1()
|
|
|
|
{
|
2020-02-12 14:07:27 +03:00
|
|
|
$response = (new ApiResponse(201, '{ "success": true }'))->asJsonResponse();
|
2014-11-06 02:44:52 +03:00
|
|
|
unset($response['success']);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-06-22 00:55:08 +03:00
|
|
|
* @group response
|
2014-11-06 02:44:52 +03:00
|
|
|
* @expectedException \BadMethodCallException
|
|
|
|
*/
|
|
|
|
public function testArrayUnsetException2()
|
|
|
|
{
|
2020-02-12 14:07:27 +03:00
|
|
|
$response = (new ApiResponse(201, '{ "success": true }'))->asJsonResponse();
|
2014-11-06 02:44:52 +03:00
|
|
|
unset($response['sssssssuccess']);
|
|
|
|
}
|
2017-03-07 12:20:27 +03:00
|
|
|
|
|
|
|
/**
|
2017-06-22 00:55:08 +03:00
|
|
|
* @group response
|
2017-03-07 12:20:27 +03:00
|
|
|
*/
|
|
|
|
public function testMagicIsset()
|
|
|
|
{
|
2020-02-12 14:07:27 +03:00
|
|
|
$response = (new ApiResponse(201, '{ "success": true }'))->asJsonResponse();
|
2017-03-07 12:20:27 +03:00
|
|
|
|
2017-06-22 00:55:08 +03:00
|
|
|
static::assertTrue(
|
2017-03-07 12:20:27 +03:00
|
|
|
isset($response->success),
|
|
|
|
'Response object returns property existing'
|
|
|
|
);
|
|
|
|
|
2017-06-22 00:55:08 +03:00
|
|
|
static::assertFalse(
|
2017-03-07 12:20:27 +03:00
|
|
|
isset($response->suess),
|
|
|
|
'Response object returns property existing'
|
|
|
|
);
|
|
|
|
}
|
2014-11-06 02:44:52 +03:00
|
|
|
}
|