Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class ApiTest extends TestCase{
- /** @var Shop $shop */
- private $shop;
- /** @var \PB\Interfaces\ApiInterface */
- private $apiRepo;
- /** @var \PB\Interfaces\ShopInterface */
- private $shopRepo;
- /** @var \PB\Interfaces\ClientInterface */
- private $clientRepo;
- /** @var \PB\Interfaces\TransactionInterface */
- private $transRepo;
- private $httpAuth = [];
- protected function prepareForTests(){
- parent::prepareForTests();
- $this->apiRepo = App::make('PB\Interfaces\ApiInterface');
- $this->shopRepo = App::make('PB\Interfaces\ShopInterface');
- $this->clientRepo = App::make('PB\Interfaces\ClientInterface');
- $this->transRepo = App::make('PB\Interfaces\TransactionInterface');
- $this->shop = $this->getNewShop();
- $this->shop->commission = 25; //25% commission when giving points
- $this->shop->is_active = true;
- $this->shop->save();
- $this->httpAuth = [
- 'PHP_AUTH_USER' => $this->shop->id,
- 'PHP_AUTH_PW' => $this->shop->api_key,
- ];
- // Filter is used for authorization
- Route::enableFilters();
- }
- /**
- * @expectedException \Symfony\Component\HttpKernel\Exception\HttpException
- * @expectedExceptionMessage Please provide a valid API key
- */
- public function testAuthWithoutCredentials(){
- $this->call(\PB\Api\Methods\ShopStatus::METHOD, '/api/v1/' . \PB\Api\Methods\ShopStatus::URI);
- }
- public function testAuthSuccess(){
- $re = $this->call(\PB\Api\Methods\ShopStatus::METHOD, '/api/v1/' . \PB\Api\Methods\ShopStatus::URI, [], [], $this->httpAuth);
- $this->assertEquals(200, $re->getStatusCode());
- }
- /**
- * @expectedException \Symfony\Component\HttpKernel\Exception\HttpException
- * @expectedExceptionMessage User or API key not found
- */
- public function testAuthBadCredentials(){
- $this->call(\PB\Api\Methods\ShopStatus::METHOD, '/api/v1/' . \PB\Api\Methods\ShopStatus::URI, [], [], [
- 'PHP_AUTH_USER' => $this->shop->id,
- 'PHP_AUTH_PW' => 'bad api key',
- ]);
- }
- public function testGivePointsStatus(){
- $giveAmount = 130;
- $startAmount = 200;
- $email = uniqid() . '@test.com';
- $this->shop->addPoints($startAmount, Currency::EUR);
- /*
- * Test initial status
- */
- $re = $this->call(\PB\Api\Methods\ShopStatus::METHOD, '/api/v1/' . \PB\Api\Methods\ShopStatus::URI, [], [], $this->httpAuth);
- $this->assertTrue($re->isOk());
- $data = json_decode($re->getContent(), true);
- $this->assertEquals($startAmount, $data['points'][0]['points']);
- $this->assertEquals(Currency::EUR, $data['points'][0]['currency']);
- /*
- * Test give points to user
- */
- $re = $this->call(\PB\Api\Methods\GivePoints::METHOD, '/api/v1/' . \PB\Api\Methods\GivePoints::URI, [
- 'email' => $email,
- 'currency' => Currency::EUR,
- 'points' => $giveAmount,
- 'phone' => '37120000000',
- 'order_id' => 1,
- ], [], $this->httpAuth);
- $this->assertTrue($re->isOk());
- $data = json_decode($re->getContent(), true);
- $this->assertEquals(\Transaction\Base::STATUS_COMPLETED, $data['transaction']['status']);
- $this->assertEquals($giveAmount, $data['transaction']['points'], 'Equal after commission');
- $transactionId = $data['transaction']['transactionId'];
- /*
- * Test for transaction status
- */
- $re = $this->call(\PB\Api\Methods\Transaction::METHOD, '/api/v1/' . \PB\Api\Methods\Transaction::URI, [
- 'id' => $transactionId
- ], [], $this->httpAuth);
- $this->assertTrue($re->isOk());
- $data = json_decode($re->getContent(), true);
- $this->assertEquals(\Transaction\Base::STATUS_COMPLETED, $data['transaction']['status']);
- /*
- * Test if user received points
- */
- $client = $this->clientRepo->getClientByMail($email);
- $this->assertEquals($giveAmount, $client->pointAmount(Currency::EUR));
- /*
- * Test if shops balance is updated
- */
- $re = $this->call(\PB\Api\Methods\ShopStatus::METHOD, '/api/v1/' . \PB\Api\Methods\ShopStatus::URI, [], [], $this->httpAuth);
- $data = json_decode($re->getContent(), true);
- $commission = $giveAmount + ($giveAmount * $this->shop->commission / 100);
- $this->assertEquals($startAmount - $commission, $data['points'][0]['points'], 'Balance update');
- }
- public function testSpendPoints(){
- $this->shop->addPoints(95, Currency::EUR);
- $client = $this->getNewClient();
- $client->addPoints(105, Currency::EUR);
- /*
- * User reserves points for shop
- */
- $clientTrans = $this->transRepo->clientReserve($this->shop->id, $client->id, 5, Currency::EUR);
- $this->assertEquals(\Transaction\Base::STATUS_CLIENT_RESERVED, $clientTrans->status);
- /*
- * Shop reserves
- */
- $re = $this->call(\PB\Api\Methods\ShopStartTransaction::METHOD, '/api/v1/' . \PB\Api\Methods\ShopStartTransaction::URI, [
- 'email' => $client->email,
- 'currency' => $clientTrans->currency,
- 'points' => $clientTrans->amount,
- 'order_id' => 1,
- ], [], $this->httpAuth);
- $this->assertTrue($re->isOk());
- $shopTransId = json_decode($re->getContent(), true)['transaction']['transactionId'];
- $this->assertEquals($clientTrans->id, $shopTransId);
- /*
- * Check if status correct
- */
- $transaction = $this->transRepo->getTransaction($this->shop->id, $shopTransId);
- $this->assertEquals(\Transaction\Base::STATUS_SHOP_RESERVED, $transaction->status);
- /*
- * Finish shop reserve, transfer
- */
- $re = $this->call(\PB\Api\Methods\ShopFinishTransaction::METHOD, '/api/v1/' . \PB\Api\Methods\ShopFinishTransaction::URI, [
- 'transactionId' => $transaction->id,
- ], [], $this->httpAuth);
- $this->assertTrue($re->isOk());
- $data = json_decode($re->getContent(), true);
- $this->assertEquals(\Transaction\Base::STATUS_COMPLETED, $data['transaction']['status']);
- /*
- * Check if shop received points
- */
- $this->assertEquals(100, $this->shop->pointAmount(Currency::EUR));
- /*
- * Check if user has the right balance
- */
- $this->assertEquals(100, $client->pointAmount(Currency::EUR));
- }
- /**
- * @expectedException \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
- */
- public function testShopReserveNonExistingUser(){
- $client = $this->getNewClient();
- /*
- * Try to reserve points when client didn't reserve points for shop
- */
- $this->call(\PB\Api\Methods\ShopStartTransaction::METHOD, '/api/v1/' . \PB\Api\Methods\ShopStartTransaction::URI, [
- 'email' => $client->email,
- 'currency' => Currency::EUR,
- 'points' => 1,
- 'order_id' => 1
- ], [], $this->httpAuth);
- }
- /**
- * @expectedException \Symfony\Component\HttpKernel\Exception\HttpException
- * @expectedExceptionMessage Amount over allowed limit
- */
- public function testShopReserveMoreThanAllowed(){
- $client = $this->getNewClient();
- $client->addPoints(10, Currency::EUR);
- $this->transRepo->clientReserve($this->shop->id, $client->id, 10, Currency::EUR);
- /*
- * Try to reserve points when client didn't reserve points for shop
- */
- $this->call(\PB\Api\Methods\ShopStartTransaction::METHOD, '/api/v1/' . \PB\Api\Methods\ShopStartTransaction::URI, [
- 'email' => $client->email,
- 'currency' => Currency::EUR,
- 'points' => 20,
- 'order_id' => 1,
- ], [], $this->httpAuth);
- }
- public function testShopReserveWithPartialRefund(){
- $client = $this->getNewClient();
- $client->addPoints(100, Currency::EUR);
- $transaction = $this->transRepo->clientReserve($this->shop->id, $client->id, 100, Currency::EUR);
- /*
- * Try to reserve less points than client reserved
- */
- $re = $this->call(\PB\Api\Methods\ShopStartTransaction::METHOD, '/api/v1/' . \PB\Api\Methods\ShopStartTransaction::URI, [
- 'email' => $client->email,
- 'currency' => Currency::EUR,
- 'points' => 60,
- 'order_id' => 1,
- ], [], $this->httpAuth);
- $this->assertTrue($re->isOk());
- /*
- * Finish transaction
- */
- $re = $this->call(\PB\Api\Methods\ShopFinishTransaction::METHOD, '/api/v1/' . \PB\Api\Methods\ShopFinishTransaction::URI, [
- 'transactionId' => $transaction->id,
- ], [], $this->httpAuth);
- $this->assertTrue($re->isOk());
- /*
- * Check if shop has correct amount of points
- */
- $this->assertEquals(60, $this->shop->pointAmount(Currency::EUR));
- /*
- * Check if user has been partially refunded
- */
- $this->assertEquals(40, $client->pointAmount(Currency::EUR));
- /*
- * Check if transaction is completed
- */
- $transaction = $this->transRepo->getTransaction($this->shop->id, $transaction->id);
- $this->assertTrue($transaction->isCompleted);
- }
- public function testClientReservedTransaction(){
- $client = $this->clientRepo->getOrCreateClient('test@test.com', null);
- $client->addPoints(100, Currency::EUR);
- $transaction = $this->transRepo->clientReserve($this->shop->id, $client->id, 100, Currency::EUR);
- $re = $this->call(\PB\Api\Methods\ClientReservedPoints::METHOD, '/api/v1/' . \PB\Api\Methods\ClientReservedPoints::URI, [
- 'email' => $client->email,
- 'currency' => Currency::EUR,
- ], [], $this->httpAuth);
- $this->assertTrue($re->isOk());
- $data = json_decode($re->getContent(), true);
- $this->assertEquals(\Transaction\Base::STATUS_CLIENT_RESERVED, $data['transaction']['status']);
- $this->assertEquals($transaction->id, $data['transaction']['transactionId']);
- $this->transRepo->shopReserve($this->shop->id, $client->id, 100, Currency::EUR);
- // Transaction should not be found, because it was reserved by shop
- try{
- $this->call(\PB\Api\Methods\ClientReservedPoints::METHOD, '/api/v1/' . \PB\Api\Methods\ClientReservedPoints::URI, [
- 'email' => $client->email,
- 'currency' => Currency::EUR,
- ], [], $this->httpAuth);
- }catch(Exception $e){
- $this->assertTrue($e instanceof \Symfony\Component\HttpKernel\Exception\NotFoundHttpException);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement