Making WordPress.org


Ignore:
Timestamp:
12/23/2020 04:56:08 PM (3 years ago)
Author:
iandunn
Message:

Events: Port some tests to PHPUnit.

Switching to PHPUnit provides consistency with the rest of the ecosystem, removes a lot of custom code we'd otherwise have to maintain, and provides new features for free. The custom tests were originally created from scratch because of the lack of tooling available on sandboxes, but that problem can be solve with Composer.

This establishes the foundation for porting the rest of the tests in the future.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/api.wordpress.org/public_html/events/1.0/tests/test-index.php

    r10518 r10544  
    11<?php
     2
     3/*
     4 * @todo Move these into proper PHPUnit tests in `tests/test-index.php` as have time.
     5 *
     6 * When that's done, delete this file and rename that one to `test-index.php`.
     7 * Also delete the `test_port_remaining_tests()` test in the PHPUnit class.
     8 */
    29
    310namespace Dotorg\API\Events;
     
    512if ( 'cli' !== php_sapi_name() ) {
    613    die();
     14}
     15
     16/*
     17 * Don't run these tests from PHPUnit.
     18 */
     19if ( defined( 'WPORG_RUNNING_TESTS' ) ) {
     20    return;
    721}
    822
     
    2034    $tests_failed  = 0;
    2135    $tests_failed += test_get_location();
    22     $tests_failed += test_get_events();
    23     $tests_failed += test_get_events_country_restriction();
    2436    $tests_failed += test_maybe_add_regional_wordcamps();
    2537    $tests_failed += test_maybe_add_wp15_promo();
     
    951963                'internal'    => true,
    952964            ),
    953         ),
    954     );
    955 
    956     return $cases;
    957 }
    958 
    959 /**
    960  * Test `get_events()`
    961  *
    962  * @return bool The number of failures
    963  */
    964 function test_get_events() {
    965     $failed = 0;
    966     $cases  = get_events_test_cases();
    967 
    968     printf( "\nRunning %d events tests", count( $cases ) );
    969 
    970     foreach ( $cases as $case_id => $case ) {
    971         $actual_result = get_events( $case['input'] );
    972 
    973         $passed = $case['expected']['count'] === count( $actual_result ) &&
    974                   ! empty( $actual_result[0]['url'] ) &&
    975                   strtotime( $actual_result[0]['date'] ) > time() - ( 2 * 24 * 60 * 60 ) &&
    976                   $case['expected']['country'] === strtoupper( $actual_result[0]['location']['country'] );
    977 
    978         output_results( $case_id, $passed, $case['expected'], $actual_result );
    979 
    980         if ( ! $passed ) {
    981             $failed++;
    982         }
    983     }
    984 
    985     return $failed;
    986 }
    987 
    988 /**
    989  * Get the cases for testing `get_events()`.
    990  *
    991  * @return array
    992  */
    993 function get_events_test_cases() {
    994     $cases = array(
    995         // This assumes there will always be at least 2 upcoming events, so it needs to be a very active community.
    996         '2-near-seattle' => array(
    997             'input' => array(
    998                 'number' => '2',
    999                 'nearby' => array(
    1000                     'latitude'  => '47.609023',
    1001                     'longitude' => '-122.335903',
    1002                 ),
    1003             ),
    1004             'expected' => array(
    1005                 'count'   => 2,
    1006                 'country' => 'US',
    1007             ),
    1008         ),
    1009 
    1010         '1-in-australia' => array(
    1011             'input' => array(
    1012                 'number'  => '1',
    1013                 'country' => 'AU',
    1014                 'restrict_by_country' => true,
    1015             ),
    1016             'expected' => array(
    1017                 'count'   => 1,
    1018                 'country' => 'AU',
    1019             ),
    1020         ),
    1021     );
    1022 
    1023     return $cases;
    1024 }
    1025 
    1026 /**
    1027  * Test `get_events()` `restricted_by_country` parameter.
    1028  *
    1029  * @return bool The number of failures
    1030  */
    1031 function test_get_events_country_restriction() {
    1032     $failed = 0;
    1033     $cases  = get_events_country_restriction_test_cases();
    1034 
    1035     printf( "\nRunning %d events restrict by country tests", count( $cases ) );
    1036 
    1037     foreach ( $cases as $case_id => $case ) {
    1038         $actual_result    = get_events( $case['input'] );
    1039         $actual_countries = array_column( array_column( $actual_result, 'location' ), 'country' );
    1040         $actual_countries = array_unique( array_map( 'strtoupper', $actual_countries ) );
    1041 
    1042         sort( $actual_countries );
    1043 
    1044         $passed = $actual_countries === $case['expected_countries'];
    1045 
    1046         output_results( $case_id, $passed, $case['expected_countries'], $actual_countries );
    1047 
    1048         if ( ! $passed ) {
    1049             $failed++;
    1050         }
    1051     }
    1052 
    1053     return $failed;
    1054 }
    1055 
    1056 /**
    1057  * Get the cases for testing the `get_events()` `restricted_by_country` parameter.
    1058  *
    1059  * @return array
    1060  */
    1061 function get_events_country_restriction_test_cases() {
    1062     $cases = array(
    1063         'restricted-by-country' => array(
    1064             'input' => array(
    1065                 'number'              => '500',
    1066                 'country'             => 'CA',
    1067                 'restrict_by_country' => true,
    1068             ),
    1069             'expected_countries' => array( 'CA' ),
    1070         ),
    1071 
    1072         /*
    1073          * This assumes there will always be at least an upcoming event on both sides of the border, so the
    1074          * coordinates need to be half-way between two very active groups in different countries, where the
    1075          * mid-point is less than `$event_distances['meetup']`.
    1076          *
    1077          * If Toronto, CA and Buffalo, US no longer work in the future, then another possible location would be
    1078          * `53.997654, -6.403377` -- between Belfast, GB and Dublin, IE -- or `47.986952, -122.961350` --
    1079          * between Seattle, US and Victoria, CA.
    1080          *
    1081          * See https://wordpress.slack.com/archives/C08M59V3P/p1524168308000202.
    1082          */
    1083         'not-restricted-by-country' => array(
    1084             'input' => array(
    1085                 'number'              => '500',
    1086                 'restrict_by_country' => false,
    1087 
    1088                 'nearby' => array(
    1089                     'latitude'  => '43.254372',
    1090                     'longitude' => '-79.063746',
    1091                 ),
    1092             ),
    1093             'expected_countries' => array( 'CA', 'US' ),
    1094965        ),
    1095966    );
Note: See TracChangeset for help on using the changeset viewer.