mardi 19 janvier 2021

Unit Testing for Filtered set of results using Laravel and PhpUnit

Lets say I have a system, build with Laravel and used for displaying different restaurants and It has a standard filter , that checks for all restaurants that have the given filter criteria. Every restaurant holds properties:

  • has_wifi; can_pay_with_card; has_parking; is_pet_friendly
Restaurant Wifi Card Pay Parking Pet Friendly
R 1 1 1 0 0
R 2 0 0 1 1
R 3 1 1 1 0

and there is filter, that uses those same properties.

(Wifi) (Card Pay) (Parking) (Pet Friendly) -> R1, R3

(Wifi) (Card Pay) (Parking) (Pet Friendly) -> R3

Currently I am writing tests using PHPUnit and for what I understand, I should check all input possibilities for the given filter and compare the returned subset of data.

(Wifi) (Card Pay) (Parking) (Pet Friendly)

(Wifi) (Card Pay) (Parking) (Pet Friendly)

(Wifi) (Card Pay) (Parking) (Pet Friendly)

…..

(Wifi) (Card Pay) (Parking) (Pet Friendly)

….

(Wifi) (Card Pay) (Parking) (Pet Friendly)

For the base case, where I should check only for places with wifi for example, I can do the following:

public function testFilterByWifiWithPlaces()
{
    $body = $this->requestParameters([
        'wifi' => 1,
    ]);

    $this->json(
        'POST',
        $this->baseUrl . self::SEARCH_PLACES_ENDPOINT,
        $body,
        $this->requestHeaders()
    )->seeJsonStructure([
        'id',
        'code',
        'data' => [
            'places' => [
                '*' => $this->dataObject->getStructure()
            ],
        ]
    ])->seeJson([
    'id' => $this->placeWithWifi->place_key,
    'name' => $this->placeWithWifi->default_name]);
}

BUT given the data that I have and the dual possibility of the values, using separate methods like testFilterByWifiWithPlaces(), testFilterByWifiAndParkingWithPlaces(),… (all possibilities),… is not an valid option for sure!

My approach to the problem is the following:

  • Creating a array holding instances of all possible objects with different property combinations
  • Accessing them via keys: ex: $testRestaurants[‘with_wifi][’no_card_pay’][‘with_parking][’no_pet_friendly’] -> Restaurant object
  • Looping through all $testRestaurants and executing above mentioned logic, knowing the state of the filter from keys given

I am not sure if i am on the right track. This seems as a problem that many people should’ve faced, taking in consideration the large amount of filter engines existing on the internet :D Still , I wonder is there easiest way to test this kind of large set with data with many possibilities?

Aucun commentaire:

Enregistrer un commentaire