Skip to content

Commit

Permalink
added data providers
Browse files Browse the repository at this point in the history
  • Loading branch information
SiR-DanieL committed Apr 14, 2015
1 parent d4ff494 commit 0347a40
Show file tree
Hide file tree
Showing 2 changed files with 168 additions and 82 deletions.
89 changes: 56 additions & 33 deletions tests/unit-tests/util/conditional-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,50 +38,73 @@ public function test_wc_prices_include_tax() {
$this->assertEquals( false, wc_prices_include_tax() );
}

/**
* Data provider for test_wc_is_webhook_valid_topic
*
* @since 2.4
*/
public function data_provider_test_wc_is_webhook_valid_topic() {
return array(
array( true, wc_is_webhook_valid_topic( 'action.woocommerce_add_to_cart' ) ),
array( true, wc_is_webhook_valid_topic( 'action.wc_add_to_cart' ) ),
array( true, wc_is_webhook_valid_topic( 'product.created' ) ),
array( true, wc_is_webhook_valid_topic( 'product.updated' ) ),
array( true, wc_is_webhook_valid_topic( 'product.deleted' ) ),
array( true, wc_is_webhook_valid_topic( 'order.created' ) ),
array( true, wc_is_webhook_valid_topic( 'order.updated' ) ),
array( true, wc_is_webhook_valid_topic( 'order.deleted' ) ),
array( true, wc_is_webhook_valid_topic( 'customer.created' ) ),
array( true, wc_is_webhook_valid_topic( 'customer.updated' ) ),
array( true, wc_is_webhook_valid_topic( 'customer.deleted' ) ),
array( true, wc_is_webhook_valid_topic( 'coupon.created' ) ),
array( true, wc_is_webhook_valid_topic( 'coupon.updated' ) ),
array( true, wc_is_webhook_valid_topic( 'coupon.deleted' ) ),
array( false, wc_is_webhook_valid_topic( 'coupon.upgraded' ) ),
array( false, wc_is_webhook_valid_topic( 'wc.product.updated' ) ),
array( false, wc_is_webhook_valid_topic( 'missingdot' ) ),
array( false, wc_is_webhook_valid_topic( 'with space' ) )
);
}

/**
* Test wc_is_webhook_valid_topic()
*
* @dataProvider data_provider_test_wc_is_webhook_valid_topic
* @since 2.4
*/
public function test_wc_is_webhook_valid_topic() {
$this->assertEquals( true, wc_is_webhook_valid_topic( 'action.woocommerce_add_to_cart' ) );
$this->assertEquals( true, wc_is_webhook_valid_topic( 'action.wc_add_to_cart' ) );
$this->assertEquals( true, wc_is_webhook_valid_topic( 'product.created' ) );
$this->assertEquals( true, wc_is_webhook_valid_topic( 'product.updated' ) );
$this->assertEquals( true, wc_is_webhook_valid_topic( 'product.deleted' ) );
$this->assertEquals( true, wc_is_webhook_valid_topic( 'order.created' ) );
$this->assertEquals( true, wc_is_webhook_valid_topic( 'order.updated' ) );
$this->assertEquals( true, wc_is_webhook_valid_topic( 'order.deleted' ) );
$this->assertEquals( true, wc_is_webhook_valid_topic( 'customer.created' ) );
$this->assertEquals( true, wc_is_webhook_valid_topic( 'customer.updated' ) );
$this->assertEquals( true, wc_is_webhook_valid_topic( 'customer.deleted' ) );
$this->assertEquals( true, wc_is_webhook_valid_topic( 'coupon.created' ) );
$this->assertEquals( true, wc_is_webhook_valid_topic( 'coupon.updated' ) );
$this->assertEquals( true, wc_is_webhook_valid_topic( 'coupon.deleted' ) );
$this->assertEquals( false, wc_is_webhook_valid_topic( 'coupon.upgraded' ) );
$this->assertEquals( false, wc_is_webhook_valid_topic( 'wc.product.updated' ) );
$this->assertEquals( false, wc_is_webhook_valid_topic( 'missingdot' ) );
$this->assertEquals( false, wc_is_webhook_valid_topic( 'with space' ) );
public function test_wc_is_webhook_valid_topic( $assert, $values ) {
$this->assertEquals( $assert, $values );
}

/**
* Test wc_is_valid_url()
* Data provider for test_wc_is_valid_url
*
* @since 2.3.0
* @since 2.4
*/
public function test_wc_is_valid_url() {
public function data_provider_test_wc_is_valid_url() {
return array(
// Test some invalid URLs
array( false, wc_is_valid_url( 'google.com' ) ),
array( false, wc_is_valid_url( 'ftp://google.com' ) ),
array( false, wc_is_valid_url( 'sftp://google.com' ) ),
array( false, wc_is_valid_url( 'https://google.com/test invalid' ) ),

// Test some invalid URLs
$this->assertEquals( false, wc_is_valid_url( 'google.com' ) );
$this->assertEquals( false, wc_is_valid_url( 'ftp://google.com' ) );
$this->assertEquals( false, wc_is_valid_url( 'sftp://google.com' ) );
$this->assertEquals( false, wc_is_valid_url( 'https://google.com/test invalid' ) );
// Test some valid URLs
array( true, wc_is_valid_url( 'http://google.com' ) ),
array( true, wc_is_valid_url( 'https://google.com' ) ),
array( true, wc_is_valid_url( 'https://google.com/test%20valid' ) ),
array( true, wc_is_valid_url( 'https://google.com/test-valid/?query=test' ) ),
array( true, wc_is_valid_url( 'https://google.com/test-valid/#hash' ) )
);
}

// Test some valid URLs
$this->assertEquals( true, wc_is_valid_url( 'http://google.com' ) );
$this->assertEquals( true, wc_is_valid_url( 'https://google.com' ) );
$this->assertEquals( true, wc_is_valid_url( 'https://google.com/test%20valid' ) );
$this->assertEquals( true, wc_is_valid_url( 'https://google.com/test-valid/?query=test' ) );
$this->assertEquals( true, wc_is_valid_url( 'https://google.com/test-valid/#hash' ) );
/**
* Test wc_is_valid_url()
*
* @dataProvider data_provider_test_wc_is_valid_url
* @since 2.3.0
*/
public function test_wc_is_valid_url( $assert, $values ) {
$this->assertEquals( $assert, $values );
}
}
161 changes: 112 additions & 49 deletions tests/unit-tests/util/validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,79 +17,142 @@ public function test_is_email() {
$this->assertFalse( \WC_Validation::is_email( 'not a mail' ) );
$this->assertFalse( \WC_Validation::is_email( 'http://test.com' ) );
}

/**
* Data provider for test_is_phone
*
* @since 2.4
*/
public function data_provider_test_is_phone() {
return array(
array( true, \WC_Validation::is_phone( '+00 000 00 00 000' ) ),
array( true, \WC_Validation::is_phone( '+00-000-00-00-000' ) ),
array( true, \WC_Validation::is_phone( '(000) 00 00 000' ) ),
array( false, \WC_Validation::is_phone( '+00.000.00.00.000' ) ),
array( false, \WC_Validation::is_phone( '+00 aaa dd ee fff' ) )
);
}

/**
* Test is_phone()
*
* @dataProvider data_provider_test_is_phone
* @since 2.3
*/
public function test_is_phone() {
$this->assertTrue( \WC_Validation::is_phone( '+00 000 00 00 000' ) );
$this->assertTrue( \WC_Validation::is_phone( '+00-000-00-00-000' ) );
$this->assertTrue( \WC_Validation::is_phone( '(000) 00 00 000' ) );
$this->assertFalse( \WC_Validation::is_phone( '+00.000.00.00.000' ) );
$this->assertFalse( \WC_Validation::is_phone( '+00 aaa dd ee fff' ) );
public function test_is_phone( $assert, $values ) {
$this->assertEquals( $assert, $values );
}

/**
* Data provider for test_is_postcode()
*
* @since 2.4
*/
public function data_provider_test_is_postcode() {
$generic = array(
array( true, \WC_Validation::is_postcode( '99999', 'IT' ) ),
array( true, \WC_Validation::is_postcode( '99999', 'IT' ) ),
array( true, \WC_Validation::is_postcode( '9999', 'IT' ) ),
array( true, \WC_Validation::is_postcode( 'ABC 999', 'IT' ) ),
array( true, \WC_Validation::is_postcode( 'ABC-999', 'IT' ) ),
array( false, \WC_Validation::is_postcode( 'ABC_123', 'IT' ) )
);

$gb = array(
array( true, \WC_Validation::is_postcode( 'A9 9AA', 'GB' ) ),
array( false, \WC_Validation::is_postcode( '99999', 'GB' ) )
);

$us = array(
array( true, \WC_Validation::is_postcode( '99999', 'US' ) ),
array( true, \WC_Validation::is_postcode( '99999-9999', 'US' ) ),
array( false, \WC_Validation::is_postcode( 'ABCDE', 'US' ) ),
array( false, \WC_Validation::is_postcode( 'ABCDE-9999', 'US' ) )
);

$ch = array(
array( true, \WC_Validation::is_postcode( '9999', 'CH' ) ),
array( false, \WC_Validation::is_postcode( '99999', 'CH' ) ),
array( false, \WC_Validation::is_postcode( 'ABCDE', 'CH' ) )
);

$br = array(
array( true, \WC_Validation::is_postcode( '99999-999', 'BR' ) ),
array( true, \WC_Validation::is_postcode( '99999999', 'BR' ) ),
array( false, \WC_Validation::is_postcode( '99999 999', 'BR' ) ),
array( false, \WC_Validation::is_postcode( '99999-ABC', 'BR' ) )
);

return array_merge( $generic, $gb, $us, $ch, $br );
}

/**
* Test is_postcode()
*
* @dataProvider data_provider_test_is_postcode
* @since 2.4
*/
public function test_is_postcode() {
// Generic postcodes
$this->assertTrue( \WC_Validation::is_postcode( '99999', 'IT' ) );
$this->assertTrue( \WC_Validation::is_postcode( '9999', 'IT' ) );
$this->assertTrue( \WC_Validation::is_postcode( 'ABC 999', 'IT' ) );
$this->assertTrue( \WC_Validation::is_postcode( 'ABC-999', 'IT' ) );
$this->assertFalse( \WC_Validation::is_postcode( 'ABC_123', 'IT' ) );
// GB postcodes
$this->assertTrue( \WC_Validation::is_postcode( 'A9 9AA', 'GB' ) );
$this->assertFalse( \WC_Validation::is_postcode( '99999', 'GB' ) );
// US postcodes
$this->assertTrue( \WC_Validation::is_postcode( '99999', 'US' ) );
$this->assertTrue( \WC_Validation::is_postcode( '99999-9999', 'US' ) );
$this->assertFalse( \WC_Validation::is_postcode( 'ABCDE', 'US' ) );
$this->assertFalse( \WC_Validation::is_postcode( 'ABCDE-9999', 'US' ) );
// CH postcodes
$this->assertTrue( \WC_Validation::is_postcode( '9999', 'CH' ) );
$this->assertFalse( \WC_Validation::is_postcode( '99999', 'CH' ) );
$this->assertFalse( \WC_Validation::is_postcode( 'ABCDE', 'CH' ) );
// BR postcodes
$this->assertTrue( \WC_Validation::is_postcode( '99999-999', 'BR' ) );
$this->assertTrue( \WC_Validation::is_postcode( '99999999', 'BR' ) );
$this->assertFalse( \WC_Validation::is_postcode( '99999 999', 'BR' ) );
$this->assertFalse( \WC_Validation::is_postcode( '99999-ABC', 'BR' ) );
public function test_is_postcode( $assert, $values ) {
$this->assertEquals( $assert, $values );
}

/**
* Data provider for test_is_GB_postcode
*
* @since 2.4
*/
public function data_provider_test_is_GB_postcode() {
return array(
array( true, \WC_Validation::is_GB_postcode( 'AA9A 9AA' ) ),
array( true, \WC_Validation::is_GB_postcode( 'A9A 9AA' ) ),
array( true, \WC_Validation::is_GB_postcode( 'A9 9AA' ) ),
array( true, \WC_Validation::is_GB_postcode( 'A99 9AA' ) ),
array( true, \WC_Validation::is_GB_postcode( 'AA99 9AA' ) ),
array( true, \WC_Validation::is_GB_postcode( 'BFPO 801' ) ),
array( false, \WC_Validation::is_GB_postcode( '99999' ) ),
array( false, \WC_Validation::is_GB_postcode( '9999 999' ) ),
array( false, \WC_Validation::is_GB_postcode( '999 999' ) ),
array( false, \WC_Validation::is_GB_postcode( '99 999' ) ),
array( false, \WC_Validation::is_GB_postcode( '9A A9A' ) )
);
}

/**
* Test is_GB_postcode()
*
* @dataProvider data_provider_test_is_GB_postcode
* @since 2.4
*/
public function test_is_GB_postcode() {
$this->assertTrue( \WC_Validation::is_GB_postcode( 'AA9A 9AA' ) );
$this->assertTrue( \WC_Validation::is_GB_postcode( 'A9A 9AA' ) );
$this->assertTrue( \WC_Validation::is_GB_postcode( 'A9 9AA' ) );
$this->assertTrue( \WC_Validation::is_GB_postcode( 'A99 9AA' ) );
$this->assertTrue( \WC_Validation::is_GB_postcode( 'AA99 9AA' ) );
$this->assertTrue( \WC_Validation::is_GB_postcode( 'BFPO 801' ) );
$this->assertFalse( \WC_Validation::is_GB_postcode( '99999' ) );
$this->assertFalse( \WC_Validation::is_GB_postcode( '9999 999' ) );
$this->assertFalse( \WC_Validation::is_GB_postcode( '999 999' ) );
$this->assertFalse( \WC_Validation::is_GB_postcode( '99 999' ) );
$this->assertFalse( \WC_Validation::is_GB_postcode( '9A A9A' ) );
public function test_is_GB_postcode( $assert, $values ) {
$this->assertEquals( $assert, $values );
}

/**
* Data provider for test_format_postcode
*
* @since 2.4
*/
public function data_provider_test_format_postcode() {
return array(
array( '99999', \WC_Validation::format_postcode( '99999', 'IT' ) ),
array( '99999', \WC_Validation::format_postcode( ' 99999 ', 'IT' ) ),
array( '99999', \WC_Validation::format_postcode( '999 99', 'IT' ) ),
array( 'ABCDE', \WC_Validation::format_postcode( 'abcde', 'IT' ) ),
array( 'AB CDE', \WC_Validation::format_postcode( 'abcde', 'GB' ) ),
array( 'AB CDE', \WC_Validation::format_postcode( 'abcde', 'CA' ) )
);
}

/**
* Test format_postcode()
*
* @dataProvider data_provider_test_format_postcode
* @since 2.4
*/
public function test_format_postcode() {
$this->assertEquals( '99999', \WC_Validation::format_postcode( '99999', 'IT' ) );
$this->assertEquals( '99999', \WC_Validation::format_postcode( ' 99999 ', 'IT' ) );
$this->assertEquals( '99999', \WC_Validation::format_postcode( '999 99', 'IT' ) );
$this->assertEquals( 'ABCDE', \WC_Validation::format_postcode( 'abcde', 'IT' ) );
$this->assertEquals( 'AB CDE', \WC_Validation::format_postcode( 'abcde', 'GB' ) );
$this->assertEquals( 'AB CDE', \WC_Validation::format_postcode( 'abcde', 'CA' ) );
public function test_format_postcode( $assert, $values ) {
$this->assertEquals( $assert, $values );
}

/**
* Test format_phone()
*
Expand Down

0 comments on commit 0347a40

Please sign in to comment.