Advertisement
Guest User

Untitled

a guest
Sep 20th, 2014
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.12 KB | None | 0 0
  1. <?php
  2.  
  3. class SocialTest extends TestCase{
  4.     /** @var \PB\Interfaces\SocialInterface */
  5.     public $social;
  6.  
  7.     /** @var \PB\Social\Connector */
  8.     public $connector;
  9.  
  10.     protected function prepareForTests(){
  11.         parent::prepareForTests();
  12.         $this->social = App::make('PB\Interfaces\SocialInterface');
  13.         $this->connector = App::make('PB\Social\Connector');
  14.     }
  15.  
  16.     public function testStoreRead(){
  17.         $user = $this->getNewUser();
  18.  
  19.         $item = new \PB\Social\types\Facebook;
  20.         $item->userId = $user->id;
  21.         $item->id = 123;
  22.         $item->firstName = 'Martin';
  23.         $item->lastName = 'Briedis';
  24.  
  25.         $this->social->store($item);
  26.  
  27.  
  28.         $byUserId = $this->social->getByUserId($item->userId);
  29.  
  30.         $this->assertEquals($item::TYPE, $byUserId[$item::TYPE]::TYPE);
  31.         $this->assertEquals($item->id, $byUserId[$item::TYPE]->id);
  32.         $this->assertEquals($item->userId, $byUserId[$item::TYPE]->userId);
  33.  
  34.         $byType = $this->social->getBySocial($item::TYPE, $item->id);
  35.         $this->assertEquals($byType->userId, $item->userId);
  36.  
  37.         // Update
  38.         $item->firstName = 'edited';
  39.         $this->social->store($item);
  40.  
  41.         $newItem = $this->social->getBySocial($item::TYPE, $item->id);
  42.         $this->assertEquals($item->firstName, $newItem->firstName);
  43.     }
  44.  
  45.     public function testNewUserFromSocial(){
  46.         $item = new \PB\Social\types\Facebook();
  47.         $item->id = 1000;
  48.         $item->email = uniqid() . '@test.com';
  49.  
  50.         $connectResult = $this->connector->connect($item);
  51.  
  52.         $this->assertEquals(true, $connectResult, 'Successfully connected');
  53.         $this->assertTrue($item->getUser() instanceof User, 'New user has been created');
  54.         $this->assertEquals($item->getUser()->id, Sentry::getUser()->getId(), 'User logged in');
  55.         $this->assertTrue($item->getUser()->isClient, 'Must be client by default');
  56.  
  57.         $item2 = $this->social->getByUserIdType($item->userId, $item::TYPE);
  58.  
  59.         $this->assertEquals($item2->id, $item->id, 'Connection stored');
  60.     }
  61.  
  62.     public function testExistingLogin(){
  63.         $user = $this->getNewUser();
  64.  
  65.         $item = new \PB\Social\types\Facebook;
  66.         $item->id = 10000;
  67.         $item->userId = $user->id;
  68.         $item->email = $user->email;
  69.  
  70.         $this->social->store($item);
  71.  
  72.         $item2 = new \PB\Social\types\Facebook;
  73.         $item2->id = $item->id;
  74.         $item2->email = $user->email;
  75.         $connectResult = $this->connector->connect($item2);
  76.  
  77.         $this->assertTrue($connectResult);
  78.  
  79.         $this->assertEquals($user->id, Sentry::getUser()->getId());
  80.     }
  81.  
  82.     public function testExistingUserConnect(){
  83.         $user = $this->getNewUser();
  84.         Sentry::login($user);
  85.  
  86.         $item = new \PB\Social\types\Facebook;
  87.         $item->userId = $user->id;
  88.         $item->id = time();
  89.         $item->email = 'whatever@test.com';
  90.  
  91.         $connectResult = $this->connector->connect($item);
  92.  
  93.         $this->assertTrue($connectResult);
  94.  
  95.         $fromDb = $this->social->getByUserIdType($user->id, $item::TYPE);
  96.  
  97.         $this->assertEquals($user->id, $fromDb->userId);
  98.     }
  99.  
  100.  
  101.     public function testUserWithEmailAlreadyRegistered(){
  102.         $user = $this->getNewUser(uniqid("", true) . '@test.lv');
  103.  
  104.         $item = new \PB\Social\types\Facebook;
  105.         $item->id = microtime();
  106.         $item->email = $user->email;
  107.  
  108.         $this->setExpectedException('Cartalyst\Sentry\Users\UserExistsException');
  109.         $this->connector->connect($item);
  110.     }
  111.  
  112.     public function testReceivePointsThenRegisterSocialAccount(){
  113.         /** @var \PB\Interfaces\TransactionInterface $transactions */
  114.         $transactions = App::make('PB\Interfaces\TransactionInterface');
  115.         /** @var \PB\Interfaces\ClientInterface $clients */
  116.         $clients = App::make('PB\Interfaces\ClientInterface');
  117.  
  118.         $email = uniqid() . '@test.com';
  119.  
  120.         $shop = $this->getNewShop();
  121.         $client = $clients->getOrCreateClient($email, 0);
  122.  
  123.         $points = 15;
  124.  
  125.         $shop->addPoints($points, Currency::EUR);
  126.         $transactions->givePoints($client->id, $shop->id, $points, Currency::EUR);
  127.  
  128.         // Now create a new account
  129.  
  130.         $item = new \PB\Social\types\Facebook;
  131.         $item->email = $email;
  132.         $item->id = 12345;
  133.  
  134.         $re = $this->connector->connect($item);
  135.  
  136.         $this->assertTrue($re);
  137.  
  138.         /** @var User $user */
  139.         $user = Sentry::getUser();
  140.  
  141.         $this->assertTrue($user->isClient);
  142.         $this->assertEquals($user->client->pointAmount(Currency::EUR) - Client::REGISTRATION_BONUS, $points);
  143.     }
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement