Advertisement
Guest User

api tests

a guest
Sep 19th, 2014
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 9.64 KB | None | 0 0
  1. <?php
  2.  
  3.  
  4. class ApiTest extends TestCase{
  5.     /** @var Shop $shop */
  6.     private $shop;
  7.  
  8.     /** @var \PB\Interfaces\ApiInterface */
  9.     private $apiRepo;
  10.  
  11.     /** @var \PB\Interfaces\ShopInterface */
  12.     private $shopRepo;
  13.  
  14.     /** @var \PB\Interfaces\ClientInterface */
  15.     private $clientRepo;
  16.  
  17.     /** @var \PB\Interfaces\TransactionInterface */
  18.     private $transRepo;
  19.  
  20.     private $httpAuth = [];
  21.  
  22.     protected function prepareForTests(){
  23.         parent::prepareForTests();
  24.  
  25.         $this->apiRepo = App::make('PB\Interfaces\ApiInterface');
  26.         $this->shopRepo = App::make('PB\Interfaces\ShopInterface');
  27.         $this->clientRepo = App::make('PB\Interfaces\ClientInterface');
  28.         $this->transRepo = App::make('PB\Interfaces\TransactionInterface');
  29.  
  30.         $this->shop = $this->getNewShop();
  31.         $this->shop->commission = 25; //25% commission when giving points
  32.         $this->shop->is_active = true;
  33.         $this->shop->save();
  34.  
  35.  
  36.         $this->httpAuth = [
  37.             'PHP_AUTH_USER' => $this->shop->id,
  38.             'PHP_AUTH_PW' => $this->shop->api_key,
  39.         ];
  40.  
  41.         // Filter is used for authorization
  42.         Route::enableFilters();
  43.     }
  44.  
  45.     /**
  46.      * @expectedException \Symfony\Component\HttpKernel\Exception\HttpException
  47.      * @expectedExceptionMessage Please provide a valid API key
  48.      */
  49.     public function testAuthWithoutCredentials(){
  50.         $this->call(\PB\Api\Methods\ShopStatus::METHOD, '/api/v1/' . \PB\Api\Methods\ShopStatus::URI);
  51.     }
  52.  
  53.     public function testAuthSuccess(){
  54.         $re = $this->call(\PB\Api\Methods\ShopStatus::METHOD, '/api/v1/' . \PB\Api\Methods\ShopStatus::URI, [], [], $this->httpAuth);
  55.  
  56.         $this->assertEquals(200, $re->getStatusCode());
  57.     }
  58.  
  59.     /**
  60.      * @expectedException \Symfony\Component\HttpKernel\Exception\HttpException
  61.      * @expectedExceptionMessage User or API key not found
  62.      */
  63.     public function testAuthBadCredentials(){
  64.         $this->call(\PB\Api\Methods\ShopStatus::METHOD, '/api/v1/' . \PB\Api\Methods\ShopStatus::URI, [], [], [
  65.             'PHP_AUTH_USER' => $this->shop->id,
  66.             'PHP_AUTH_PW' => 'bad api key',
  67.         ]);
  68.     }
  69.  
  70.     public function testGivePointsStatus(){
  71.         $giveAmount = 130;
  72.         $startAmount = 200;
  73.         $email = uniqid() . '@test.com';
  74.  
  75.         $this->shop->addPoints($startAmount, Currency::EUR);
  76.  
  77.         /*
  78.          * Test initial status
  79.          */
  80.         $re = $this->call(\PB\Api\Methods\ShopStatus::METHOD, '/api/v1/' . \PB\Api\Methods\ShopStatus::URI, [], [], $this->httpAuth);
  81.  
  82.         $this->assertTrue($re->isOk());
  83.  
  84.         $data = json_decode($re->getContent(), true);
  85.  
  86.         $this->assertEquals($startAmount, $data['points'][0]['points']);
  87.         $this->assertEquals(Currency::EUR, $data['points'][0]['currency']);
  88.  
  89.         /*
  90.          * Test give points to user
  91.          */
  92.  
  93.         $re = $this->call(\PB\Api\Methods\GivePoints::METHOD, '/api/v1/' . \PB\Api\Methods\GivePoints::URI, [
  94.             'email' => $email,
  95.             'currency' => Currency::EUR,
  96.             'points' => $giveAmount,
  97.             'phone' => '37120000000',
  98.             'order_id' => 1,
  99.         ], [], $this->httpAuth);
  100.  
  101.         $this->assertTrue($re->isOk());
  102.  
  103.         $data = json_decode($re->getContent(), true);
  104.  
  105.         $this->assertEquals(\Transaction\Base::STATUS_COMPLETED, $data['transaction']['status']);
  106.         $this->assertEquals($giveAmount, $data['transaction']['points'], 'Equal after commission');
  107.  
  108.         $transactionId = $data['transaction']['transactionId'];
  109.  
  110.         /*
  111.          * Test for transaction status
  112.          */
  113.         $re = $this->call(\PB\Api\Methods\Transaction::METHOD, '/api/v1/' . \PB\Api\Methods\Transaction::URI, [
  114.             'id' => $transactionId
  115.         ], [], $this->httpAuth);
  116.  
  117.         $this->assertTrue($re->isOk());
  118.  
  119.         $data = json_decode($re->getContent(), true);
  120.         $this->assertEquals(\Transaction\Base::STATUS_COMPLETED, $data['transaction']['status']);
  121.  
  122.         /*
  123.          * Test if user received points
  124.          */
  125.         $client = $this->clientRepo->getClientByMail($email);
  126.         $this->assertEquals($giveAmount, $client->pointAmount(Currency::EUR));
  127.  
  128.         /*
  129.          * Test if shops balance is updated
  130.          */
  131.         $re = $this->call(\PB\Api\Methods\ShopStatus::METHOD, '/api/v1/' . \PB\Api\Methods\ShopStatus::URI, [], [], $this->httpAuth);
  132.         $data = json_decode($re->getContent(), true);
  133.  
  134.         $commission = $giveAmount + ($giveAmount * $this->shop->commission / 100);
  135.  
  136.         $this->assertEquals($startAmount - $commission, $data['points'][0]['points'], 'Balance update');
  137.     }
  138.  
  139.     public function testSpendPoints(){
  140.         $this->shop->addPoints(95, Currency::EUR);
  141.  
  142.  
  143.         $client = $this->getNewClient();
  144.  
  145.         $client->addPoints(105, Currency::EUR);
  146.  
  147.         /*
  148.          * User reserves points for shop
  149.          */
  150.         $clientTrans = $this->transRepo->clientReserve($this->shop->id, $client->id, 5, Currency::EUR);
  151.  
  152.         $this->assertEquals(\Transaction\Base::STATUS_CLIENT_RESERVED, $clientTrans->status);
  153.  
  154.         /*
  155.          * Shop reserves
  156.          */
  157.         $re = $this->call(\PB\Api\Methods\ShopStartTransaction::METHOD, '/api/v1/' . \PB\Api\Methods\ShopStartTransaction::URI, [
  158.             'email' => $client->email,
  159.             'currency' => $clientTrans->currency,
  160.             'points' => $clientTrans->amount,
  161.             'order_id' => 1,
  162.         ], [], $this->httpAuth);
  163.  
  164.         $this->assertTrue($re->isOk());
  165.         $shopTransId = json_decode($re->getContent(), true)['transaction']['transactionId'];
  166.         $this->assertEquals($clientTrans->id, $shopTransId);
  167.  
  168.         /*
  169.          * Check if status correct
  170.          */
  171.         $transaction = $this->transRepo->getTransaction($this->shop->id, $shopTransId);
  172.         $this->assertEquals(\Transaction\Base::STATUS_SHOP_RESERVED, $transaction->status);
  173.  
  174.         /*
  175.          * Finish shop reserve, transfer
  176.          */
  177.         $re = $this->call(\PB\Api\Methods\ShopFinishTransaction::METHOD, '/api/v1/' . \PB\Api\Methods\ShopFinishTransaction::URI, [
  178.             'transactionId' => $transaction->id,
  179.         ], [], $this->httpAuth);
  180.  
  181.         $this->assertTrue($re->isOk());
  182.  
  183.         $data = json_decode($re->getContent(), true);
  184.         $this->assertEquals(\Transaction\Base::STATUS_COMPLETED, $data['transaction']['status']);
  185.  
  186.         /*
  187.          * Check if shop received points
  188.          */
  189.         $this->assertEquals(100, $this->shop->pointAmount(Currency::EUR));
  190.  
  191.         /*
  192.          * Check if user has the right balance
  193.          */
  194.         $this->assertEquals(100, $client->pointAmount(Currency::EUR));
  195.     }
  196.  
  197.     /**
  198.      * @expectedException \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
  199.      */
  200.     public function testShopReserveNonExistingUser(){
  201.         $client = $this->getNewClient();
  202.  
  203.         /*
  204.          * Try to reserve points when client didn't reserve points for shop
  205.          */
  206.         $this->call(\PB\Api\Methods\ShopStartTransaction::METHOD, '/api/v1/' . \PB\Api\Methods\ShopStartTransaction::URI, [
  207.             'email' => $client->email,
  208.             'currency' => Currency::EUR,
  209.             'points' => 1,
  210.             'order_id' => 1
  211.         ], [], $this->httpAuth);
  212.     }
  213.  
  214.     /**
  215.      * @expectedException \Symfony\Component\HttpKernel\Exception\HttpException
  216.      * @expectedExceptionMessage Amount over allowed limit
  217.      */
  218.     public function testShopReserveMoreThanAllowed(){
  219.         $client = $this->getNewClient();
  220.  
  221.         $client->addPoints(10, Currency::EUR);
  222.  
  223.         $this->transRepo->clientReserve($this->shop->id, $client->id, 10, Currency::EUR);
  224.  
  225.         /*
  226.          * Try to reserve points when client didn't reserve points for shop
  227.          */
  228.         $this->call(\PB\Api\Methods\ShopStartTransaction::METHOD, '/api/v1/' . \PB\Api\Methods\ShopStartTransaction::URI, [
  229.             'email' => $client->email,
  230.             'currency' => Currency::EUR,
  231.             'points' => 20,
  232.             'order_id' => 1,
  233.         ], [], $this->httpAuth);
  234.     }
  235.  
  236.     public function testShopReserveWithPartialRefund(){
  237.  
  238.         $client = $this->getNewClient();
  239.         $client->addPoints(100, Currency::EUR);
  240.  
  241.         $transaction = $this->transRepo->clientReserve($this->shop->id, $client->id, 100, Currency::EUR);
  242.  
  243.         /*
  244.          * Try to reserve less points than client reserved
  245.          */
  246.         $re = $this->call(\PB\Api\Methods\ShopStartTransaction::METHOD, '/api/v1/' . \PB\Api\Methods\ShopStartTransaction::URI, [
  247.             'email' => $client->email,
  248.             'currency' => Currency::EUR,
  249.             'points' => 60,
  250.             'order_id' => 1,
  251.         ], [], $this->httpAuth);
  252.  
  253.         $this->assertTrue($re->isOk());
  254.  
  255.         /*
  256.          * Finish transaction
  257.          */
  258.         $re = $this->call(\PB\Api\Methods\ShopFinishTransaction::METHOD, '/api/v1/' . \PB\Api\Methods\ShopFinishTransaction::URI, [
  259.             'transactionId' => $transaction->id,
  260.         ], [], $this->httpAuth);
  261.  
  262.         $this->assertTrue($re->isOk());
  263.  
  264.         /*
  265.          * Check if shop has correct amount of points
  266.          */
  267.         $this->assertEquals(60, $this->shop->pointAmount(Currency::EUR));
  268.  
  269.         /*
  270.          * Check if user has been partially refunded
  271.          */
  272.         $this->assertEquals(40, $client->pointAmount(Currency::EUR));
  273.  
  274.         /*
  275.          * Check if transaction is completed
  276.          */
  277.         $transaction = $this->transRepo->getTransaction($this->shop->id, $transaction->id);
  278.         $this->assertTrue($transaction->isCompleted);
  279.     }
  280.  
  281.     public function testClientReservedTransaction(){
  282.         $client = $this->clientRepo->getOrCreateClient('test@test.com', null);
  283.         $client->addPoints(100, Currency::EUR);
  284.  
  285.         $transaction = $this->transRepo->clientReserve($this->shop->id, $client->id, 100, Currency::EUR);
  286.  
  287.         $re = $this->call(\PB\Api\Methods\ClientReservedPoints::METHOD, '/api/v1/' . \PB\Api\Methods\ClientReservedPoints::URI, [
  288.             'email' => $client->email,
  289.             'currency' => Currency::EUR,
  290.         ], [], $this->httpAuth);
  291.  
  292.         $this->assertTrue($re->isOk());
  293.  
  294.         $data = json_decode($re->getContent(), true);
  295.         $this->assertEquals(\Transaction\Base::STATUS_CLIENT_RESERVED, $data['transaction']['status']);
  296.         $this->assertEquals($transaction->id, $data['transaction']['transactionId']);
  297.  
  298.         $this->transRepo->shopReserve($this->shop->id, $client->id, 100, Currency::EUR);
  299.  
  300.         // Transaction should not be found, because it was reserved by shop
  301.         try{
  302.             $this->call(\PB\Api\Methods\ClientReservedPoints::METHOD, '/api/v1/' . \PB\Api\Methods\ClientReservedPoints::URI, [
  303.                 'email' => $client->email,
  304.                 'currency' => Currency::EUR,
  305.             ], [], $this->httpAuth);
  306.         }catch(Exception $e){
  307.             $this->assertTrue($e instanceof \Symfony\Component\HttpKernel\Exception\NotFoundHttpException);
  308.         }
  309.     }
  310. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement