From 0347a4005f62c1f1be7e51b48b128fe8fac1f9b2 Mon Sep 17 00:00:00 2001 From: Nicola Mustone Date: Tue, 14 Apr 2015 15:34:35 +0200 Subject: [PATCH] added data providers --- .../unit-tests/util/conditional-functions.php | 91 ++++++---- tests/unit-tests/util/validation.php | 163 ++++++++++++------ 2 files changed, 170 insertions(+), 84 deletions(-) diff --git a/tests/unit-tests/util/conditional-functions.php b/tests/unit-tests/util/conditional-functions.php index 04847d0b18e..8b5f591a8b7 100644 --- a/tests/unit-tests/util/conditional-functions.php +++ b/tests/unit-tests/util/conditional-functions.php @@ -39,49 +39,72 @@ class Conditional_Functions extends \WC_Unit_Test_Case { } /** - * Test wc_is_webhook_valid_topic() + * Data provider for 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 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( $assert, $values ) { + $this->assertEquals( $assert, $values ); + } + + /** + * Data provider for test_wc_is_valid_url + * + * @since 2.4 + */ + 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 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 wc_is_valid_url() * + * @dataProvider data_provider_test_wc_is_valid_url * @since 2.3.0 */ - public function test_wc_is_valid_url() { - - // 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 - $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' ) ); + public function test_wc_is_valid_url( $assert, $values ) { + $this->assertEquals( $assert, $values ); } } diff --git a/tests/unit-tests/util/validation.php b/tests/unit-tests/util/validation.php index 8e960470f36..426293ff471 100644 --- a/tests/unit-tests/util/validation.php +++ b/tests/unit-tests/util/validation.php @@ -17,79 +17,142 @@ class Validation extends \WC_Unit_Test_Case { $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 ); } + /** - * Test format_postcode() + * Data provider for 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 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( $assert, $values ) { + $this->assertEquals( $assert, $values ); + } + /** * Test format_phone() *