Skip to content

Commit

Permalink
Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Dmitry Gladyshev committed Apr 4, 2017
1 parent 37427d4 commit 7801435
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 7 deletions.
2 changes: 1 addition & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
colors="true">
<testsuites>
<testsuite name="all">
<directory>./tests</directory>
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>

Expand Down
51 changes: 51 additions & 0 deletions tests/Helper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php
/**
* @author Dmitry Gladyshev <[email protected]>
* @date 04.04.17 12:00
*/

namespace Yandex\Direct\Test;

use ReflectionClass;
use ReflectionMethod;
use ReflectionProperty;

/**
* Class Helper
* @package Yandex\Direct\Test
*/
class Helper
{
/**
* getPrivateProperty
*
* @author Joe Sexton <[email protected]>
* @param string $className
* @param string $propertyName
* @return ReflectionProperty
*/
public static function getPrivateProperty( $className, $propertyName ) {
$reflector = new ReflectionClass( $className );
$property = $reflector->getProperty( $propertyName );
$property->setAccessible( true );

return $property;
}

/**
* Get a private or protected method for testing/documentation purposes.
* How to use for MyClass->foo():
* $cls = new MyClass();
* $foo = PHPUnitUtil::getPrivateMethod($cls, 'foo');
* $foo->invoke($cls, $...);
* @param object $obj The instantiated instance of your class
* @param string $name The name of your private/protected method
* @return ReflectionMethod The method you asked for
*/
public static function getPrivateMethod($obj, $name) {
$class = new ReflectionClass($obj);
$method = $class->getMethod($name);
$method->setAccessible(true);
return $method;
}
}
54 changes: 51 additions & 3 deletions tests/ServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,22 @@


use PHPUnit\Framework\TestCase;
use ReflectionClass;
use ReflectionProperty;
use Yandex\Direct\CredentialsInterface;
use Yandex\Direct\Service;
use Yandex\Direct\ServiceFactoryInterface;
use Yandex\Direct\Transport\Response;
use Yandex\Direct\Transport\TransportInterface;
use Yandex\Direct\Transport\Request;
use Yandex\Direct\Transport\RequestInterface;


class ServiceTest extends TestCase
{
/**
* @covers Service::setName()
*/
public function testNameSetter()
{
$service = new DummyService;
Expand All @@ -25,6 +32,9 @@ public function testNameSetter()
$this->assertEquals($name, $service->getName());
}

/**
* @covers Service::setTransport()
*/
public function testTransportSetter()
{
$service = new DummyService;
Expand All @@ -33,25 +43,63 @@ public function testTransportSetter()
$this->assertEquals($transport, $service->getTransport());
}

/**
* @covers Service::setCredentials()
*/
public function testCredentialsSetter()
{
$service = new DummyService;
$mockCredentials = new MockCredentials;
$service->setCredentials($mockCredentials);
$this->assertEquals($mockCredentials, $service->getCredentials());
}

/**
* @covers Service::setUseOperatorUnits()
*/
public function testUseOperatorUnitsSetter()
{
$service = new DummyService;
$service->setUseOperatorUnits(true);
$this->assertEquals(true, Helper::getPrivateProperty(Service::class, 'useOperatorUnits')->getValue($service));

$service->setUseOperatorUnits(false);
$this->assertEquals(false, Helper::getPrivateProperty(Service::class, 'useOperatorUnits')->getValue($service));
}


public function testDoingRequest()
{
$service = new DummyService;
$service->setOptions([
'transport' => new MockTransport(),
'credentials' => new MockCredentials()
]);

$response = $service->request([]);

$this->assertArrayHasKey('results', $response);
$this->assertArrayHasKey('units', $response);
$this->assertArraySubset($response['units'], ['debit' => 0, 'limit' => 0,'rest' => 0]);
$this->assertArrayHasKey('request_id', $response);
}
}

class DummyService extends Service {
public function getName() {return $this->name;}
public function getTransport() {return $this->transport;}
public function getName() { return $this->name;}
public function getTransport() { return $this->transport;}
public function getCredentials() {return $this->credentials;}
}

class MockTransport implements TransportInterface {
public function setOptions(array $options){}
public function getServiceUrl($serviceName){}
public function request(RequestInterface $request){}
public function request(RequestInterface $request) { return new Response([
'headers' => [],
'body' => '{"results":{}}',
'requestId' => '1234567890',
'units' => '0/0/0'
]);}
}

class MockCredentials implements CredentialsInterface {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,25 @@
* @date 17/08/2016 12:51
*/

namespace Yandex\Direct\Test\Transport;
namespace Yandex\Direct\Test\Transport\Json;

use GuzzleHttp\Client as HttpClient;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\MessageFormatter;
use GuzzleHttp\Psr7\Response;
use PHPUnit\Framework\TestCase;
use Psr\Log\AbstractLogger;
use Yandex\Direct\CredentialsInterface;
use Yandex\Direct\Test\Helper;
use Yandex\Direct\Transport\Json\Transport;
use Yandex\Direct\Transport\Request;

/**
* Class JsonTransport
* @package Yandex\Direct\Test\Transport
*/
class JsonTransportTest extends TestCase
class TransportTest extends TestCase
{
public function testResolvingServiceUrls()
{
Expand Down Expand Up @@ -59,6 +62,45 @@ public function testDoingRequests($body, $headers, $code)
$this->assertEquals($response->getHeaders(), $headers);
}


public function testLoggerMiddleware()
{
$tr = new Transport;
$tr->setLogger(new MockPsr3Logger);

$loggerGetterReflection = Helper::getPrivateMethod($tr, 'getLogger');
$this->assertInstanceOf(
MockPsr3Logger::class,
$loggerGetterReflection->invoke($tr)
);

$messageFormatterGetterReflection = Helper::getPrivateMethod($tr, 'getMessageFormatter');
$this->assertInstanceOf(
MessageFormatter::class,
$messageFormatterGetterReflection->invoke($tr)
);

$httpHandlerStackGetterReflection = Helper::getPrivateMethod($tr, 'getHttpHandlers');
$this->assertInstanceOf(
HandlerStack::class,
$httpHandlerStackGetterReflection->invoke($tr)
);
}

public function testHeadersSetter()
{
$headers = [
'a' => 'b',
'c' => 'd',
'e' => 'f'
];

$tr = new Transport;
$tr->setHeaders($headers);
$this->assertArraySubset($headers, Helper::getPrivateProperty(Transport::class, 'headers')->getValue($tr));
}


public function providerMockYandexDirectResponse()
{
return [
Expand Down Expand Up @@ -94,4 +136,8 @@ class MockCredentials implements CredentialsInterface {
public function getMasterToken() { return ''; }
public function getToken() { return ''; }
public function getLogin() { return ''; }
}
}

class MockPsr3Logger extends AbstractLogger {
public function log($level, $message, array $context = array()) {}
}

0 comments on commit 7801435

Please sign in to comment.