Skip to content

valeriolg/docker-php-sqlite

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

docker-php-sqlite

Features

  • PHP API for order creation
  • SQLite database for persistence
  • PHPUnit tests for API endpoints
  • Docker

Project Structure

.
├── scripts/
│   ├── build.sh   # Install dependencies and initialize the database
│   ├── run.sh     # Start the Apache server on port 9090
│   └── test.sh    # Run syntax checks and PHPUnit tests
├── src/           # PHP source code (API, services, config)
├── tests/         # PHPUnit test cases
├── Dockerfile     # Docker build instructions
└── README.md

Database Structure

The SQLite database consists of the following tables:

  • vat_rates

    • vat_rate_id
    • name
    • rate
  • products

    • product_id
    • price
    • vat_rate_id
  • orders

    • order_id
    • price
    • vat
  • order_items

    • order_id
    • product_id
    • discount_percentage

Sample Data:
On first initialization, the database is seeded with:

  • One VAT rate (default, 10%)
  • Three products:
    • Product 1: price 2.00
    • Product 2: price 1.50
    • Product 3: price 3.00

API Example

POST /api/order/create

Request body:

{
  "order": {
    "items": [
      { "product_id": 1, "quantity": 1 },
      { "product_id": 2, "quantity": 5 },
      { "product_id": 3, "quantity": 1 }
    ]
  }
}

Expected response:

{
  "order_id": 3412433,
  "order_price": 12.50,
  "order_vat": 1.25,
  "items": [
    {
      "product_id": 1,
      "quantity": 1,
      "price": 2.00,
      "vat": 0.20
    },
    {
      "product_id": 2,
      "quantity": 5,
      "price": 7.50,
      "vat": 0.75
    },
    {
      "product_id": 3,
      "quantity": 1,
      "price": 3.00,
      "vat": 0.30
    }
  ]
}

Scripts

  • build.sh
    Installs Composer dependencies, ensures the SQLite database directory exists, and initializes the database schema and sample data.

  • test.sh
    Runs PHP syntax checks on all source files and executes PHPUnit tests.

  • run.sh
    Configures Apache to listen on port 9090 and starts the server in the foreground.

Usage

1. Build the Docker image

docker build -t mytest .

2. Initialize the application (install dependencies, setup DB)

docker run -v $(pwd):/mnt -p 9090:9090 -w /mnt mytest ./scripts/build.sh

3. Run Tests

docker run -v $(pwd):/mnt -p 9090:9090 -w /mnt mytest ./scripts/test.sh

4. Start the application

docker run -v $(pwd):/mnt -p 9090:9090 -w /mnt mytest ./scripts/run.sh

You can test the API using curl:

curl -X POST localhost:9090/api/order/create \
  -H "Content-Type: application/json" \
  -d '{
    "order": {
      "items": [
        {
          "product_id": 1,
          "quantity": 1
        },
        {
          "product_id": 2,
          "quantity": 5
        },
        {
          "product_id": 3,
          "quantity": 1
        }
      ]
    }
  }'

Running Tests

Tests are written with PHPUnit and cover both successful and error scenarios for the order creation API.
To run the tests, use the test.sh script as shown above.


Note:
All scripts are intended to be run inside the Docker container as shown above.
The -v $(pwd):/mnt flag mounts your project directory into the container, and -w /mnt sets it as the working directory.


About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published