Checking string for invalid format possibilities

33 views
Skip to first unread message

Jaime Isagani Sangcap

unread,
Jun 22, 2015, 5:06:10 AM6/22/15
to phpsp...@googlegroups.com
I am new to phpspec and I want to test my CourseCode class for number of invalid format possibilities.

I have this class


/**
* The course code
*/
final class CourseCode
{
/**
* Matches the range
* AA000 | ZZ999
* AA000-0 | ZZ999-9 | ZZ000-A | ZZ999-F
*/
const CODE_PATTERN = '/[A-Za-z]{2}[0-9]{3}-?[A-Za-z0-9]?^/';

/**
* @var string
*/
private $code;

/**
* The course code should match the CODE_PATTERN
*
* @param string $aCode The course code.
*/
public function __construct($aCode)
{
AssertionConcern::regex(
$aCode,
self::CODE_PATTERN,
sprintf(
'Code should match the pattern %s. Ex: AA000 | ZZ999 | AA000-0 | ZZ999-9 | ZZ000-A | ZZ999-F. %s given',
self::CODE_PATTERN,
$aCode
));

$this->code = $aCode;
}

/**
* @return string
*/
public function code()
{
return $this->code;
}
}

I want to test if my regex is working as expected.


How can I achieve this using phpspec?

Gonzalo Vilaseca

unread,
Jun 22, 2015, 5:17:53 AM6/22/15
to phpsp...@googlegroups.com
I would suggest something like this:

function it_validates_correct_code()
{
  $this->beConstructedWith('SomeValidCode');

  $this->code()->shouldReturn('SomeValidCode);
}

Repeat for as amny codes as you want to test.
I'm not familiar with AssertionConcerrn, but if it throws exceptions, then you should do something like:

$this->shouldThrow('\ExpectedException')->duringCode();

--
You received this message because you are subscribed to the Google Groups "PHPSpec - Behavior Driven Design (BDD) for PHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to phpspec-dev...@googlegroups.com.
To post to this group, send email to phpsp...@googlegroups.com.
Visit this group at http://groups.google.com/group/phpspec-dev.
For more options, visit https://groups.google.com/d/optout.

Jaime Isagani Sangcap

unread,
Jun 22, 2015, 6:33:46 AM6/22/15
to phpsp...@googlegroups.com
Thank you for the fast response.

I have now this test

function it_should_not_allow_invalid_course_code_format()
{
$this->beConstructedWith('LM001-');
$this->shouldThrow('\InvalidArgumentException')->duringInstantiation();

$this->beConstructedWith('LM1-');
$this->shouldThrow('\InvalidArgumentException')->duringInstantiation();

$this->beConstructedWith('LM001-AA');
$this->shouldThrow('\InvalidArgumentException')->duringInstantiation();

$this->beConstructedWith('LM0000');
$this->shouldThrow('\InvalidArgumentException')->duringInstantiation();

$this->beConstructedWith('LM00-1');
$this->shouldThrow('\InvalidArgumentException')->duringInstantiation();
}

Is this also good?

Gonzalo Vilaseca

unread,
Jun 22, 2015, 7:04:24 AM6/22/15
to phpsp...@googlegroups.com
No, you should split every case into different methods, also see how to check the constructor:

function it_should_not_allow_invalid_course_code_format_1()
{
   $this->shouldThrow('\InvalidArgumentException')->during('__construct', [LM001-]);
}

function it_should_not_allow_invalid_course_code_format_2()
{
   $this->shouldThrow('\InvalidArgumentException')->during('__construct', [LM1-]);
}

function it_should_not_allow_invalid_course_code_format_3()
{
   $this->shouldThrow('\InvalidArgumentException')->during('__construct', [LM001-AA]);
}

Jaime Isagani Sangcap

unread,
Jun 22, 2015, 8:34:17 AM6/22/15
to phpsp...@googlegroups.com
May I know what are the benefit of separting every possibiities instead of putting all together the related test on one function?

Gonzalo Vilaseca

unread,
Jun 22, 2015, 8:46:01 AM6/22/15
to phpsp...@googlegroups.com
You are testing behaviours, each test should explain one behaviour.
Also I don't think you can have multiple 'beConstructedWith' in one method, as that's passed to the SUS contructor.
And lastly, having them in different methods makes it clear which one of them fails in case they do.

Jaime Isagani Sangcap

unread,
Jun 22, 2015, 1:36:55 PM6/22/15
to phpsp...@googlegroups.com
That makes sense. I will try it now. Thank you
Reply all
Reply to author
Forward
0 new messages