Skip to content

Commit

Permalink
More tests
Browse files Browse the repository at this point in the history
  • Loading branch information
SecondeJK committed Jan 31, 2025
1 parent 07a5557 commit 5db10b5
Show file tree
Hide file tree
Showing 4 changed files with 168 additions and 0 deletions.
20 changes: 20 additions & 0 deletions test/Client/Exception/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,24 @@ public function testExceptionIsInstanceOfBaseException(): void

$this->assertInstanceOf(\Exception::class, $exception);
}

public function testSetAndGetRequestId(): void
{
$exception = new Request('Test exception');
$requestId = '12345';

$exception->setRequestId($requestId);

$this->assertSame($requestId, $exception->getRequestId());
}

public function testSetAndGetNetworkId(): void
{
$exception = new Request('Test exception');
$networkId = '67890';

$exception->setNetworkId($networkId);

$this->assertSame($networkId, $exception->getNetworkId());
}
}
77 changes: 77 additions & 0 deletions test/Client/Response/AbstractResponseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

declare(strict_types=1);

namespace VonageTest\Client\Response;

use PHPUnit\Framework\TestCase;
use Vonage\Client\Response\AbstractResponse;

class AbstractResponseTest extends TestCase
{
public function testGetDataReturnsCorrectData()
{
$mock = $this->getMockForAbstractClass(AbstractResponse::class);

$reflection = new \ReflectionClass($mock);
$property = $reflection->getProperty('data');
$property->setAccessible(true);

$testData = ['key' => 'value'];
$property->setValue($mock, $testData);

$this->assertSame($testData, $mock->getData());
}

public function testIsSuccessReturnsTrueWhenStatusIsZero()
{
$mock = $this->getMockForAbstractClass(AbstractResponse::class);

$reflection = new \ReflectionClass($mock);
$property = $reflection->getProperty('data');
$property->setAccessible(true);

$property->setValue($mock, ['status' => 0]);

$this->assertTrue($mock->isSuccess());
}

public function testIsSuccessReturnsFalseWhenStatusIsNotZero()
{
$mock = $this->getMockForAbstractClass(AbstractResponse::class);

$reflection = new \ReflectionClass($mock);
$property = $reflection->getProperty('data');
$property->setAccessible(true);

$property->setValue($mock, ['status' => 1]);

$this->assertFalse($mock->isSuccess());
}

public function testIsErrorReturnsTrueWhenStatusIsNotZero()
{
$mock = $this->getMockForAbstractClass(AbstractResponse::class);

$reflection = new \ReflectionClass($mock);
$property = $reflection->getProperty('data');
$property->setAccessible(true);

$property->setValue($mock, ['status' => 1]);

$this->assertTrue($mock->isError());
}

public function testIsErrorReturnsFalseWhenStatusIsZero()
{
$mock = $this->getMockForAbstractClass(AbstractResponse::class);

$reflection = new \ReflectionClass($mock);
$property = $reflection->getProperty('data');
$property->setAccessible(true);

$property->setValue($mock, ['status' => 0]);

$this->assertFalse($mock->isError());
}
}
40 changes: 40 additions & 0 deletions test/Client/Response/ErrorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

namespace VonageTest\Client\Response;

use PHPUnit\Framework\TestCase;
use Vonage\Client\Response\Error;

class ErrorTest extends TestCase
{
public function testErrorInstance(): void
{
$data = [
'status' => '400',
'error_text' => 'Invalid request'
];

$error = new Error($data);

$this->assertInstanceOf(Error::class, $error);
$this->assertTrue($error->isError());
$this->assertFalse($error->isSuccess());
$this->assertEquals(400, $error->getCode());
$this->assertEquals('Invalid request', $error->getMessage());
}

public function testErrorTextNormalization(): void
{
$data = [
'status' => '500',
'error_text' => 'Internal Server Error'
];

$error = new Error($data);

$this->assertEquals(500, $error->getCode());
$this->assertEquals('Internal Server Error', $error->getMessage());
}
}
31 changes: 31 additions & 0 deletions test/Client/Response/ResponseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace VonageTest\Client\Response;

use PHPUnit\Framework\TestCase;
use Vonage\Client\Response\Response;
use RuntimeException;

class ResponseTest extends TestCase
{
public function testConstructorWithExpectedKeys()
{
$stub = new class(['key1' => 'value1', 'key2' => 'value2']) extends Response {
protected $expected = ['key1', 'key2'];
};

$this->assertInstanceOf(Response::class, $stub);
}

public function testConstructorThrowsExceptionOnMissingKeys()
{
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('missing expected response keys: key2');

new class(['key1' => 'value1']) extends Response {
protected $expected = ['key1', 'key2'];
};
}
}

0 comments on commit 5db10b5

Please sign in to comment.