<?php
/**
 * @copyright (c) Kufer Software GmbH, Loisachstrasse 4, D-84453 Mühldorf
 */

use Kufer\Web\Classes\Dbfilter\KurseSQLFilter;
use Kufer\Web\KuferTools\Utils\FilterSearch\KurseSearchFilter;

class KurseSearchFilterIntegrationTest extends \PHPUnit\Framework\TestCase
{
  /**
   * @var \Kufer\Web\KuferTools\Utils\FilterSearch\KurseSearchFilter
   */
  protected $object;

  /**
   * Sets up the fixture, for example, opens a network connection.
   * This method is called before a test is executed.
   */
  protected function setUp(): void
  {
    $this->object = new KurseSearchFilter();
  }

  /**
   * Tears down the fixture, for example, closes a network connection.
   * This method is called after a test is executed.
   */
  protected function tearDown(): void
  {
  }

  protected function assertSearchMapAndQuery(array $searchMap, bool $checkEmpty = true)
  {
    $dbwKurse = \DBWorkKurseWT::getDefaultInstance();
    $filter = KurseSQLFilter::create();
    $filter = $this->object->applySearch($filter, $searchMap);
    $dbwKurse->countByFilter($filter);

    if ($checkEmpty) {
      $this->assertNotEmpty($filter->createSql($dbwKurse->dboperation));
    } else {
      $this->assertEmpty($filter->createSql($dbwKurse->dboperation));
    }

    $this->assertTrue(true); // Hier kommt man nur an wenn keine Exception
  }

  /**
   * @test
   */
  public function testStichwort()
  {
    $this->assertSearchMapAndQuery(array('stichwort' => '211'));
    $this->assertSearchMapAndQuery(array('stichwort' => '211-'));
    $this->assertSearchMapAndQuery(array('stichwort' => '211- Vertragskurs'));
  }

  /**
   * @test
   */
  public function testKursort()
  {
    $this->assertSearchMapAndQuery(array('kursort' => 'Demohausen'));
    $this->assertSearchMapAndQuery(array('kursort' => 'Küche'));
    $this->assertSearchMapAndQuery(array('mktext' => 'Küche'));
  }

  /**
   * @test
   */
  public function testKurstitel()
  {
    $this->assertSearchMapAndQuery(array('kurstitel' => 'Vertragskurs'));
  }

  /**
   * @test
   */
  public function testKategorie()
  {
    $this->assertSearchMapAndQuery(array('kategorie' => 1));
    $this->assertSearchMapAndQuery(array('kategorie' => 'Vorträge'));
  }

  /**
   * @test
   */
  public function testAussenstelle()
  {
    $this->assertSearchMapAndQuery(array('aussenstelle' => 'Demohausen'));
    $this->assertSearchMapAndQuery(array('bezeichn' => 'Demohausen'));
  }

  /**
   * @test
   */
  public function testFachbereich()
  {
    $this->assertSearchMapAndQuery(array('fachbereich' => '12'));
  }

  /**
   * @test
   */
  public function testAusfall()
  {
    $this->assertSearchMapAndQuery(array('ausfall' => \DBWorkKurseBase::FILTER_KURSAUSFALL_OHNE_AUSGEFALLENE));
    $this->assertSearchMapAndQuery(array('kursausfall' => \DBWorkKurseBase::FILTER_KURSAUSFALL_OHNE_AUSGEFALLENE));
    $this->assertSearchMapAndQuery(array('ausfall' => \DBWorkKurseBase::FILTER_KURSAUSFALL_NUR_AUSGEFALLENE));
    $this->assertSearchMapAndQuery(array('ausfall' => 'ErrorTest'), false);
    $this->assertSearchMapAndQuery(array('ausfall' => ''), false);
  }

  /**
   * @test
   */
  public function testAbschluss()
  {
    $this->assertSearchMapAndQuery(array('abschluss' => \DBWorkKurseBase::FILTER_KURSABSCHLUSS_OHNE_ABGESCHLOSSENE));
    $this->assertSearchMapAndQuery(array('kursabschluss' => \DBWorkKurseBase::FILTER_KURSABSCHLUSS_OHNE_ABGESCHLOSSENE));
    $this->assertSearchMapAndQuery(array('abschluss' => \DBWorkKurseBase::FILTER_KURSABSCHLUSS_NUR_ABGESCHLOSSENE));
    $this->assertSearchMapAndQuery(array('abschluss' => 'ErrorTest'), false);
    $this->assertSearchMapAndQuery(array('abschluss' => ''), false);
  }

  /**
   * @test
   */
  public function testStatus()
  {
    $this->assertSearchMapAndQuery(array('status' => \DBWorkKurseBase::FILTER_KURSSTATUS_BEENDETE));
    $this->assertSearchMapAndQuery(array('status' => \DBWorkKurseBase::FILTER_KURSSTATUS_LAUFENDE_BY_DATUM));
    $this->assertSearchMapAndQuery(array('status' => \DBWorkKurseBase::FILTER_KURSSTATUS_NICHT_BEGONNENE));
    $this->assertSearchMapAndQuery(array('kursstatus' => \DBWorkKurseBase::FILTER_KURSSTATUS_BEENDETE));
    $this->assertSearchMapAndQuery(array('kursstatus' => \DBWorkKurseBase::FILTER_KURSSTATUS_LAUFENDE_BY_DATUM));
    $this->assertSearchMapAndQuery(array('kursstatus' => \DBWorkKurseBase::FILTER_KURSSTATUS_NICHT_BEGONNENE));
    $this->assertSearchMapAndQuery(array('status' => 'ErrorTest'), false);
    $this->assertSearchMapAndQuery(array('status' => ''), false);
  }
}
