/** * @param User $user * @param string $name * * @return ApiToken */ public function getOrCreate(User $user, $name) { $tokens = $user->getApiTokens(); /** @var ApiToken $token */ foreach ($tokens as $token) { if ($token->getName() === $name) { return $token; } } return $this->create($user, $name); }
public function testGetOrCreate() { $registry = M::mock(ManagerRegistry::class); $user = new User(); $token1 = new ApiToken(); $token1->setName('token1'); $token2 = new ApiToken(); $token2->setName('token2'); $user->setApiTokens([$token1, $token2]); $manager = M::mock(ObjectManager::class); $registry->shouldReceive('getManager')->withNoArgs()->andReturn($manager); $manager->shouldReceive('persist')->withAnyArgs()->andReturn(); $manager->shouldReceive('flush')->withNoArgs(); $tm = new ApiTokenManager($registry); $token = $tm->getOrCreate($user, 'token3'); $this->assertInstanceOf(ApiToken::class, $token); $this->assertNotSame($token1, $token); $this->assertNotSame($token2, $token); $this->assertSame($user, $token->getUser()); $this->assertSame('token3', $token->getName()); }
/** * @param array $settings * @dataProvider settingsDataProvider */ public function testSaveSettings(array $settings) { $doctrine = $this->container->get('doctrine'); $manager = $doctrine->getManager(); $user = new User(); $user->setEmail('*****@*****.**')->setMobile('+1234567890')->setUsername('admin')->setPassword('admin'); /* @var EntityRepository $setting */ $settingsRepo = $manager->getRepository('CSBillSettingsBundle:Setting'); $settingsRepo->createQueryBuilder('s')->update()->set('s.value', ':value')->where('s.key = :key')->setParameter('key', 'room_id')->setParameter('value', 12345)->getQuery()->execute(); $settingsRepo->createQueryBuilder('s')->update()->set('s.value', ':value')->where('s.key = :key')->setParameter('key', 'auth_token')->setParameter('value', 'abcdef')->getQuery()->execute(); $manager->persist($user); $manager->flush(); $settingsLoader = new Loader($doctrine); $settingsLoader->saveSettings($settings); $allSettings = $doctrine->getManager()->getRepository('CSBillNotificationBundle:Notification')->findAll(); /** @var Notification $setting */ foreach ($allSettings as $setting) { $this->assertFalse($setting->getEmail()); $this->assertTrue($setting->getHipchat()); $this->assertTrue($setting->getSms()); } }
/** * @param User $user * * @return array */ public static function user(User $user) { $username = $user->getUsername() . ' <b class="caret"></b>'; return ['user', ['uri' => '#', 'allow_safe_labels' => true, 'label' => $username, 'extras' => ['safe_label' => true, 'icon' => 'user']]]; }