Skip to content

Commit

Permalink
Merge pull request #52 from miguelcleverti/FixRandomNumberGeneration
Browse files Browse the repository at this point in the history
Improve unique genarated values for names and identifiers
  • Loading branch information
andrerom authored Nov 11, 2016
2 parents 002a004 + f72b6dc commit 1d90397
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 75 deletions.
45 changes: 7 additions & 38 deletions Context/Object/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,17 +135,6 @@ public function iHaveUserWithId( $id )
$this->addValuesToKeyMap( $id, $user->id );
}

/**
* @Given there isn't a User with id :id
*
*Makes user a user with (mapped) id ':id' does not exist
*/
public function iDontHaveUserWithId( $id )
{
$randomId = $this->findNonExistingUserId();
$this->addValuesToKeyMap( $id, $randomId );
}

/**
* @Given there is a User with name :username with id :id in :parentGroup group
*
Expand Down Expand Up @@ -314,16 +303,17 @@ public function assertUserWithNameExistsWithFields( $username, TableNode $table
*/
private function findNonExistingUserEmail( $username = 'User' )
{
$userManager = $this->getUserManager();
$email = "${username}@ez.no";
if ( $this->getUserManager()->checkUserExistenceByEmail( $email ) )
if ( $userManager->checkUserExistenceByEmail( $email ) )
{
return $email;
}

for ( $i = 0; $i < 20; $i++ )
{
$email = 'User#' . rand( 1000, 9999 ) . "@ez.no";
if ( !$this->getUserManager()->checkUserExistenceByEmail( $email ) )
$email = 'User#'. uniqid() . '@ez.no';
if ( !$userManager->checkUserExistenceByEmail( $email ) )
{
return $email;
}
Expand All @@ -341,37 +331,16 @@ private function findNonExistingUserEmail( $username = 'User' )
*/
private function findNonExistingUserName()
{
$userManager = $this->getUserManager();
for ( $i = 0; $i < 20; $i++ )
{
$username = 'User#' . rand( 1000, 9999 );
if ( !$this->getUserManager()->checkUserExistenceByUsername( $username ) )
$username = 'User#'. uniqid();
if ( !$userManager->checkUserExistenceByUsername( $username ) )
{
return $username;
}
}

throw new \Exception( 'Possible endless loop when attempting to find a new name for User.' );
}

/**
* Find an non existent User id
*
* @return int Non existing id
*
* @throws \Exception Possible endless loop
*/
private function findNonExistingUserId()
{
for ( $i = 0; $i < 20; $i++ )
{
$id = rand( 1000, 9999 );
if ( !$this->getUserManager()->checkUserExistenceById( $id ) )
{
return $id;
}
}

throw new \Exception( 'Possible endless loop when attempting to find a new id for User.' );
}

}
38 changes: 3 additions & 35 deletions Context/Object/UserGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,18 +76,6 @@ public function iHaveUserGroupWithId( $id )
$this->addValuesToKeyMap( $id, $userGroup->id );
}

/**
* @Given there isn't a User Group with id :id
*
* Maps id ':id' to a non-existent user group.
*/
public function iDontHaveUserGroupWithId( $id )
{
$randomId = $this->findNonExistingUserGroupId();

$this->addValuesToKeyMap( $id, $randomId );
}

/**
* @Given there is a User Group with name :name with id :id in :parentGroup group
*
Expand Down Expand Up @@ -184,36 +172,16 @@ public function assertUserGroupWithNameDoesntExistInGroup( $name, $parentGroup )
*/
private function findNonExistingUserGroupId()
{
$userGoupManager = $this->getUserGroupManager();
for ( $i = 0; $i < 20; $i++ )
{
$id = rand( 1000, 9999 );
if ( !$this->getUserGroupManager()->checkUserGroupExistence( $id ) )
$id = uniqid();
if ( !$userGroupManager()->checkUserGroupExistence( $id ) )
{
return $id;
}
}

throw new \Exception( 'Possible endless loop when attempting to find a new id for UserGroup.' );
}

/**
* Find a non existing UserGroup name
*
* @return string A not used name
*
* @throws \Exception Possible endless loop
*/
private function findNonExistingUserGroupName()
{
for ( $i = 0; $i < 20; $i++ )
{
$name = 'UserGroup#' . rand( 1000, 9999 );
if ( !$this->getUserGroupManager()->checkUserGroupExistenceByName( $name ) )
{
return $name;
}
}

throw new \Exception( 'Possible endless loop when attempting to find a new name for UserGroup.' );
}
}
3 changes: 1 addition & 2 deletions ObjectManager/FieldType.php
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,6 @@ public function getThisContentId()
return $this->fieldConstructionObject[ 'content' ]->id;
}


/**
* Creates an instance of a contenttype and stores it for later publishing
*/
Expand All @@ -315,7 +314,7 @@ private function createContentType()
$repository = $this->getRepository();
$contentTypeService = $repository->getContentTypeService();
$name = $this->fieldConstructionObject[ 'fieldType' ]->identifier;
$name .= "#" . rand( 1000, 9000 );
$name = $name . '#' . uniqid();
$identifier = strtolower( $name );
$contentTypeCreateStruct = $contentTypeService->newContentTypeCreateStruct( $identifier );
$contentTypeCreateStruct->mainLanguageCode = self::DEFAULT_LANGUAGE;
Expand Down

0 comments on commit 1d90397

Please sign in to comment.