# BuyerKiosk Tests

This directory contains tests for the BuyerKiosk application.

## Running Tests

You can use the `test.sh` script to run tests and optionally trigger a deployment:

### Basic Usage

Run tests without deployment:

```bash
./test.sh
```

Run tests and trigger deployment:

```bash
./test.sh --deploy
```

or use the deploy.sh shortcut:

```bash
./deploy.sh
```

### Additional Options

Show all warnings and deprecation notices (verbose mode):

```bash
./test.sh --verbose
```

or

```bash
./test.sh -v
```

Combine options:

```bash
./test.sh --verbose --deploy
```

or

```bash
./deploy.sh --verbose
```

## Test Structure

- `Unit/`: Contains unit tests for individual components
  - `KLoggerTest.php`: Tests for the KLogger class
  - `AccessConditionExpressionTest.php`: Tests for the AccessConditionExpression class
  - `SimpleTest.php`: A simple test that always passes
  - `UserDatabaseTest.php`: Tests for database operations with the User model

## Database Testing

The application includes support for database testing. Database tests:

1. Connect to a test database (configured in the .env file)
2. Run tests within a transaction
3. Roll back the transaction after each test to keep the database clean

### Setting Up Database Tests

1. Create a test database (you can use your existing database or create a separate test database)
2. Configure the database connection in your .env file:
   ```
   DB_HOST=localhost
   DB_DATABASE=buyerkiosk_db
   DB_USERNAME=root
   DB_PASSWORD=your_password
   ```

3. Optionally, you can specify a separate test database:
   ```
   DB_TEST_NAME=buyerkiosk_test
   ```
   If `DB_TEST_NAME` is not specified, the tests will use the database specified in `DB_DATABASE`.

### Creating Database Tests

To create a database test:

1. Extend the `Tests\DatabaseTestCase` class
2. Use the provided database connection and helper methods
3. Write tests that interact with the database

Example:

```php
<?php

namespace Tests\Unit;

use Tests\DatabaseTestCase;
use YourApp\YourModel;

class YourModelTest extends DatabaseTestCase
{
    public function testCreateRecord(): void
    {
        // Skip this test if no database connection
        if (!$this->db) {
            $this->markTestSkipped('No database connection available.');
        }
        
        // Create a new record
        $model = new YourModel($this->db);
        $id = $model->create(['field' => 'value']);
        
        // Verify the record was created
        $result = $this->executeQuery(
            "SELECT * FROM your_table WHERE id = :id",
            [':id' => $id]
        );
        
        $this->assertCount(1, $result);
        $this->assertEquals('value', $result[0]['field']);
    }
}
```

## Adding New Tests

To add a new test:

1. Create a new test file in the appropriate directory (e.g., `Unit/`)
2. Extend the `PHPUnit\Framework\TestCase` class (or `Tests\DatabaseTestCase` for database tests)
3. Add test methods that start with `test`
4. Run the tests to ensure they pass

Example:

```php
<?php

namespace Tests\Unit;

use PHPUnit\Framework\TestCase;

class MyTest extends TestCase
{
    public function testSomething(): void
    {
        $this->assertTrue(true);
    }
}
``` 