Skip to content

Commit

Permalink
Update unit tests to support phpunit 6
Browse files Browse the repository at this point in the history
  • Loading branch information
Craig Manley committed May 4, 2019
1 parent 8579fdc commit 2ae618e
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 28 deletions.
27 changes: 14 additions & 13 deletions t/Spec.t.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
<?php
namespace PHPUnit\Framework;
if (isset($argv)) {
print "Usage:\n";
print 'phpunit ' . $argv[0] . "\n";
class PHPUnit_Framework_TestCase {}
class TestCase {}
}


class Test extends PHPUnit_Framework_TestCase {
class T extends TestCase {

const CLASS_NAME = 'Validate\\Spec';
const FILE_NAME = '../src/Spec.php';
Expand Down Expand Up @@ -58,7 +59,7 @@ public function testCreate() {
'default' => '[email protected]',
'description' => 'Email address',
'optional' => true,
'validation' => new Validate\Validation(array(
'validation' => new \Validate\Validation(array(
'type' => 'string',
'callback' => function($x) { return filter_var($x, FILTER_VALIDATE_EMAIL); },
'mb_max_length' => 50,
Expand All @@ -70,12 +71,12 @@ public function testCreate() {
}

public function testValidate() {
$spec = new Validate\Spec(array(
$spec = new \Validate\Spec(array(
'allow_empty' => false,
'before' => function(&$x) { if (is_string($x)) { $x = mb_strtolower($x); } },
'after' => function(&$x) { if (is_string($x)) { $x = ucfirst($x); } },
'description' => 'Email address',
'validation' => new Validate\Validation(array(
'validation' => new \Validate\Validation(array(
'type' => 'string',
'callbacks' => array(
'syntax' => function($x) { return filter_var($x, FILTER_VALIDATE_EMAIL); },
Expand Down Expand Up @@ -126,12 +127,12 @@ public function testValidate() {
}

public function testValidateWithDefault() {
$spec = new Validate\Spec(array(
$spec = new \Validate\Spec(array(
'allow_empty' => false,
'after' => function(&$x) { $x = mb_strtolower($x); },
'description' => 'Email address',
'default' => 'nobody', # defaults aren't validated as they are trusted values
'validation' => new Validate\Validation(array(
'validation' => new \Validate\Validation(array(
'type' => 'string',
'callbacks' => array(
'syntax' => function($x) { return filter_var($x, FILTER_VALIDATE_EMAIL); },
Expand Down Expand Up @@ -170,21 +171,21 @@ public function testValidateWithDefault() {

public function testReturnValuesBeforeAfter() {
foreach (array('before', 'after') as $when) {
$spec = new Validate\Spec(array(
$spec = new \Validate\Spec(array(
$when => function(&$x) {}, # void (null) result
));
$input = 'anything';
$this->assertTrue($spec->validate($input), "Using callback '$when' with void return value validates as true.");
$this->assertEquals(null, $spec->getLastFailure(), 'Check last failure');

$spec = new Validate\Spec(array(
$spec = new \Validate\Spec(array(
$when => function(&$x) { return false; },
));
$input = 'anything';
$this->assertTrue(!$spec->validate($input), "Using callback '$when' with FALSE return value validates as FALSE.");
$this->assertEquals("callback $when", $spec->getLastFailure(), 'Check last failure');

$spec = new Validate\Spec(array(
$spec = new \Validate\Spec(array(
$when => function(&$x) { $x = strtoupper($x); return true; },
));
$input = 'anything';
Expand All @@ -198,14 +199,14 @@ public function testReturnValuesBeforeAfter() {


if (isset($argv)) {
require_once(__DIR__ . '/' . Test::FILE_NAME);
require_once(__DIR__ . '/' . T::FILE_NAME);
if (1) {
$spec = new Validate\Spec(array(
$spec = new \Validate\Spec(array(
'allow_empty' => false,
'before' => function(&$x) { if (is_string($x)) { $x = mb_strtolower($x); } },
'after' => function(&$x) { if (is_string($x)) { $x = ucfirst($x); } },
'description' => 'Email address',
'validation' => new Validate\Validation(array(
'validation' => new \Validate\Validation(array(
'type' => 'string',
'callbacks' => array(
'syntax' => function($x) { return filter_var($x, FILTER_VALIDATE_EMAIL); },
Expand Down
11 changes: 6 additions & 5 deletions t/SpecCollection.t.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
<?php
namespace PHPUnit\Framework;
if (isset($argv)) {
print "Usage:\n";
print 'phpunit ' . $argv[0] . "\n";
class PHPUnit_Framework_TestCase {}
class TestCase {}
}


class Test extends PHPUnit_Framework_TestCase {
class T extends TestCase {

const CLASS_NAME = 'Validate\\SpecCollection';
const FILE_NAME = '../src/SpecCollection.php';
Expand Down Expand Up @@ -131,7 +132,7 @@ public function testValidate() {
'expect' => true,
),
);
$specs = new Validate\SpecCollection(array(
$specs = new \Validate\SpecCollection(array(
'firstname' => array(
'description' => 'First name',
'mb_max_length' => 10,
Expand Down Expand Up @@ -166,7 +167,7 @@ public function testValidate() {


if (isset($argv)) {
require_once(__DIR__ . '/' . Test::FILE_NAME);
require_once(__DIR__ . '/' . T::FILE_NAME);
if (1) {
$tests = array(
array(
Expand Down Expand Up @@ -219,7 +220,7 @@ public function testValidate() {
'expect' => true,
),
);
$specs = new Validate\SpecCollection(array(
$specs = new \Validate\SpecCollection(array(
'firstname' => array(
'description' => 'First name',
'mb_max_length' => 10,
Expand Down
7 changes: 4 additions & 3 deletions t/Validation.t.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
<?php
namespace PHPUnit\Framework;
if (isset($argv)) {
print "Usage:\n";
print 'phpunit ' . $argv[0] . "\n";
class PHPUnit_Framework_TestCase {}
class TestCase {}
}


class Test extends PHPUnit_Framework_TestCase {
class T extends TestCase {

const CLASS_NAME = 'Validate\\Validation';
const FILE_NAME = '../src/Validation.php';
Expand Down Expand Up @@ -67,7 +68,7 @@ public function testCreate() {
}

public function testValidate() {
$validation = new Validate\Validation(array(
$validation = new \Validate\Validation(array(
'callbacks' => array(
'is_lc' => function($s) { return mb_strtolower($s) == $s; },
),
Expand Down
15 changes: 8 additions & 7 deletions t/Validator.t.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
<?php
namespace PHPUnit\Framework;
if (isset($argv)) {
print "Usage:\n";
print 'phpunit ' . $argv[0] . "\n";
class PHPUnit_Framework_TestCase {}
class TestCase {}
}


class Test extends PHPUnit_Framework_TestCase {
class T extends TestCase {

const CLASS_NAME = 'Validate\\Validator';
const FILE_NAME = '../src/Validator.php';
Expand Down Expand Up @@ -105,7 +106,7 @@ public function testValidate() {
'min_value' => 0,
),
);
$validator = new Validate\Validator(array(
$validator = new \Validate\Validator(array(
'allow_extra' => false,
'delete_null' => true,
'null_empty_strings' => true,
Expand Down Expand Up @@ -159,7 +160,7 @@ public function testValidate() {
try {
$validated_input = $validator->validate($input);
}
catch (Validate\Exception\ValidationException $e) {
catch (\Validate\Exception\ValidationException $e) {
$got_exception = $e->getMessage();
}
$this->assertEquals($expect, $validated_input, "Test $i validate() returns expected result.");
Expand Down Expand Up @@ -332,7 +333,7 @@ public function testValidatePosSpecCollection() {
foreach ($tests as $name => $test) {
$args = $test['options'];
$args['specs'] = $specs;
$validator = new Validate\Validator($args);
$validator = new \Validate\Validator($args);
foreach ($inputs as $input_name => $input) {
$expect = $test['expects'][$input_name]['expect'];
$expect_exception = $test['expects'][$input_name]['expect_exception'];
Expand All @@ -341,7 +342,7 @@ public function testValidatePosSpecCollection() {
try {
$validated_input = $validator->validate_pos($input);
}
catch (Validate\Exception\ValidationException $e) {
catch (\Validate\Exception\ValidationException $e) {
$got_exception = $e->getMessage();
}
$this->assertEquals($expect, $validated_input, "Test \"$name\"->\"$input_name\" validate() returns expected result.");
Expand All @@ -355,5 +356,5 @@ public function testValidatePosSpecCollection() {


if (isset($argv)) {
require_once(Test::FILE_NAME);
require_once(T::FILE_NAME);
}

0 comments on commit 2ae618e

Please sign in to comment.