PHP Classes

File: tests/ApiGeneratorTest.php

Recommend this page to a friend!
  Classes of Adrian M   PHP CRUD API Generator   tests/ApiGeneratorTest.php   Download  
File: tests/ApiGeneratorTest.php
Role: Class source
Content type: text/plain
Description: Class source
Class: PHP CRUD API Generator
Create an API to access MySQL database record
Author: By
Last change:
Date: 1 month ago
Size: 1,998 bytes
 

Contents

Class file image Download
<?php
use PHPUnit\Framework\TestCase;
use
App\Database;
use
App\ApiGenerator;
use
App\SchemaInspector;

class
ApiGeneratorTest extends TestCase
{
    private
Database $db;
    private
ApiGenerator $api;
    private
string $table = 'test_table';

    public static function
setUpBeforeClass(): void
   
{
       
$dbConfig = require __DIR__ . '/../config/db.php';
       
$pdo = (new App\Database($dbConfig))->getPdo();
       
$pdo->exec("DROP TABLE IF EXISTS test_table");
       
$pdo->exec("CREATE TABLE test_table (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255))");
    }

    public static function
tearDownAfterClass(): void
   
{
       
$dbConfig = require __DIR__ . '/../config/db.php';
       
$pdo = (new App\Database($dbConfig))->getPdo();
       
$pdo->exec("DROP TABLE IF EXISTS test_table");
    }

    protected function
setUp(): void
   
{
       
$dbConfig = require __DIR__ . '/../config/db.php';
       
$this->db = new App\Database($dbConfig);
       
$this->api = new App\ApiGenerator($this->db->getPdo());
    }

    public function
testCreateAndRead()
    {
       
$row = $this->api->create($this->table, ['name' => 'Alice']);
       
$this->assertEquals('Alice', $row['name']);
       
$read = $this->api->read($this->table, $row['id']);
       
$this->assertEquals('Alice', $read['name']);
    }

    public function
testUpdate()
    {
       
$row = $this->api->create($this->table, ['name' => 'Bob']);
       
$updated = $this->api->update($this->table, $row['id'], ['name' => 'Bobby']);
       
$this->assertEquals('Bobby', $updated['name']);
    }

    public function
testDelete()
    {
       
$row = $this->api->create($this->table, ['name' => 'Charlie']);
       
$deleted = $this->api->delete($this->table, $row['id']);
       
$this->assertTrue($deleted);
    }

    public function
testList()
    {
       
$this->api->create($this->table, ['name' => 'Daisy']);
       
$list = $this->api->list($this->table);
       
$this->assertIsArray($list);
    }
}