API updadte
This commit is contained in:
28
tests/Api/ApiDocumentationTest.php
Normal file
28
tests/Api/ApiDocumentationTest.php
Normal file
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace App\Tests\Api;
|
||||
|
||||
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
|
||||
|
||||
class ApiDocumentationTest extends WebTestCase
|
||||
{
|
||||
public function testSwaggerUIAccessible(): void
|
||||
{
|
||||
$client = static::createClient();
|
||||
|
||||
// Test: Swagger UI ist öffentlich zugänglich
|
||||
$client->request('GET', '/api/docs.html');
|
||||
|
||||
$this->assertResponseIsSuccessful();
|
||||
}
|
||||
|
||||
public function testOpenAPIJsonLdAccessible(): void
|
||||
{
|
||||
$client = static::createClient();
|
||||
|
||||
// Test: OpenAPI JSON-LD ist öffentlich zugänglich
|
||||
$client->request('GET', '/api/docs.jsonld');
|
||||
|
||||
$this->assertResponseIsSuccessful();
|
||||
}
|
||||
}
|
||||
50
tests/Api/BundeslandApiTest.php
Normal file
50
tests/Api/BundeslandApiTest.php
Normal file
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
namespace App\Tests\Api;
|
||||
|
||||
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
|
||||
|
||||
class BundeslandApiTest extends WebTestCase
|
||||
{
|
||||
public function testGetBundeslaenderPublicAccess(): void
|
||||
{
|
||||
$client = static::createClient();
|
||||
|
||||
// Test: Bundesländer können ohne API-Key abgerufen werden
|
||||
$client->request('GET', '/api/bundeslands');
|
||||
|
||||
$this->assertResponseIsSuccessful();
|
||||
$this->assertResponseHeaderSame('content-type', 'application/ld+json; charset=utf-8');
|
||||
}
|
||||
|
||||
public function testGetSingleBundeslandPublicAccess(): void
|
||||
{
|
||||
$client = static::createClient();
|
||||
|
||||
// Test: Einzelnes Bundesland kann ohne API-Key abgerufen werden
|
||||
$client->request('GET', '/api/bundeslands/1');
|
||||
|
||||
// Response kann 200 (OK) oder 404 (Not Found) sein, beides ist akzeptabel
|
||||
$this->assertResponseStatusCodeSame(200);
|
||||
}
|
||||
|
||||
public function testCreateBundeslandRequiresAuthentication(): void
|
||||
{
|
||||
$client = static::createClient();
|
||||
|
||||
// Test: Bundesland erstellen ohne API-Key sollte fehlschlagen
|
||||
$client->request(
|
||||
'POST',
|
||||
'/api/bundeslands',
|
||||
[],
|
||||
[],
|
||||
['CONTENT_TYPE' => 'application/json'],
|
||||
json_encode([
|
||||
'name' => 'Test Bundesland',
|
||||
'grunderwerbsteuer' => 5.0,
|
||||
])
|
||||
);
|
||||
|
||||
$this->assertResponseStatusCodeSame(403); // Access Denied (no authentication on this firewall)
|
||||
}
|
||||
}
|
||||
49
tests/Api/HeizungstypApiTest.php
Normal file
49
tests/Api/HeizungstypApiTest.php
Normal file
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
namespace App\Tests\Api;
|
||||
|
||||
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
|
||||
|
||||
class HeizungstypApiTest extends WebTestCase
|
||||
{
|
||||
public function testGetHeizungstypenPublicAccess(): void
|
||||
{
|
||||
$client = static::createClient();
|
||||
|
||||
// Test: Heizungstypen können ohne API-Key abgerufen werden
|
||||
$client->request('GET', '/api/heizungstyps');
|
||||
|
||||
$this->assertResponseIsSuccessful();
|
||||
$this->assertResponseHeaderSame('content-type', 'application/ld+json; charset=utf-8');
|
||||
}
|
||||
|
||||
public function testGetSingleHeizungstypPublicAccess(): void
|
||||
{
|
||||
$client = static::createClient();
|
||||
|
||||
// Test: Einzelner Heizungstyp kann ohne API-Key abgerufen werden
|
||||
$client->request('GET', '/api/heizungstyps/1');
|
||||
|
||||
// Response kann 200 (OK) oder 404 (Not Found) sein
|
||||
$this->assertResponseStatusCodeSame(200);
|
||||
}
|
||||
|
||||
public function testCreateHeizungstypRequiresAuthentication(): void
|
||||
{
|
||||
$client = static::createClient();
|
||||
|
||||
// Test: Heizungstyp erstellen ohne API-Key sollte fehlschlagen
|
||||
$client->request(
|
||||
'POST',
|
||||
'/api/heizungstyps',
|
||||
[],
|
||||
[],
|
||||
['CONTENT_TYPE' => 'application/json'],
|
||||
json_encode([
|
||||
'name' => 'Test Heizung',
|
||||
])
|
||||
);
|
||||
|
||||
$this->assertResponseStatusCodeSame(401); // Unauthorized
|
||||
}
|
||||
}
|
||||
65
tests/Entity/BundeslandTest.php
Normal file
65
tests/Entity/BundeslandTest.php
Normal file
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
|
||||
namespace App\Tests\Entity;
|
||||
|
||||
use App\Entity\Bundesland;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class BundeslandTest extends TestCase
|
||||
{
|
||||
public function testBundeslandCreation(): void
|
||||
{
|
||||
$bundesland = new Bundesland();
|
||||
$bundesland->setName('Bayern');
|
||||
$bundesland->setGrunderwerbsteuer(3.5);
|
||||
|
||||
$this->assertEquals('Bayern', $bundesland->getName());
|
||||
$this->assertEquals(3.5, $bundesland->getGrunderwerbsteuer());
|
||||
}
|
||||
|
||||
public function testGrunderwerbsteuerValues(): void
|
||||
{
|
||||
$testCases = [
|
||||
['Baden-Württemberg', 5.0],
|
||||
['Bayern', 3.5],
|
||||
['Berlin', 6.0],
|
||||
['Brandenburg', 6.5],
|
||||
['Bremen', 5.0],
|
||||
['Hamburg', 5.5],
|
||||
['Hessen', 6.0],
|
||||
['Mecklenburg-Vorpommern', 6.0],
|
||||
['Niedersachsen', 5.0],
|
||||
['Nordrhein-Westfalen', 6.5],
|
||||
['Rheinland-Pfalz', 5.0],
|
||||
['Saarland', 6.5],
|
||||
['Sachsen', 5.5],
|
||||
['Sachsen-Anhalt', 5.0],
|
||||
['Schleswig-Holstein', 6.5],
|
||||
['Thüringen', 5.0],
|
||||
];
|
||||
|
||||
foreach ($testCases as [$name, $steuer]) {
|
||||
$bundesland = new Bundesland();
|
||||
$bundesland->setName($name);
|
||||
$bundesland->setGrunderwerbsteuer($steuer);
|
||||
|
||||
$this->assertEquals($name, $bundesland->getName());
|
||||
$this->assertEquals($steuer, $bundesland->getGrunderwerbsteuer());
|
||||
}
|
||||
}
|
||||
|
||||
public function testMinMaxGrunderwerbsteuer(): void
|
||||
{
|
||||
// Niedrigster Satz: Bayern mit 3.5%
|
||||
$bayern = new Bundesland();
|
||||
$bayern->setName('Bayern');
|
||||
$bayern->setGrunderwerbsteuer(3.5);
|
||||
$this->assertEquals(3.5, $bayern->getGrunderwerbsteuer());
|
||||
|
||||
// Höchster Satz: Brandenburg, NRW, Saarland, SH mit 6.5%
|
||||
$nrw = new Bundesland();
|
||||
$nrw->setName('Nordrhein-Westfalen');
|
||||
$nrw->setGrunderwerbsteuer(6.5);
|
||||
$this->assertEquals(6.5, $nrw->getGrunderwerbsteuer());
|
||||
}
|
||||
}
|
||||
29
tests/Entity/HeizungstypTest.php
Normal file
29
tests/Entity/HeizungstypTest.php
Normal file
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace App\Tests\Entity;
|
||||
|
||||
use App\Entity\Heizungstyp;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class HeizungstypTest extends TestCase
|
||||
{
|
||||
public function testHeizungstypCreation(): void
|
||||
{
|
||||
$heizungstyp = new Heizungstyp();
|
||||
$heizungstyp->setName('Wärmepumpe');
|
||||
|
||||
$this->assertEquals('Wärmepumpe', $heizungstyp->getName());
|
||||
}
|
||||
|
||||
public function testCommonHeizungstypen(): void
|
||||
{
|
||||
$typen = ['Gasheizung', 'Wärmepumpe', 'Ölheizung', 'Fernwärme', 'Pelletheizung'];
|
||||
|
||||
foreach ($typen as $typName) {
|
||||
$heizungstyp = new Heizungstyp();
|
||||
$heizungstyp->setName($typName);
|
||||
|
||||
$this->assertEquals($typName, $heizungstyp->getName());
|
||||
}
|
||||
}
|
||||
}
|
||||
160
tests/Entity/ImmobilieTest.php
Normal file
160
tests/Entity/ImmobilieTest.php
Normal file
@@ -0,0 +1,160 @@
|
||||
<?php
|
||||
|
||||
namespace App\Tests\Entity;
|
||||
|
||||
use App\Entity\Bundesland;
|
||||
use App\Entity\Immobilie;
|
||||
use App\Entity\User;
|
||||
use App\Enum\ImmobilienTyp;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class ImmobilieTest extends TestCase
|
||||
{
|
||||
private User $verwalter;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->verwalter = new User();
|
||||
$this->verwalter->setName('Test Verwalter');
|
||||
$this->verwalter->setEmail('verwalter@example.com');
|
||||
}
|
||||
|
||||
public function testImmobilieCreation(): void
|
||||
{
|
||||
$immobilie = new Immobilie();
|
||||
$immobilie->setVerwalter($this->verwalter);
|
||||
$immobilie->setAdresse('Teststraße 123, 12345 Teststadt');
|
||||
$immobilie->setWohnflaeche(100);
|
||||
$immobilie->setNutzflaeche(20);
|
||||
$immobilie->setZimmer(4);
|
||||
$immobilie->setTyp(ImmobilienTyp::WOHNUNG);
|
||||
|
||||
$this->assertEquals('Teststraße 123, 12345 Teststadt', $immobilie->getAdresse());
|
||||
$this->assertEquals(100, $immobilie->getWohnflaeche());
|
||||
$this->assertEquals(20, $immobilie->getNutzflaeche());
|
||||
$this->assertEquals(4, $immobilie->getZimmer());
|
||||
$this->assertEquals(ImmobilienTyp::WOHNUNG, $immobilie->getTyp());
|
||||
$this->assertEquals($this->verwalter, $immobilie->getVerwalter());
|
||||
}
|
||||
|
||||
public function testGesamtflaecheCalculation(): void
|
||||
{
|
||||
$immobilie = new Immobilie();
|
||||
$immobilie->setVerwalter($this->verwalter);
|
||||
$immobilie->setAdresse('Test');
|
||||
$immobilie->setWohnflaeche(85);
|
||||
$immobilie->setNutzflaeche(15);
|
||||
$immobilie->setZimmer(3);
|
||||
|
||||
$this->assertEquals(100, $immobilie->getGesamtflaeche());
|
||||
}
|
||||
|
||||
public function testKaufnebenkostenWithoutBundesland(): void
|
||||
{
|
||||
$immobilie = new Immobilie();
|
||||
$immobilie->setVerwalter($this->verwalter);
|
||||
$immobilie->setAdresse('Test');
|
||||
$immobilie->setWohnflaeche(100);
|
||||
$immobilie->setNutzflaeche(0);
|
||||
$immobilie->setZimmer(3);
|
||||
$immobilie->setKaufpreis(300000);
|
||||
|
||||
$kosten = $immobilie->getKaufnebenkosten();
|
||||
|
||||
$this->assertEquals(0, $kosten['notar']);
|
||||
$this->assertEquals(0, $kosten['grundbuch']);
|
||||
$this->assertEquals(0, $kosten['grunderwerbsteuer']);
|
||||
$this->assertEquals(0, $kosten['gesamt']);
|
||||
}
|
||||
|
||||
public function testKaufnebenkostenWithBundesland(): void
|
||||
{
|
||||
$bundesland = new Bundesland();
|
||||
$bundesland->setName('Bayern');
|
||||
$bundesland->setGrunderwerbsteuer(3.5);
|
||||
|
||||
$immobilie = new Immobilie();
|
||||
$immobilie->setVerwalter($this->verwalter);
|
||||
$immobilie->setAdresse('Test');
|
||||
$immobilie->setWohnflaeche(100);
|
||||
$immobilie->setNutzflaeche(0);
|
||||
$immobilie->setZimmer(3);
|
||||
$immobilie->setKaufpreis(300000);
|
||||
$immobilie->setBundesland($bundesland);
|
||||
|
||||
$kosten = $immobilie->getKaufnebenkosten();
|
||||
|
||||
// Notar: 1.5% von 300000 = 4500
|
||||
$this->assertEquals(4500, $kosten['notar']);
|
||||
|
||||
// Grundbuch: 0.5% von 300000 = 1500
|
||||
$this->assertEquals(1500, $kosten['grundbuch']);
|
||||
|
||||
// Grunderwerbsteuer: 3.5% von 300000 = 10500
|
||||
$this->assertEqualsWithDelta(10500, $kosten['grunderwerbsteuer'], 0.01);
|
||||
|
||||
// Gesamt: 4500 + 1500 + 10500 = 16500
|
||||
$this->assertEquals(16500, $kosten['gesamt']);
|
||||
}
|
||||
|
||||
public function testKaufnebenkostenWithDifferentBundesland(): void
|
||||
{
|
||||
$bundesland = new Bundesland();
|
||||
$bundesland->setName('Nordrhein-Westfalen');
|
||||
$bundesland->setGrunderwerbsteuer(6.5);
|
||||
|
||||
$immobilie = new Immobilie();
|
||||
$immobilie->setVerwalter($this->verwalter);
|
||||
$immobilie->setAdresse('Test');
|
||||
$immobilie->setWohnflaeche(100);
|
||||
$immobilie->setNutzflaeche(0);
|
||||
$immobilie->setZimmer(3);
|
||||
$immobilie->setKaufpreis(400000);
|
||||
$immobilie->setBundesland($bundesland);
|
||||
|
||||
$kosten = $immobilie->getKaufnebenkosten();
|
||||
|
||||
// Notar: 1.5% von 400000 = 6000
|
||||
$this->assertEquals(6000, $kosten['notar']);
|
||||
|
||||
// Grundbuch: 0.5% von 400000 = 2000
|
||||
$this->assertEquals(2000, $kosten['grundbuch']);
|
||||
|
||||
// Grunderwerbsteuer: 6.5% von 400000 = 26000
|
||||
$this->assertEquals(26000, $kosten['grunderwerbsteuer']);
|
||||
|
||||
// Gesamt: 6000 + 2000 + 26000 = 34000
|
||||
$this->assertEquals(34000, $kosten['gesamt']);
|
||||
}
|
||||
|
||||
public function testDefaultValues(): void
|
||||
{
|
||||
$immobilie = new Immobilie();
|
||||
|
||||
$this->assertEquals(ImmobilienTyp::WOHNUNG, $immobilie->getTyp());
|
||||
$this->assertEquals(0, $immobilie->getNutzflaeche());
|
||||
$this->assertFalse($immobilie->getGarage());
|
||||
$this->assertNotNull($immobilie->getCreatedAt());
|
||||
$this->assertNotNull($immobilie->getUpdatedAt());
|
||||
}
|
||||
|
||||
public function testOptionalFields(): void
|
||||
{
|
||||
$immobilie = new Immobilie();
|
||||
$immobilie->setVerwalter($this->verwalter);
|
||||
$immobilie->setAdresse('Test');
|
||||
$immobilie->setWohnflaeche(100);
|
||||
$immobilie->setNutzflaeche(0);
|
||||
$immobilie->setZimmer(3);
|
||||
|
||||
$immobilie->setBaujahr(2020);
|
||||
$immobilie->setEtage(3);
|
||||
$immobilie->setAbschreibungszeit(50);
|
||||
$immobilie->setBeschreibung('Schöne Wohnung');
|
||||
|
||||
$this->assertEquals(2020, $immobilie->getBaujahr());
|
||||
$this->assertEquals(3, $immobilie->getEtage());
|
||||
$this->assertEquals(50, $immobilie->getAbschreibungszeit());
|
||||
$this->assertEquals('Schöne Wohnung', $immobilie->getBeschreibung());
|
||||
}
|
||||
}
|
||||
92
tests/Entity/UserTest.php
Normal file
92
tests/Entity/UserTest.php
Normal file
@@ -0,0 +1,92 @@
|
||||
<?php
|
||||
|
||||
namespace App\Tests\Entity;
|
||||
|
||||
use App\Entity\User;
|
||||
use App\Enum\UserRole;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class UserTest extends TestCase
|
||||
{
|
||||
public function testUserCreation(): void
|
||||
{
|
||||
$user = new User();
|
||||
$user->setName('Max Mustermann');
|
||||
$user->setEmail('max@example.com');
|
||||
$user->setRole(UserRole::USER);
|
||||
|
||||
$this->assertEquals('Max Mustermann', $user->getName());
|
||||
$this->assertEquals('max@example.com', $user->getEmail());
|
||||
$this->assertEquals(UserRole::USER, $user->getRole());
|
||||
$this->assertNotNull($user->getApiKey());
|
||||
$this->assertNotNull($user->getCreatedAt());
|
||||
}
|
||||
|
||||
public function testApiKeyGeneration(): void
|
||||
{
|
||||
$user = new User();
|
||||
$user->setName('Test User');
|
||||
$user->setEmail('test@example.com');
|
||||
|
||||
$apiKey = $user->getApiKey();
|
||||
|
||||
$this->assertNotEmpty($apiKey);
|
||||
$this->assertEquals(64, strlen($apiKey)); // SHA256 hash length
|
||||
}
|
||||
|
||||
public function testRegenerateApiKey(): void
|
||||
{
|
||||
$user = new User();
|
||||
$user->setName('Test User');
|
||||
$user->setEmail('test@example.com');
|
||||
|
||||
$originalKey = $user->getApiKey();
|
||||
$user->regenerateApiKey();
|
||||
$newKey = $user->getApiKey();
|
||||
|
||||
$this->assertNotEquals($originalKey, $newKey);
|
||||
$this->assertEquals(64, strlen($newKey));
|
||||
}
|
||||
|
||||
public function testUserRoles(): void
|
||||
{
|
||||
// Test USER role
|
||||
$user = new User();
|
||||
$user->setRole(UserRole::USER);
|
||||
$this->assertContains('ROLE_USER', $user->getRoles());
|
||||
|
||||
// Test ADMIN role
|
||||
$admin = new User();
|
||||
$admin->setRole(UserRole::ADMIN);
|
||||
$this->assertContains('ROLE_ADMIN', $admin->getRoles());
|
||||
$this->assertContains('ROLE_USER', $admin->getRoles());
|
||||
|
||||
// Test TECHNICAL role
|
||||
$technical = new User();
|
||||
$technical->setRole(UserRole::TECHNICAL);
|
||||
$this->assertContains('ROLE_TECHNICAL', $technical->getRoles());
|
||||
$this->assertContains('ROLE_USER', $technical->getRoles());
|
||||
|
||||
// Test MODERATOR role
|
||||
$moderator = new User();
|
||||
$moderator->setRole(UserRole::MODERATOR);
|
||||
$this->assertContains('ROLE_MODERATOR', $moderator->getRoles());
|
||||
$this->assertContains('ROLE_USER', $moderator->getRoles());
|
||||
}
|
||||
|
||||
public function testUserIdentifier(): void
|
||||
{
|
||||
$user = new User();
|
||||
$user->setEmail('identifier@example.com');
|
||||
|
||||
$this->assertEquals('identifier@example.com', $user->getUserIdentifier());
|
||||
}
|
||||
|
||||
public function testDefaultRole(): void
|
||||
{
|
||||
$user = new User();
|
||||
|
||||
// Default role should be USER
|
||||
$this->assertEquals(UserRole::USER, $user->getRole());
|
||||
}
|
||||
}
|
||||
13
tests/bootstrap.php
Normal file
13
tests/bootstrap.php
Normal file
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
use Symfony\Component\Dotenv\Dotenv;
|
||||
|
||||
require dirname(__DIR__).'/vendor/autoload.php';
|
||||
|
||||
if (method_exists(Dotenv::class, 'bootEnv')) {
|
||||
(new Dotenv())->bootEnv(dirname(__DIR__).'/.env');
|
||||
}
|
||||
|
||||
if ($_SERVER['APP_DEBUG']) {
|
||||
umask(0000);
|
||||
}
|
||||
Reference in New Issue
Block a user