How do I go about testing in Laravel

Posted 1 year ago by andremac96

I started learning Laravel earlier this year, and I want to get into testing. I have never written a test in any language, and I have no idea how to start.

For example. I have a function below, that performs the logic of a search filter. How would I perform a test, what's the point of testing, if I have validation?

    public function search(Request $request){
      //Search Filter Logic

      //Validation

      $this->validate($request, [
        "county" => "required",
        "town" => "required",
        "type" => "required",
        "min-bedrooms" => "required",
        "max-bedrooms" => "required",
        "min-bathrooms" => "required",
        "max-bathrooms" => "required",
        "min-rent" => "required|max:4",
        "max-rent" => "required|max:4",
      ]);

      $user = Auth::user();
      $query = PropertyAdvert::query();
      $options = [];

      //Fills options array with county, town and yupe
      if ($request->has('county')) {
          $options['county'] = $request->input('county');
      }

      if ($request->has('town')) {
          $options['town'] = $request->input('town');
      }

      if($request->has('type')){
        $options['type'] = $request->input('type');
      }

      //Preforms query on options array
      if ($options) {
          $query->where($options);
      }

      //Bedrooms
      /*
      *  Checks Min
      *  Checks Max
      *  Results in min -> Max and in between being returned
      *  For Bed, Bath and Rent
      */

      if ($request->has('min-bedrooms')) {
          if ($request->has('max-bedrooms')) {
              $query->whereBetween('bedrooms', [
                  $request->input('min-bedrooms'),
                  $request->input('max-bedrooms')
              ]);
          } else {
              $query->where('bedrooms', '>', $request->input('min-bedrooms'));
          }
      } elseif ($request->has('max-bedrooms')) {
          $query->where('bedrooms', '<', $request->input('max-bedrooms'));
      }

      //Bathrooms
      if ($request->has('min-bathrooms')) {
          if ($request->has('max-bathrooms')) {
              $query->whereBetween('bathrooms', [
                  $request->input('min-bathrooms'),
                  $request->input('max-bathrooms')
              ]);
          } else {
              $query->where('bathrooms', '>', $request->input('min-bathrooms'));
          }
      } elseif ($request->has('max-bathrooms')) {
          $query->where('bathrooms', '<', $request->input('max-bathrooms'));
      }

      //Rent
      if ($request->has('min-rent')) {
          if ($request->has('max-rent')) {
              $query->whereBetween('rent', [
                  $request->input('min-rent'),
                  $request->input('max-rent')
              ]);
          } else {
              $query->where('rent', '>', $request->input('min-rent'));
          }
      } elseif ($request->has('max-rent')) {
          $query->where('rent', '<', $request->input('max-rent'));
      }

      $properties = $query->get(); //Gets all
}

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.