[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/externals/balanced-php/tests/Balanced/ -> ResourceTest.php (source)

   1  <?php
   2  
   3  namespace Balanced\Test;
   4  
   5  \Balanced\Bootstrap::init();
   6  \RESTful\Bootstrap::init();
   7  \Httpful\Bootstrap::init();
   8  
   9  use Balanced\Resource;
  10  use Balanced\Settings;
  11  use Balanced\APIKey;
  12  use Balanced\Marketplace;
  13  use Balanced\Credit;
  14  use Balanced\Debit;
  15  use Balanced\Refund;
  16  use Balanced\Account;
  17  use Balanced\Merchant;
  18  use Balanced\BankAccount;
  19  use Balanced\Card;
  20  use Balanced\Hold;
  21  
  22  use \RESTful\Collection;
  23  
  24  
  25  class APIKeyTest extends \PHPUnit_Framework_TestCase
  26  {
  27  
  28      function testRegistry()
  29      {
  30          $this->expectOutputString('');
  31          $result = Resource::getRegistry()->match('/v1/api_keys');
  32          return;
  33          $expected = array(
  34              'collection' => true,
  35              'class' => 'Balanced\APIKey',
  36              );
  37          $this->assertEquals($expected, $result);
  38          $result = Resource::getRegistry()->match('/v1/api_keys/1234');
  39          $expected = array(
  40              'collection' => false,
  41              'class' => 'Balanced\APIKey',
  42              'ids' => array('id' => '1234'),
  43              );
  44          $this->assertEquals($expected, $result);
  45      }
  46  }
  47  
  48  class MarketplaceTest extends \PHPUnit_Framework_TestCase
  49  {
  50      function testRegistry()
  51      {
  52          $result = Resource::getRegistry()->match('/v1/marketplaces');
  53          $expected = array(
  54              'collection' => true,
  55              'class' => 'Balanced\Marketplace',
  56              );
  57          $this->assertEquals($expected, $result);
  58          $result = Resource::getRegistry()->match('/v1/marketplaces/1122');
  59          $expected = array(
  60              'collection' => false,
  61              'class' => 'Balanced\Marketplace',
  62              'ids' => array('id' => '1122'),
  63              );
  64          $this->assertEquals($expected, $result);
  65      }
  66      
  67      function testCreateCard()
  68      {
  69          $collection = $this->getMock(
  70              '\RESTful\Collection',
  71              array('create'),
  72              array('\Balanced\Card', 'some/uri', null)
  73              );
  74  
  75          $collection->expects($this->once())
  76                     ->method('create')
  77                     ->with(array(
  78                         'street_address' => '123 Fake Street',
  79                         'city' => 'Jollywood',
  80                         'region' => '',
  81                         'postal_code' => '90210',
  82                         'name' => 'khalkhalash',
  83                         'card_number' => '4112344112344113',
  84                         'security_code' => '123',
  85                         'expiration_month' => 12,
  86                         'expiration_year' => 2013,
  87                         ));
  88          
  89          $marketplace = new Marketplace(array('cards' => $collection));
  90          $marketplace->createCard(
  91              '123 Fake Street',
  92              'Jollywood',
  93              '',
  94              '90210',
  95              'khalkhalash',
  96              '4112344112344113',
  97              '123',
  98              12,
  99              2013);
 100      }
 101  
 102      function testCreateBankAccount()
 103      {
 104          $collection = $this->getMock(
 105              '\RESTful\Collection',
 106              array('create'),
 107              array('\Balanced\BankAccount', 'some/uri', null)
 108              );
 109          
 110          $collection->expects($this->once())
 111                     ->method('create')
 112                     ->with(array(
 113                         'name' => 'Homer Jay',
 114                         'account_number' => '112233a',
 115                         'routing_number' => '121042882',
 116                         'type' => 'savings',
 117                         'meta' => null
 118                         ));
 119          
 120          $marketplace = new Marketplace(array('bank_accounts' => $collection));
 121          $marketplace->createBankAccount(
 122              'Homer Jay',
 123              '112233a',
 124              '121042882',
 125              'savings');
 126      }
 127      
 128      function testCreateAccount()
 129      {
 130          $collection = $this->getMock(
 131              '\RESTful\Collection',
 132              array('create'),
 133              array('\Balanced\Account', 'some/uri', null)
 134              );
 135      
 136          $collection->expects($this->once())
 137          ->method('create')
 138          ->with(array(
 139              'email_address' => '[email protected]',
 140              'meta' => array('test#' => 'test_d')
 141               ));
 142      
 143          $marketplace = new Marketplace(array('accounts' => $collection));
 144          $marketplace->createAccount(
 145              '[email protected]',
 146              array('test#' => 'test_d')
 147              );
 148      }
 149      
 150      function testCreateBuyer()
 151      {
 152          $collection = $this->getMock(
 153              '\RESTful\Collection',
 154              array('create'),
 155              array('\Balanced\Account', 'some/uri', null)
 156              );
 157          
 158          $collection->expects($this->once())
 159                     ->method('create')
 160                     ->with(array(
 161                         'email_address' => '[email protected]',
 162                         'card_uri' => '/some/card/uri',
 163                         'meta' => array('test#' => 'test_d'),
 164                         'name' => 'Buy Er'
 165                         ));
 166          
 167          $marketplace = new Marketplace(array('accounts' => $collection));
 168          $marketplace->createBuyer(
 169              '[email protected]',
 170              '/some/card/uri',
 171              array('test#' => 'test_d'),
 172              'Buy Er'
 173              );
 174      }
 175  }
 176  
 177  class AccountTest extends \PHPUnit_Framework_TestCase
 178  {
 179      function testRegistry()
 180      {
 181          $result = Resource::getRegistry()->match('/v1/accounts');
 182          $expected = array(
 183              'collection' => true,
 184              'class' => 'Balanced\Account',
 185              );
 186          $this->assertEquals($expected, $result);
 187          $result = Resource::getRegistry()->match('/v1/accounts/0099');
 188          $expected = array(
 189              'collection' => false,
 190              'class' => 'Balanced\Account',
 191              'ids' => array('id' => '0099'),
 192              );
 193          $this->assertEquals($expected, $result);
 194      }
 195  
 196      function testCredit()
 197      {
 198          $collection = $this->getMock(
 199              '\RESTful\Collection',
 200              array('create'),
 201              array('\Balanced\Credit', 'some/uri', null)
 202          );
 203          
 204          $collection
 205              ->expects($this->once())
 206              ->method('create')
 207              ->with(array(
 208                 'amount' => 101,
 209                 'description' => 'something sweet',
 210                 'meta' => null,
 211                 'destination_uri' => null,
 212                 'appears_on_statement_as' => null
 213                 ));
 214          
 215          $account = new Account(array('credits' => $collection));
 216          $account->credit(101, 'something sweet');
 217      }
 218      
 219      function testDebit()
 220      {
 221          $collection = $this->getMock(
 222              '\RESTful\Collection',
 223              array('create'),
 224              array('\Balanced\Debit', 'some/uri', null)
 225              );
 226          
 227          $collection
 228              ->expects($this->once())
 229              ->method('create')
 230              ->with(array(
 231                  'amount' => 9911,
 232                  'description' => 'something tangy',
 233                  'appears_on_statement_as' => 'BAL*TANG',
 234                  'meta' => null,
 235                  'source_uri' => null,
 236                  'on_behalf_of_uri' => null,
 237                  ));
 238          
 239          $account = new Account(array('debits' => $collection));
 240          $account->debit(9911, 'BAL*TANG', 'something tangy');
 241      }
 242      
 243      function testHold()
 244      {
 245          $collection = $this->getMock(
 246              '\RESTful\Collection',
 247              array('create'),
 248              array('\Balanced\Hold', 'some/uri', null)
 249              );
 250          
 251          $collection
 252              ->expects($this->once())
 253              ->method('create')
 254              ->with(array(
 255                  'amount' => 1243,
 256                  'description' => 'something crispy',
 257                  'source_uri' => '/some/card/uri',
 258                  'meta' => array('test#' => 'test_d')
 259                  ));
 260  
 261          $account = new Account(array('holds' => $collection));
 262          $account->hold(
 263              1243,
 264              'something crispy',
 265              '/some/card/uri',
 266              array('test#' => 'test_d')
 267              );
 268      }
 269      
 270      function testAddCard()
 271      {
 272          $account = $this->getMock(
 273              '\Balanced\Account',
 274              array('save')
 275              );
 276          
 277          $account
 278              ->expects($this->once())
 279              ->method('save')
 280              ->with();
 281          
 282          $account->addCard('/my/new/card/121212');
 283          $this->assertEquals($account->card_uri, '/my/new/card/121212');
 284      }
 285      
 286      function testAddBankAccount()
 287      {
 288          $account = $this->getMock(
 289              '\Balanced\Account',
 290              array('save')
 291              );
 292          
 293          $account
 294              ->expects($this->once())
 295              ->method('save')
 296              ->with();
 297          
 298          $account->addBankAccount('/my/new/bank_account/121212');
 299          $this->assertEquals($account->bank_account_uri, '/my/new/bank_account/121212');
 300      }
 301      
 302      function testPromotToMerchant()
 303      {
 304          $account = $this->getMock(
 305              '\Balanced\Account',
 306              array('save')
 307              );
 308      
 309          $account
 310              ->expects($this->once())
 311              ->method('save')
 312              ->with();
 313      
 314          $merchant = array(
 315              'type' => 'person',
 316              'name' => 'William James',
 317              'tax_id' => '393-48-3992',
 318              'street_address' => '167 West 74th Street',
 319              'postal_code' => '10023',
 320              'dob' => '1842-01-01',
 321              'phone_number' => '+16505551234',
 322              'country_code' => 'USA'
 323              );
 324  
 325          $account->promoteToMerchant($merchant);
 326          $this->assertEquals($account->merchant, $merchant);
 327      }
 328  }
 329  
 330  class HoldTest extends \PHPUnit_Framework_TestCase
 331  {
 332      function testRegistry()
 333      {
 334          $result = Resource::getRegistry()->match('/v1/holds');
 335          $expected = array(
 336              'collection' => true,
 337              'class' => 'Balanced\Hold',
 338          );
 339          $this->assertEquals($expected, $result);
 340          $result = Resource::getRegistry()->match('/v1/holds/112233');
 341          $expected = array(
 342              'collection' => false,
 343              'class' => 'Balanced\Hold',
 344              'ids' => array('id' => '112233'),
 345          );
 346          $this->assertEquals($expected, $result);
 347      }
 348      
 349      function testVoid()
 350      {
 351          $hold = $this->getMock(
 352              '\Balanced\Hold',
 353              array('save')
 354              );
 355          
 356          $hold
 357              ->expects($this->once())
 358              ->method('save')
 359              ->with();
 360          
 361          $hold->void();
 362          $this->assertTrue($hold->is_void);
 363      }
 364      
 365      function testCapture()
 366      {
 367          $collection = $this->getMock(
 368              '\RESTful\Collection',
 369              array('create'),
 370              array('\Balanced\Debit', 'some/uri', null)
 371              );
 372          
 373          $collection
 374              ->expects($this->once())
 375              ->method('create')
 376              ->with(array(
 377                  'hold_uri' => 'some/hold/uri',
 378                  'amount' => 2211,
 379                  ));
 380  
 381          $account = new Account(array('debits' => $collection));
 382          
 383          $hold = new Hold(array('uri' => 'some/hold/uri', 'account' => $account));
 384          
 385          $hold->capture(2211);
 386      }
 387  }
 388  
 389  class CreditTest extends \PHPUnit_Framework_TestCase
 390  {
 391      function testRegistry()
 392      {
 393          $result = Resource::getRegistry()->match('/v1/credits');
 394          $expected = array(
 395              'collection' => true,
 396              'class' => 'Balanced\Credit',
 397              );
 398          $this->assertEquals($expected, $result);
 399          $result = Resource::getRegistry()->match('/v1/credits/9988');
 400          $expected = array(
 401              'collection' => false,
 402              'class' => 'Balanced\Credit',
 403              'ids' => array('id' => '9988'),
 404              );
 405          $this->assertEquals($expected, $result);
 406      }
 407  }
 408  
 409  class DebitTest extends \PHPUnit_Framework_TestCase
 410  {
 411      function testRegistry()
 412      {
 413          $result = Resource::getRegistry()->match('/v1/debits');
 414          $expected = array(
 415              'collection' => true,
 416              'class' => 'Balanced\Debit',
 417              );
 418          $this->assertEquals($expected, $result);
 419          $result = Resource::getRegistry()->match('/v1/debits/4545');
 420          $expected = array(
 421              'collection' => false,
 422              'class' => 'Balanced\Debit',
 423              'ids' => array('id' => '4545'),
 424              );
 425          $this->assertEquals($expected, $result);
 426      }
 427      
 428      function testRefund()
 429      {
 430          $collection = $this->getMock(
 431              '\RESTful\Collection',
 432              array('create'),
 433              array('\Balanced\Refund', 'some/uri', null)
 434          );
 435          
 436          $collection
 437              ->expects($this->once())
 438              ->method('create')
 439              ->with(array(
 440                  'amount' => 5645,
 441                  'description' => null,
 442                  'meta' => array('test#' => 'test_d')
 443                  ));
 444          
 445          $debit = new Debit(array('refunds' => $collection));
 446          
 447          $debit->refund(5645, null, array('test#' => 'test_d'));
 448      }
 449  }
 450  
 451  class RefundTest extends \PHPUnit_Framework_TestCase
 452  {
 453      function testRegistry()
 454      {
 455          $result = Resource::getRegistry()->match('/v1/refunds');
 456          $expected = array(
 457              'collection' => true,
 458              'class' => 'Balanced\Refund',
 459              );
 460          $this->assertEquals($expected, $result);
 461          $result = Resource::getRegistry()->match('/v1/refunds/1287');
 462          $expected = array(
 463              'collection' => false,
 464              'class' => 'Balanced\Refund',
 465              'ids' => array('id' => '1287'),
 466              );
 467          $this->assertEquals($expected, $result);
 468      }
 469  }
 470  
 471  class BankAccountTest extends \PHPUnit_Framework_TestCase
 472  {
 473      function testRegistry()
 474      {
 475          $result = Resource::getRegistry()->match('/v1/bank_accounts');
 476          $expected = array(
 477              'collection' => true,
 478              'class' => 'Balanced\BankAccount',
 479              );
 480          $this->assertEquals($expected, $result);
 481          $result = Resource::getRegistry()->match('/v1/bank_accounts/887766');
 482          $expected = array(
 483                  'collection' => false,
 484                  'class' => 'Balanced\BankAccount',
 485                  'ids' => array('id' => '887766'),
 486          );
 487          $this->assertEquals($expected, $result);
 488      }
 489      
 490      function testCreditAccount()
 491      {
 492          $collection = $this->getMock(
 493                  '\RESTful\Collection',
 494                  array('create'),
 495                  array('\Balanced\Credit', 'some/uri', null)
 496          );
 497      
 498          $collection
 499          ->expects($this->once())
 500          ->method('create')
 501          ->with(array(
 502                  'amount' => 101,
 503                  'description' => 'something super sweet',
 504                  'meta' => null,
 505                  'destination_uri' => '/some/other/uri',
 506                  'appears_on_statement_as' => null
 507          ));
 508      
 509          $account = new Account(array('credits' => $collection));
 510          $bank_account = new BankAccount(array('uri' => '/some/other/uri', 'account' => $account));
 511          
 512          $bank_account->credit(101, 'something super sweet');
 513      }
 514  
 515      function testCreditAccountless()
 516      {
 517           $collection = $this->getMock(
 518                  '\RESTful\Collection',
 519                  array('create'),
 520                  array('\Balanced\Credit', 'some/uri', null)
 521          );
 522      
 523          $collection
 524          ->expects($this->once())
 525          ->method('create')
 526          ->with(array(
 527                  'amount' => 101,
 528                  'description' => 'something super sweet',
 529          ));
 530          $bank_account = new BankAccount(array(
 531                  'uri' => '/some/other/uri',
 532                  'account' => null,
 533                  'credits' => $collection,
 534                  ));
 535  
 536          $bank_account->credit(101, 'something super sweet');
 537      }
 538  }
 539  
 540  class CardTest extends \PHPUnit_Framework_TestCase
 541  {
 542      function testRegistry()
 543      {
 544          $result = Resource::getRegistry()->match('/v1/cards');
 545          $expected = array(
 546              'collection' => true,
 547              'class' => 'Balanced\Card',
 548              );
 549          $this->assertEquals($expected, $result);
 550          $result = Resource::getRegistry()->match('/v1/cards/136asd6713');
 551          $expected = array(
 552              'collection' => false,
 553              'class' => 'Balanced\Card',
 554              'ids' => array('id' => '136asd6713'),
 555              );
 556          $this->assertEquals($expected, $result);
 557      }
 558      
 559      function testDebit()
 560      {
 561          $collection = $this->getMock(
 562                  '\RESTful\Collection',
 563                  array('create'),
 564                  array('\Balanced\Debit', 'some/uri', null)
 565                  );
 566      
 567          $account = new Account(array('debits' => $collection));
 568          $card = new Card(array('uri' => '/some/uri', 'account' => $account ));
 569          
 570          $collection
 571          ->expects($this->once())
 572          ->method('create')
 573          ->with(array(
 574                  'amount' => 9911,
 575                  'description' => 'something tangy',
 576                  'appears_on_statement_as' => 'BAL*TANG',
 577                  'meta' => null,
 578                  'source_uri' => '/some/uri',
 579                  'on_behalf_of_uri' => null,
 580                  ));
 581          
 582          $card->debit(9911, 'BAL*TANG', 'something tangy');
 583      }
 584      
 585      /**
 586       * @expectedException \UnexpectedValueException
 587       */
 588      function testNotAssociatedDebit()
 589      {
 590          $card = new Card(array('uri' => '/some/uri', 'account' => null ));
 591          $card->debit(9911, 'BAL*TANG', 'something tangy');
 592      }
 593  }
 594  
 595  
 596  class MerchantTest extends \PHPUnit_Framework_TestCase
 597  {
 598      function testRegistry()
 599      {
 600          $result = Resource::getRegistry()->match('/v1/merchants');
 601          $expected = array(
 602              'collection' => true,
 603              'class' => 'Balanced\Merchant',
 604          );
 605          $this->assertEquals($expected, $result);
 606          $result = Resource::getRegistry()->match('/v1/merchants/136asd6713');
 607          $expected = array(
 608              'collection' => false,
 609              'class' => 'Balanced\Merchant',
 610              'ids' => array('id' => '136asd6713'),
 611          );
 612          $this->assertEquals($expected, $result);
 613      }
 614  }


Generated: Sun Nov 30 09:20:46 2014 Cross-referenced by PHPXref 0.7.1