Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Provide an interface for the random string generator #2

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions spec/Drupal/Driver/Cores/Drupal7Spec.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@

namespace spec\Drupal\Driver\Cores;

use Drupal\Component\Utility\Random;

use Drupal\Component\Utility\RandomInterface;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;

class Drupal7Spec extends ObjectBehavior
{
function let(Random $random)
function let(RandomInterface $random)
{
$this->beConstructedWith('path', 'http://www.example.com', $random);
}
Expand Down
4 changes: 2 additions & 2 deletions spec/Drupal/Driver/Cores/Drupal8Spec.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

namespace spec\Drupal\Driver\Cores;

use Drupal\Component\Utility\Random;
use Drupal\Component\Utility\RandomInterface;

use PhpSpec\ObjectBehavior;
use Prophecy\Argument;

class Drupal8Spec extends ObjectBehavior
{
function let(Random $random)
function let(RandomInterface $random)
{
$this->beConstructedWith('path', 'http://www.example.com', $random);
}
Expand Down
49 changes: 4 additions & 45 deletions src/Drupal/Component/Utility/Random.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
/**
* Defines a utility class for creating random data.
*/
class Random {
class Random implements RandomInterface {

/**
* The maximum number of times name() and string() can loop.
Expand All @@ -37,24 +37,7 @@ class Random {
protected $names = array();

/**
* Generates a random string of ASCII characters of codes 32 to 126.
*
* The generated string includes alpha-numeric characters and common
* miscellaneous characters. Use this method when testing general input
* where the content is not restricted.
*
* @param int $length
* Length of random string to generate.
* @param bool $unique
* (optional) If TRUE ensures that the random string returned is unique.
* Defaults to FALSE.
* @param callable $validator
* (optional) A callable to validate the the string. Defaults to NULL.
*
* @return string
* Randomly generated string.
*
* @see \Drupal\Component\Utility\Random::name()
* {@inheritdoc}
*/
public function string($length = 8, $unique = FALSE, $validator = NULL) {
$counter = 0;
Expand Down Expand Up @@ -91,24 +74,7 @@ public function string($length = 8, $unique = FALSE, $validator = NULL) {
}

/**
* Generates a random string containing letters and numbers.
*
* The string will always start with a letter. The letters may be upper or
* lower case. This method is better for restricted inputs that do not
* accept certain characters. For example, when testing input fields that
* require machine readable values (i.e. without spaces and non-standard
* characters) this method is best.
*
* @param int $length
* Length of random string to generate.
* @param bool $unique
* (optional) If TRUE ensures that the random string returned is unique.
* Defaults to FALSE.
*
* @return string
* Randomly generated string.
*
* @see \Drupal\Component\Utility\Random::string()
* {@inheritdoc}
*/
public function name($length = 8, $unique = FALSE) {
$values = array_merge(range(65, 90), range(97, 122), range(48, 57));
Expand All @@ -134,14 +100,7 @@ public function name($length = 8, $unique = FALSE) {
}

/**
* Generates a random PHP object.
*
* @param int $size
* The number of random keys to add to the object.
*
* @return \stdClass
* The generated object, with the specified number of random keys. Each key
* has a random string value.
* {@inheritdoc}
*/
public function object($size = 4) {
$object = new \stdClass();
Expand Down
71 changes: 71 additions & 0 deletions src/Drupal/Component/Utility/RandomInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

/**
* @file
* Contains \Drupal\Component\Utility\RandomInterface.
*/

namespace Drupal\Component\Utility;

/**
* Defines a utility class for creating random data.
*/
interface RandomInterface {

/**
* Generates a random string of ASCII characters of codes 32 to 126.
*
* The generated string includes alpha-numeric characters and common
* miscellaneous characters. Use this method when testing general input
* where the content is not restricted.
*
* @param int $length
* Length of random string to generate.
* @param bool $unique
* (optional) If TRUE ensures that the random string returned is unique.
* Defaults to FALSE.
* @param callable $validator
* (optional) A callable to validate the the string. Defaults to NULL.
*
* @return string
* Randomly generated string.
*
* @see \Drupal\Component\Utility\Random::name()
*/
public function string($length = 8, $unique = FALSE, $validator = NULL);

/**
* Generates a random string containing letters and numbers.
*
* The string will always start with a letter. The letters may be upper or
* lower case. This method is better for restricted inputs that do not
* accept certain characters. For example, when testing input fields that
* require machine readable values (i.e. without spaces and non-standard
* characters) this method is best.
*
* @param int $length
* Length of random string to generate.
* @param bool $unique
* (optional) If TRUE ensures that the random string returned is unique.
* Defaults to FALSE.
*
* @return string
* Randomly generated string.
*
* @see \Drupal\Component\Utility\Random::string()
*/
public function name($length = 8, $unique = FALSE);

/**
* Generates a random PHP object.
*
* @param int $size
* The number of random keys to add to the object.
*
* @return \stdClass
* The generated object, with the specified number of random keys. Each key
* has a random string value.
*/
public function object($size = 4);

}
8 changes: 5 additions & 3 deletions src/Drupal/Driver/Cores/CoreInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Drupal\Driver\Cores;

use Drupal\Component\Utility\Random;
use Drupal\Component\Utility\RandomInterface;

/**
* Drupal core interface.
Expand All @@ -16,13 +16,15 @@ interface CoreInterface {
* @param string $uri
* URI that is accessing Drupal. Defaults to 'default'.
*
* @param \Drupal\Component\Utility\Random $random
* @param \Drupal\Component\Utility\RandomInterface $random
* Random string generator.
*/
public function __construct($drupalRoot, $uri = 'default', Random $random);
public function __construct($drupalRoot, $uri = 'default', RandomInterface $random);

/**
* Return random generator.
*
* @return \Drupal\Component\Utility\RandomInterface
*/
public function getRandom();

Expand Down
5 changes: 3 additions & 2 deletions src/Drupal/Driver/Cores/Drupal7.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Drupal\Driver\Cores;

use Drupal\Component\Utility\Random;
use Drupal\Component\Utility\RandomInterface;
use Drupal\Driver\Exception\BootstrapException;

/**
Expand All @@ -26,14 +27,14 @@ class Drupal7 implements CoreInterface {
/**
* Random generator.
*
* @var \Drupal\Component\Utility\Random
* @var \Drupal\Component\Utility\RandomInterface
*/
private $random;

/**
* {@inheritDoc}
*/
public function __construct($drupalRoot, $uri = 'default', Random $random) {
public function __construct($drupalRoot, $uri = 'default', RandomInterface $random) {
$this->drupalRoot = realpath($drupalRoot);
$this->uri = $uri;
$this->random = $random;
Expand Down
5 changes: 4 additions & 1 deletion src/Drupal/Driver/Cores/Drupal8.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Drupal\Driver\Cores;

use Drupal\Component\Utility\Random;
use Drupal\Component\Utility\RandomInterface;
use Drupal\Driver\Exception\BootstrapException;
use Drupal\node\Entity\Node;
use Drupal\node\NodeInterface;
Expand Down Expand Up @@ -36,9 +37,11 @@ class Drupal8 implements CoreInterface {
/**
* {@inheritDoc}
*/
public function __construct($drupalRoot, $uri = 'default', Random $random = NULL) {
public function __construct($drupalRoot, $uri = 'default', RandomInterface $random = NULL) {
$this->drupalRoot = realpath($drupalRoot);
$this->uri = $uri;

// @todo This should not be optional but injected by a factory method.
if (!isset($random)) {
$random = new Random();
}
Expand Down
2 changes: 2 additions & 0 deletions src/Drupal/Driver/DriverInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ interface DriverInterface {

/**
* Returns a random generator.
*
* @return \Drupal\Component\Utility\RandomInterface
*/
public function getRandom();

Expand Down
10 changes: 6 additions & 4 deletions src/Drupal/Driver/DrushDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Drupal\Driver;

use Drupal\Component\Utility\Random;
use Drupal\Component\Utility\RandomInterface;
use Drupal\Driver\Exception\BootstrapException;

use Symfony\Component\Process\Process;
Expand Down Expand Up @@ -41,7 +42,7 @@ class DrushDriver extends BaseDriver {
/**
* Random generator.
*
* @var \Drupal\Component\Utility\Random
* @var \Drupal\Component\Utility\RandomInterface
*/
private $random;

Expand All @@ -61,12 +62,12 @@ class DrushDriver extends BaseDriver {
* The root path of the Drupal install. This is an alternative to using aliases.
* @param string $binary
* The path to the drush binary.
* @param \Drupal\Component\Utility\Random $random
* @param \Drupal\Component\Utility\RandomInterface $random
* Random generator.
*
* @throws \Drupal\Driver\Exception\BootstrapException
*/
public function __construct($alias = NULL, $root_path = NULL, $binary = 'drush', Random $random = NULL) {
public function __construct($alias = NULL, $root_path = NULL, $binary = 'drush', RandomInterface $random = NULL) {
if (!empty($alias)) {
// Trim off the '@' symbol if it has been added.
$alias = ltrim($alias, '@');
Expand All @@ -82,6 +83,7 @@ public function __construct($alias = NULL, $root_path = NULL, $binary = 'drush',

$this->binary = $binary;

// @todo This should not be optional but injected by a factory method.
if (!isset($random)) {
$random = new Random();
}
Expand Down Expand Up @@ -209,7 +211,7 @@ protected static function parseArguments(array $arguments) {
$string .= ' --' . $name;
}
else {
$string .= '--' . $name . '=' . $value;
$string .= ' --' . $name . '=' . $value;
}
}
return $string;
Expand Down