Debug School

rakesh kumar
rakesh kumar

Posted on

Integration and Unit Testing for Search Functionality

In Laravel, you can use PHPUnit for both unit testing and integration testing. Below is an example of how you can structure your tests for a country, state, and city filtering system.

Assuming you have a filter class that filters countries, states, and cities based on certain criteria, here's an example:

// app/Filters/LocationFilter.php

namespace App\Filters;

class LocationFilter
{
    public function filterCountries($countries, $criteria)
    {
        // Implementation of country filtering logic
        // ...

        return $filteredCountries;
    }

    public function filterStates($states, $criteria)
    {
        // Implementation of state filtering logic
        // ...

        return $filteredStates;
    }

    public function filterCities($cities, $criteria)
    {
        // Implementation of city filtering logic
        // ...

        return $filteredCities;
    }
}
Enter fullscreen mode Exit fullscreen mode

Now, let's create tests for both unit testing and integration testing.

// tests/Unit/LocationFilterTest.php

namespace Tests\Unit;

use App\Filters\LocationFilter;
use PHPUnit\Framework\TestCase;

class LocationFilterTest extends TestCase
{
    public function testCountryFilter()
    {
        $filter = new LocationFilter();
        $countries = ['USA', 'Canada', 'UK'];
        $criteria = 'some_criteria';

        $result = $filter->filterCountries($countries, $criteria);

        // Add your assertions based on the expected behavior of the filter
        $this->assertNotEmpty($result);
        // ...
    }

    public function testStateFilter()
    {
        $filter = new LocationFilter();
        $states = ['New York', 'California', 'Ontario'];
        $criteria = 'some_criteria';

        $result = $filter->filterStates($states, $criteria);

        // Add your assertions based on the expected behavior of the filter
        $this->assertNotEmpty($result);
        // ...
    }
}
Enter fullscreen mode Exit fullscreen mode

// tests/Feature/LocationFilterIntegrationTest.php

namespace Tests\Feature;

use App\Filters\LocationFilter;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class LocationFilterIntegrationTest extends TestCase
{
    use RefreshDatabase;

    public function testCityFilter()
    {
        // Assume you have cities stored in the database

        $filter = new LocationFilter();
        $criteria = 'some_criteria';

        // Fetch cities from the database or set up test data
        $cities = \App\Models\City::pluck('name')->toArray();

        $result = $filter->filterCities($cities, $criteria);

        // Add your assertions based on the expected behavior of the filter
        $this->assertNotEmpty($result);
        // ...
    }
}
Enter fullscreen mode Exit fullscreen mode

Make sure to adapt the code according to your actual implementation and requirements. To run the tests, you can use the following command:

php artisan test
Enter fullscreen mode Exit fullscreen mode

This will execute both unit tests and integration tests in your Laravel application.

Another Example

Let's create an example for unit testing and integration testing of a search functionality for countries, states, and cities in Laravel. We'll assume you have models for Country, State, and City, and a SearchService class that handles the search logic.

Models

// app/Models/Country.php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Country extends Model
{
    protected $fillable = ['name'];
}
Enter fullscreen mode Exit fullscreen mode

// app/Models/State.php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class State extends Model
{
    protected $fillable = ['name', 'country_id'];
}

Enter fullscreen mode Exit fullscreen mode

// app/Models/City.php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class City extends Model
{
    protected $fillable = ['name', 'state_id'];
}
Enter fullscreen mode Exit fullscreen mode

SearchService

// app/Services/SearchService.php

namespace App\Services;

use App\Models\Country;
use App\Models\State;
use App\Models\City;

class SearchService
{
    public function search($keyword)
    {
        // Search logic using the given keyword
        $countries = Country::where('name', 'like', "%$keyword%")->get();
        $states = State::where('name', 'like', "%$keyword%")->get();
        $cities = City::where('name', 'like', "%$keyword%")->get();

        return compact('countries', 'states', 'cities');
    }
}
Enter fullscreen mode Exit fullscreen mode

Unit Test

// tests/Unit/SearchServiceTest.php

namespace Tests\Unit;

use App\Services\SearchService;
use Tests\TestCase;

class SearchServiceTest extends TestCase
{
    public function testSearch()
    {
        $searchService = new SearchService();

        // Test search for a specific keyword
        $result = $searchService->search('United');

        $this->assertNotEmpty($result['countries']);
        $this->assertNotEmpty($result['states']);
        $this->assertNotEmpty($result['cities']);

        // Add more assertions based on your specific criteria
    }
}
Enter fullscreen mode Exit fullscreen mode

Integration Test

// tests/Feature/SearchIntegrationTest.php

namespace Tests\Feature;

use App\Services\SearchService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class SearchIntegrationTest extends TestCase
{
    use RefreshDatabase;

    public function testSearchInteractionsWithDatabase()
    {
        // Seed the database with test data
        // ...

        $searchService = new SearchService();

        // Test search interactions with the database
        $result = $searchService->search('New York');

        $this->assertCount(1, $result['states']);
        $this->assertCount(1, $result['cities']);

        // Add more assertions based on your specific criteria
    }
}
Enter fullscreen mode Exit fullscreen mode

In these examples:

The SearchService class is responsible for performing the search operation on the Country, State, and City models.
The unit test (SearchServiceTest) checks the behavior of the SearchService class independently.
The integration test (SearchIntegrationTest) tests the interaction of the search service with the database.
Make sure to adapt the code according to your actual implementation and requirements. To run the tests, you can use the following command:

php artisan test
Enter fullscreen mode Exit fullscreen mode

This will execute both the unit and integration tests and verify that the search functionality is working as expected.

Top comments (0)