From cef6605b75a2548cdd87dccc4b8ecf142f09fa7f Mon Sep 17 00:00:00 2001 From: epriestley Date: Mon, 22 Oct 2012 16:25:00 -0700 Subject: [PATCH] Add a generateNewTestUser() method to PhabricatorTestCase Summary: I need this shortly and it seems like something we're likely to need more of in the future now that fixtures work. Test Plan: Ran unit tests. Used this productively in an upcoming diff. Reviewers: btrahan, vrana Reviewed By: vrana CC: aran Differential Revision: https://secure.phabricator.com/D3775 --- .../testing/PhabricatorTestCase.php | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/infrastructure/testing/PhabricatorTestCase.php b/src/infrastructure/testing/PhabricatorTestCase.php index 51acaf71fd..ca614f4cfb 100644 --- a/src/infrastructure/testing/PhabricatorTestCase.php +++ b/src/infrastructure/testing/PhabricatorTestCase.php @@ -51,6 +51,7 @@ abstract class PhabricatorTestCase extends ArcanistPhutilTestCase { private static $storageFixtureReferences = 0; private static $storageFixture; + private static $storageFixtureObjectSeed = 0; protected function getPhabricatorTestCaseConfiguration() { return array(); @@ -144,4 +145,34 @@ abstract class PhabricatorTestCase extends ArcanistPhutilTestCase { '&jump=true&context='.get_class($this); } + /** + * Returns an integer seed to use when building unique identifiers (e.g., + * non-colliding usernames). The seed is unstable and its value will change + * between test runs, so your tests must not rely on it. + * + * @return int A unique integer. + */ + protected function getNextObjectSeed() { + self::$storageFixtureObjectSeed += mt_rand(1, 100); + return self::$storageFixtureObjectSeed; + } + + protected function generateNewTestUser() { + $seed = $this->getNextObjectSeed(); + + $user = id(new PhabricatorUser()) + ->setRealName("Test User {$seed}}") + ->setUserName("test{$seed}"); + + $email = id(new PhabricatorUserEmail()) + ->setAddress("testuser{$seed}@example.com") + ->setIsVerified(1); + + $editor = new PhabricatorUserEditor(); + $editor->setActor($user); + $editor->createNewUser($user, $email); + + return $user; + } + }