-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add lab #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
quang-do-se
wants to merge
1
commit into
main
Choose a base branch
from
lab
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| ARG VARIANT=8.5-bookworm | ||
| FROM mcr.microsoft.com/devcontainers/php:4-${VARIANT} | ||
|
|
||
| RUN apt-get update \ | ||
| && export DEBIAN_FRONTEND=noninteractive \ | ||
| && apt-get install -y --no-install-recommends libsqlite3-dev \ | ||
| && docker-php-ext-install pdo_sqlite \ | ||
| && apt-get clean \ | ||
| && rm -rf /var/lib/apt/lists/* | ||
|
|
||
| COPY php-memory-limit.ini /usr/local/etc/php/conf.d/php-memory-limit.ini |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| memory_limit = 32M |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| /data/*.sqlite | ||
| /data/*.sqlite-shm | ||
| /data/*.sqlite-wal | ||
| /data/*.jsonl |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,32 +1,103 @@ | ||
| # PHP Technical Assessment | ||
|
|
||
| This repository contains the starter project for the technical interview. | ||
| # Senior Interview Lab | ||
|
|
||
| ## Getting started | ||
|
|
||
| 1. Open this repository in GitHub Codespaces. | ||
| 2. Wait for the Codespace setup to finish. | ||
| 3. Open a terminal in the Codespace. | ||
|
|
||
| Run the starter program: | ||
| ## Introduction | ||
|
|
||
| This repository contains two independent PHP exercises: | ||
|
|
||
| 1. **User maintenance** | ||
| 2. **Order processing** | ||
|
|
||
| The lab is dependency-free and uses file-backed **SQLite** databases. You do not | ||
| need Docker, MySQL, PostgreSQL, Composer, or any third-party packages. | ||
|
|
||
| You can also list the available lab commands: | ||
|
|
||
| ```bash | ||
| php lab.php help | ||
| ``` | ||
|
|
||
| ## Candidate instructions | ||
|
|
||
| For each exercise: | ||
|
|
||
| ```sh | ||
| php index.php | ||
| 1. Explain how the existing implementation works. | ||
| 2. Identify anything you think should be improved. | ||
| 3. Make appropriate changes while preserving intended behavior. | ||
| 4. Explain your decisions and trade-offs. | ||
|
|
||
| You are encouraged to use your judgment about what to investigate and how far to refactor the | ||
| exercise. You may make code changes wherever appropriate. | ||
|
|
||
| Do not add external dependencies or require additional infrastructure. | ||
|
|
||
| The exercises are independent. Complete them in the order directed by your interviewer. | ||
|
|
||
| ## Exercise 1 — User maintenance | ||
|
|
||
| Prepare a fresh user database: | ||
|
|
||
| ```bash | ||
| php lab.php setup:users | ||
| ``` | ||
|
|
||
| The expected starter output is: | ||
| Review and improve: | ||
|
|
||
| ```text | ||
| Hello, World! | ||
| code exercises/01_user_maintenance.php | ||
| ``` | ||
|
|
||
| ## Coding agent | ||
| Run the exercise: | ||
|
|
||
| ```bash | ||
| php exercises/01_user_maintenance.php | ||
| ``` | ||
|
|
||
| Codex is available from the Codespace terminal: | ||
| Inspect the resulting user statuses: | ||
|
|
||
| ```sh | ||
| codex | ||
| ```bash | ||
| php lab.php inspect:users | ||
| ``` | ||
|
|
||
| Follow the interviewer's instructions for the assessment and submission | ||
| process. | ||
| Re-run the reset command whenever you need to restore the original dataset: | ||
|
|
||
| ```bash | ||
| php lab.php reset:users | ||
| ``` | ||
|
|
||
| ## Exercise 2 — Order processing | ||
|
|
||
| Prepare a fresh order database: | ||
|
|
||
| ```bash | ||
| php lab.php setup:orders | ||
| ``` | ||
|
|
||
| Review and improve: | ||
|
|
||
| ```text | ||
| code exercises/02_order_processing.php | ||
| ``` | ||
|
|
||
| Run the exercise: | ||
|
|
||
| ```bash | ||
| php exercises/02_order_processing.php | ||
| ``` | ||
|
|
||
| Inspect products, orders, order items, and payments: | ||
|
|
||
| ```bash | ||
| php lab.php inspect:orders | ||
| ``` | ||
|
|
||
| Re-run the setup command whenever you need a clean database: | ||
|
|
||
| ```bash | ||
| php lab.php reset:orders | ||
| ``` |
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| // ============================================================================= | ||
| // Setup | ||
| // ============================================================================= | ||
|
|
||
| // Load dependencies. | ||
| require_once __DIR__ . '/../support/Database.php'; | ||
|
|
||
| // Connect to the exercise database. | ||
| $dbPath = __DIR__ . '/../data/users.sqlite'; | ||
| $pdo = Database::connect($dbPath); | ||
|
|
||
| // ============================================================================= | ||
| // Exercise | ||
| // ============================================================================= | ||
|
|
||
| $users = $pdo->query(<<<'SQL' | ||
| SELECT * | ||
| FROM users | ||
| WHERE status = 'active' | ||
| AND last_login_at < '2025-08-13' | ||
| ORDER BY id | ||
| SQL)->fetchAll(); | ||
|
|
||
| foreach ($users as $user) { | ||
| $stmt = $pdo->prepare("UPDATE users SET status = 'inactive' WHERE id = ?"); | ||
| $stmt->execute([$user['id']]); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| // ============================================================================= | ||
| // Setup | ||
| // ============================================================================= | ||
|
|
||
| // Load dependencies. | ||
| require_once __DIR__ . '/../support/Database.php'; | ||
|
|
||
| // Parse command-line options and connect to the exercise database. | ||
| $pdo = Database::connect(__DIR__ . '/../data/orders.sqlite'); | ||
|
|
||
| // ============================================================================= | ||
| // Exercise | ||
| // ============================================================================= | ||
|
|
||
| // Define the products and quantities in the order. | ||
| $items = [ | ||
| ['product_id' => 1, 'quantity' => 2], | ||
| ['product_id' => 2, 'quantity' => 1], | ||
| ['product_id' => 3, 'quantity' => 3], | ||
| ]; | ||
|
|
||
| // Start processing the order | ||
| try { | ||
| $pdo->exec("INSERT INTO orders(status, total_cents, created_at) VALUES ('pending', 0, datetime('now'))"); | ||
| $orderId = (int) $pdo->lastInsertId(); | ||
|
|
||
| $total = 0; | ||
| foreach ($items as $item) { | ||
| $productStmt = $pdo->prepare('SELECT price_cents, stock FROM products WHERE id = ?'); | ||
| $productStmt->execute([$item['product_id']]); | ||
| $product = $productStmt->fetch(); | ||
|
|
||
| if (!$product) { | ||
| throw new RuntimeException('Product not found'); | ||
| } | ||
|
|
||
| if ((int) $product['stock'] < $item['quantity']) { | ||
| throw new RuntimeException('Insufficient stock'); | ||
| } | ||
|
|
||
| $lineTotal = (int) $product['price_cents'] * $item['quantity']; | ||
| $total += $lineTotal; | ||
|
|
||
| $insertItem = $pdo->prepare(<<<'SQL' | ||
| INSERT INTO order_items(order_id, product_id, quantity, unit_price_cents) | ||
| VALUES (?, ?, ?, ?) | ||
| SQL); | ||
| $insertItem->execute([$orderId, $item['product_id'], $item['quantity'], $product['price_cents']]); | ||
|
|
||
| $newStock = (int) $product['stock'] - $item['quantity']; | ||
| $updateStock = $pdo->prepare('UPDATE products SET stock = ? WHERE id = ?'); | ||
| $updateStock->execute([$newStock, $item['product_id']]); | ||
| } | ||
|
|
||
| $updateOrder = $pdo->prepare("UPDATE orders SET total_cents = ?, status = 'ready_for_payment' WHERE id = ?"); | ||
| $updateOrder->execute([$total, $orderId]); | ||
|
|
||
| $payment = $pdo->prepare("INSERT INTO payments(order_id, amount_cents, status) VALUES (?, ?, 'pending')"); | ||
| $payment->execute([$orderId, $total]); | ||
|
|
||
| echo "Order {$orderId} completed successfully.\n"; | ||
| } catch (Throwable $e) { | ||
| fwrite(STDERR, "FAILED: {$e->getMessage()}\n"); | ||
| fwrite(STDERR, "Inspect the DB now. Partial writes may remain.\n"); | ||
| exit(1); | ||
| } | ||
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.