Skip to content

Commit

Permalink
Improve test coverage.
Browse files Browse the repository at this point in the history
  • Loading branch information
mglaman committed Aug 12, 2017
1 parent 03a5155 commit f437ccf
Show file tree
Hide file tree
Showing 10 changed files with 113 additions and 42 deletions.
15 changes: 15 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# This file is for unifying the coding style for different editors and IDEs
# editorconfig.org

# PHP PSR-2 Coding Standards
# http://www.php-fig.org/psr/psr-2/

root = true

[*.php]
charset = utf-8
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
indent_style = space
indent_size = 4
8 changes: 0 additions & 8 deletions src/CreateCustomerProfileRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,6 @@ public function setProfile($profile)
$this->profile = $profile;
}

/**
* @return string
*/
public function getValidationMode()
{
return $this->validationMode;
}

/**
* @param string $validationMode
*/
Expand Down
11 changes: 11 additions & 0 deletions src/DataTypes/BaseDataType.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,17 @@ public function __set($name, $value)
$this->properties[$name] = $value;
}

public function __get($name)
{
return $this->properties[$name];
}

public function __isset($name)
{
return isset($this->properties[$name]);
}


public function __unset($name)
{
unset($this->properties[$name]);
Expand Down
2 changes: 2 additions & 0 deletions src/DataTypes/TransactionRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,12 @@ public function addPayment(PaymentMethodInterface $paymentMethod)
{
$this->properties['payment'][$paymentMethod->getType()] = $paymentMethod->toArray();
}

public function addOrder(Order $order)
{
$this->addDataType($order);
}

public function addLineItem(LineItem $lineItem)
{
$this->properties['lineItems'][$lineItem->getType()] = $lineItem->toArray();
Expand Down
9 changes: 8 additions & 1 deletion tests/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,12 @@ public function testConfiguration()

$config->setTransactionKey('test');
$this->assertEquals('test', $config->getTransactionKey());

$config->setRequestMode('json');
$this->assertEquals('json', $config->getRequestMode());

$this->assertTrue(strpos($config->getCertificateVerify(), 'resources/cert.pem') !== FALSE);
$config->setCertificateVerify('/path/to/some/cert.pem');
$this->assertTrue(strpos($config->getCertificateVerify(), 'resources/cert.pem') === FALSE);
}
}
}
5 changes: 1 addition & 4 deletions tests/CreateTransactionRequest/VoidTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,6 @@ public function testVoidTransaction()

$response = $request->execute();
$this->assertTrue(isset($response->transactionResponse));
$this->assertEquals('I00001', $response->getMessages()[0]->getCode());
$this->assertEquals('Successful.',
$response->getMessages()[0]->getText());
$this->assertEquals('Ok', $response->getResultCode());
$this->assertResponse($response, 'I00001', 'Successful.', 'Ok');
}
}
24 changes: 17 additions & 7 deletions tests/CustomerPaymentProfileRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@ class CustomerPaymentProfileRequestTest extends TestBase
public function testCreateCustomerPaymentProfileWithCreditCard()
{
$profile = new Profile([
'email' => 'example+' . rand(0, 10000) . '@example.com',
'email' => 'example+' . mt_rand() . '@example.com',
]);
$request = new CreateCustomerProfileRequest($this->configurationXml, $this->client);
$request->setProfile($profile);
$request->setValidationMode('none');
$response = $request->execute();
$this->assertResponse($response, 'I00001', 'Successful.', 'Ok');
$customerProfileId = $response->customerProfileId;

$request = new CreateCustomerPaymentProfileRequest($this->configurationXml, $this->client);
Expand Down Expand Up @@ -53,9 +54,7 @@ public function testCreateCustomerPaymentProfileWithCreditCard()
$request->setPaymentProfile($paymentProfile);

$response = $request->execute();
$this->assertEquals('I00001', $response->getMessages()[0]->getCode());
$this->assertEquals('Successful.', $response->getMessages()[0]->getText());
$this->assertEquals('Ok', $response->getResultCode());
$this->assertResponse($response, 'I00001', 'Successful.', 'Ok');
$this->assertTrue(isset($response->customerPaymentProfileId));

$customerPaymentProfileId = $response->customerPaymentProfileId;
Expand All @@ -65,6 +64,18 @@ public function testCreateCustomerPaymentProfileWithCreditCard()
$request->setCustomerPaymentProfileId($customerPaymentProfileId);
$response = $request->execute();
$this->assertEquals('I00001', $response->getMessages()[0]->getCode());
$this->assertEquals('XXXX1111', $response->paymentProfile->payment->creditCard->cardNumber);
$this->assertEquals('XXXX', $response->paymentProfile->payment->creditCard->expirationDate);

// Get profile again, with unmasked credit card expiration date.
$request = new GetCustomerPaymentProfileRequest($this->configurationXml, $this->client);
$request->setCustomerProfileId($customerProfileId);
$request->setCustomerPaymentProfileId($customerPaymentProfileId);
$request->setUnmaskExpirationDate(TRUE);
$response = $request->execute();
$this->assertEquals('I00001', $response->getMessages()[0]->getCode());
$this->assertEquals('XXXX1111', $response->paymentProfile->payment->creditCard->cardNumber);
$this->assertEquals('2020-12', $response->paymentProfile->payment->creditCard->expirationDate);

$request = new ValidateCustomerPaymentProfileRequest($this->configurationXml, $this->client);
$request->setCustomerProfileId($customerProfileId);
Expand Down Expand Up @@ -127,6 +138,7 @@ public function testCreateCustomerPaymentProfileWithBankAccount()
$customerProfileId = $response->customerProfileId;

$request = new CreateCustomerPaymentProfileRequest($this->configurationXml, $this->client);
$request->setValidationMode('testMode');
$request->setCustomerProfileId($customerProfileId);

$paymentProfile = new PaymentProfile([
Expand Down Expand Up @@ -155,9 +167,7 @@ public function testCreateCustomerPaymentProfileWithBankAccount()
$request->setPaymentProfile($paymentProfile);

$response = $request->execute();
$this->assertEquals('I00001', $response->getMessages()[0]->getCode());
$this->assertEquals('Successful.', $response->getMessages()[0]->getText());
$this->assertEquals('Ok', $response->getResultCode());
$this->assertResponse($response, 'I00001', 'Successful.', 'Ok');
$this->assertTrue(isset($response->customerPaymentProfileId));

$customerPaymentProfileId = $response->customerPaymentProfileId;
Expand Down
19 changes: 6 additions & 13 deletions tests/CustomerProfileRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ public function testGetCustomerProfileIdsRequest()
$request = new GetCustomerProfileIdsRequest($this->configurationXml, $this->client);
$response = $request->execute();
$this->assertTrue(isset($response->ids));
$this->assertEquals('I00001', $response->getMessages()[0]->getCode());
$this->assertEquals('Successful.', $response->getMessages()[0]->getText());
$this->assertEquals('Ok', $response->getResultCode());
$this->assertResponse($response, 'I00001', 'Successful.', 'Ok');
}

public function testCreateCustomerProfileCRUDRequests()
Expand All @@ -47,22 +45,21 @@ public function testCreateCustomerProfileCRUDRequests()
]));

$profile = new Profile([
'email' => 'example+' . rand(0, 10000) . '@example.com',
'email' => 'example+' . mt_rand() . '@example.com',
]);
$profile->addPaymentProfile($paymentProfile);

$request = new CreateCustomerProfileRequest($this->configurationXml, $this->client);
$request->setProfile($profile);
$response = $request->execute();
$this->assertResponse($response, 'I00001', 'Successful.', 'Ok');
$this->assertTrue(isset($response->customerProfileId));
$this->assertTrue(isset($response->customerPaymentProfileIdList));
$this->assertTrue(isset($response->validationDirectResponseList));

$request = new GetCustomerProfileRequest($this->configurationXml, $this->client, $response->customerProfileId);
$response = $request->execute();
$this->assertEquals('I00001', $response->getMessages()[0]->getCode());
$this->assertEquals('Successful.', $response->getMessages()[0]->getText());
$this->assertEquals('Ok', $response->getResultCode());
$this->assertResponse($response, 'I00001', 'Successful.', 'Ok');
$this->assertTrue(isset($response->profile));

$customerProfileId = $response->profile->customerProfileId;
Expand All @@ -72,19 +69,15 @@ public function testCreateCustomerProfileCRUDRequests()
]);
$request = new UpdateCustomerProfileRequest($this->configurationXml, $this->client, $profile);
$response = $request->execute();
$this->assertEquals('I00001', $response->getMessages()[0]->getCode());
$this->assertEquals('Successful.', $response->getMessages()[0]->getText());
$this->assertEquals('Ok', $response->getResultCode());
$this->assertResponse($response, 'I00001', 'Successful.', 'Ok');

$request = new DeleteCustomerProfileRequest(
$this->configurationXml,
$this->client,
$customerProfileId
);
$response = $request->execute();
$this->assertEquals('I00001', $response->getMessages()[0]->getCode());
$this->assertEquals('Successful.', $response->getMessages()[0]->getText());
$this->assertEquals('Ok', $response->getResultCode());
$this->assertResponse($response, 'I00001', 'Successful.', 'Ok');

}
}
34 changes: 34 additions & 0 deletions tests/DataTypeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace CommerceGuys\AuthNet\Tests;

use CommerceGuys\AuthNet\DataTypes\CreditCard;
use CommerceGuys\AuthNet\DataTypes\Message;

class DataTypeTest extends TestBase
{
public function testBaseDataType()
{
// Tests methods provided by \CommerceGuys\AuthNet\DataTypes\BaseDataType
$creditCard = new CreditCard([
'cardNumber' => '4111111111111111',
'expirationDate' => '2020-12',
]);
unset($creditCard->cardNumber);
$this->assertTrue(!isset($creditCard->cardNumber));
$creditCard->addData('cardNumber', '5424000000000015');
$this->assertEquals('5424000000000015', $creditCard->cardNumber);
}

public function testMessageValidationCode()
{
$this->setExpectedException(\InvalidArgumentException::class, 'Messages must have a code');
new Message(['message' => 'Test']);
}

public function testMessageValidationTest()
{
$this->setExpectedException(\InvalidArgumentException::class, 'Messages must have a text');
new Message(['code' => 'I00122']);
}
}
28 changes: 19 additions & 9 deletions tests/TestBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace CommerceGuys\AuthNet\Tests;

use CommerceGuys\AuthNet\Response\ResponseInterface;
use GuzzleHttp\Client;
use CommerceGuys\AuthNet\Configuration;
use CommerceGuys\AuthNet\RequestFactory;
Expand Down Expand Up @@ -37,21 +38,30 @@ protected function setUp()
{
parent::setUp();
$this->configurationXml = new Configuration([
'api_login' => AUTHORIZENET_API_LOGIN_ID,
'transaction_key' => AUTHORIZENET_TRANSACTION_KEY,
'sandbox' => true,
'certificate_verify' => TESTS_CERTIFICATE_VERIFY,
'api_login' => AUTHORIZENET_API_LOGIN_ID,
'transaction_key' => AUTHORIZENET_TRANSACTION_KEY,
'sandbox' => true,
'certificate_verify' => TESTS_CERTIFICATE_VERIFY,
]);
$this->configurationJson = new Configuration([
'api_login' => AUTHORIZENET_API_LOGIN_ID,
'transaction_key' => AUTHORIZENET_TRANSACTION_KEY,
'sandbox' => true,
'certificate_verify' => TESTS_CERTIFICATE_VERIFY,
'request_mode' => 'json',
'api_login' => AUTHORIZENET_API_LOGIN_ID,
'transaction_key' => AUTHORIZENET_TRANSACTION_KEY,
'sandbox' => true,
'certificate_verify' => TESTS_CERTIFICATE_VERIFY,
'request_mode' => 'json',
]);
$this->client = new Client();

$this->xmlRequestFactory = new RequestFactory($this->configurationXml, $this->client);
$this->jsonRequestFactory = new RequestFactory($this->configurationJson, $this->client);
}

protected function assertResponse(ResponseInterface $response, $expectedCode, $expectedText, $expectedResultCode)
{
$message = $response->getMessages()[0];
// Since this is first possible assert failure, message with response text.
$this->assertEquals($expectedCode, $message->getCode(), $message->getText());
$this->assertEquals($expectedText, $message->getText());
$this->assertEquals($expectedResultCode, $response->getResultCode());
}
}

0 comments on commit f437ccf

Please sign in to comment.