1
0
mirror of synced 2024-11-24 06:16:27 +03:00
api-client-php/tests/RetailCrm/Tests/Response/ApiResponseTest.php

284 lines
6.9 KiB
PHP
Raw Normal View History

<?php
2016-07-22 01:01:47 +03:00
/**
* PHP version 5.4
2016-07-22 01:01:47 +03:00
*
* API client response test class
*
* @category RetailCrm
* @package RetailCrm
*/
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
*/
class ApiResponseTest extends TestCase
{
/**
* @group response
*/
public function testSuccessConstruct()
{
$response = new ApiResponse(200);
static::assertInstanceOf(
'RetailCrm\Response\ApiResponse',
$response,
'Response object created'
);
2020-02-12 14:07:27 +03:00
$response = (new ApiResponse(201, '{ "success": true }'))->asJsonResponse();
static::assertInstanceOf(
'RetailCrm\Response\ApiResponse',
$response,
'Response object created'
);
}
/**
* @group response
2016-03-12 01:54:33 +03:00
* @expectedException \RetailCrm\Exception\InvalidJsonException
*/
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'
);
}
/**
* @group response
*/
public function testStatusCodeGetting()
{
$response = new ApiResponse(200);
static::assertEquals(
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();
static::assertEquals(
460,
$response->getStatusCode(),
'Response object returns the right status code'
);
}
/**
* @group response
*/
public function testIsSuccessful()
{
$response = new ApiResponse(200);
static::assertTrue(
$response->isSuccessful(),
'Request was successful'
);
2020-02-12 14:07:27 +03:00
$response = (new ApiResponse(460, '{ "success": false }'))->asJsonResponse();
static::assertFalse(
$response->isSuccessful(),
'Request was failed'
);
}
/**
* @group response
*/
public function testMagicCall()
{
2020-02-12 14:07:27 +03:00
$response = (new ApiResponse(201, '{ "success": true }'))->asJsonResponse();
static::assertEquals(
true,
$response->isSuccessful(),
'Response object returns property value throw magic method'
);
}
/**
* @group response
* @expectedException \InvalidArgumentException
*/
public function testMagicCallException1()
{
$response = new ApiResponse(200);
/* @noinspection PhpUndefinedMethodInspection */
$response->getSome();
}
/**
* @group response
* @expectedException \InvalidArgumentException
*/
public function testMagicCallException2()
{
2020-02-12 14:07:27 +03:00
$response = (new ApiResponse(201, '{ "success": true }'))->asJsonResponse();
/* @noinspection PhpUndefinedMethodInspection */
$response->getSomeSuccess();
}
/**
* @group response
*/
public function testMagicGet()
{
2020-02-12 14:07:27 +03:00
$response = (new ApiResponse(201, '{ "success": true }'))->asJsonResponse();
static::assertEquals(
true,
$response->success,
'Response object returns property value throw magic get'
);
}
/**
* @group response
* @expectedException \InvalidArgumentException
*/
public function testMagicGetException1()
{
$response = new ApiResponse(200);
/* @noinspection PhpUndefinedFieldInspection */
$response->some;
}
/**
* @group response
* @expectedException \InvalidArgumentException
*/
public function testMagicGetException2()
{
2020-02-12 14:07:27 +03:00
$response = (new ApiResponse(201, '{ "success": true }'))->asJsonResponse();
/* @noinspection PhpUndefinedFieldInspection */
$response->someSuccess;
}
/**
* @group response
*/
public function testArrayGet()
{
2020-02-12 14:07:27 +03:00
$response = (new ApiResponse(201, '{ "success": true }'))->asJsonResponse();
static::assertEquals(
true,
$response['success'],
'Response object returns property value throw magic array get'
);
}
/**
* @group response
* @expectedException \InvalidArgumentException
*/
public function testArrayGetException1()
{
$response = new ApiResponse(200);
$response['some'];
}
/**
* @group response
* @expectedException \InvalidArgumentException
*/
public function testArrayGetException2()
{
2020-02-12 14:07:27 +03:00
$response = (new ApiResponse(201, '{ "success": true }'))->asJsonResponse();
$response['someSuccess'];
}
/**
* @group response
*/
public function testArrayIsset()
{
2020-02-12 14:07:27 +03:00
$response = (new ApiResponse(201, '{ "success": true }'))->asJsonResponse();
static::assertTrue(
isset($response['success']),
'Response object returns property existing'
);
static::assertFalse(
isset($response['suess']),
'Response object returns property existing'
);
}
/**
* @group response
* @expectedException \BadMethodCallException
*/
public function testArraySetException1()
{
2020-02-12 14:07:27 +03:00
$response = (new ApiResponse(201, '{ "success": true }'))->asJsonResponse();
$response['success'] = 'a';
}
/**
* @group response
* @expectedException \BadMethodCallException
*/
public function testArraySetException2()
{
2020-02-12 14:07:27 +03:00
$response = (new ApiResponse(201, '{ "success": true }'))->asJsonResponse();
$response['sssssssuccess'] = 'a';
}
/**
* @group response
* @expectedException \BadMethodCallException
*/
public function testArrayUnsetException1()
{
2020-02-12 14:07:27 +03:00
$response = (new ApiResponse(201, '{ "success": true }'))->asJsonResponse();
unset($response['success']);
}
/**
* @group response
* @expectedException \BadMethodCallException
*/
public function testArrayUnsetException2()
{
2020-02-12 14:07:27 +03:00
$response = (new ApiResponse(201, '{ "success": true }'))->asJsonResponse();
unset($response['sssssssuccess']);
}
/**
* @group response
*/
public function testMagicIsset()
{
2020-02-12 14:07:27 +03:00
$response = (new ApiResponse(201, '{ "success": true }'))->asJsonResponse();
static::assertTrue(
isset($response->success),
'Response object returns property existing'
);
static::assertFalse(
isset($response->suess),
'Response object returns property existing'
);
}
}