Skip to content

Commit

Permalink
Refactor project: Transform single script into a structured application
Browse files Browse the repository at this point in the history
- Reorganized project structure into a PSR-4 compliant format.
- Introduced PHPUnit for automated testing with 100% coverage potential.
- Added Docker for containerized development and deployment.
- Configured PHP_CodeSniffer and PHP-CS-Fixer for code style and formatting.
- Updated SequenceGenerator to follow modern PHP practices (type safety, namespaces).
- Added composer.json scripts for test, linting, and formatting workflows.
- Enhanced documentation in README with installation, usage, and development steps.
- Provided code coverage capabilities with Xdebug/PCOV support.
  • Loading branch information
illmatix committed Nov 29, 2024
1 parent e33980f commit 1dd15a3
Show file tree
Hide file tree
Showing 16 changed files with 512 additions and 82 deletions.
16 changes: 16 additions & 0 deletions .devcontainer/.devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "PHP Dev Environment",
"build": {
"dockerfile": "../Dockerfile",
"context": ".."
},
"settings": {
"php.validate.executablePath": "/usr/local/bin/php"
},
"extensions": [
"xdebug.php-debug",
"bmewburn.vscode-intelephense-client"
],
"remoteUser": "www-data",
"forwardPorts": [80]
}
25 changes: 25 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: CI

on:
push:
branches:
- main
pull_request:
branches:
- main

jobs:
test:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Set up PHP
uses: shivammathur/setup-php@v2
with:
php-version: 8.2
extensions: mbstring, dom, xml
- name: Install dependencies
run: composer install
- name: Run tests
run: ./vendor/bin/phpunit
38 changes: 38 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Composer dependencies
vendor/

# PHPUnit-specific files
build/logs/
phpunit.xml.cache
.phpunit.result.cache

# Generated files and caches
*.log
*.cache
*.tmp
*.lock
.DS_Store
Thumbs.db

# IDE and editor-specific files
.idea/
.vscode/
*.iml

# Docker-related files
docker-compose.override.yml
.env
.env.local

# OS-generated files
*.swp
*.swo
*.bak
*~

# Generated public assets (if applicable)
public/assets/
public/build/

# Exclude test result artifacts
coverage/
22 changes: 22 additions & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

$finder = PhpCsFixer\Finder::create()
->in(['src', 'tests'])
->exclude(['vendor', 'public/assets']);

return (new PhpCsFixer\Config())
->setRules([
'@PSR12' => true,
'array_syntax' => ['syntax' => 'short'],
'binary_operator_spaces' => ['default' => 'align_single_space_minimal'],
'blank_line_after_namespace' => true,
'blank_line_after_opening_tag' => true,
'concat_space' => ['spacing' => 'one'],
'declare_strict_types' => true, // Risky fixer
'no_unused_imports' => true,
'single_quote' => true,
'ternary_operator_spaces' => true,
'trailing_comma_in_multiline' => true,
])
->setRiskyAllowed(true) // Enable risky fixers
->setFinder($finder);
10 changes: 10 additions & 0 deletions .run/SequenceGenerator.run.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="SequenceGenerator" type="PhpLocalRunConfigurationType" factoryName="PHP Console" path="$PROJECT_DIR$/public/index.php">
<CommandLine>
<PhpTestInterpreterSettings>
<option name="interpreterName" value="php:8.2-apache" />
</PhpTestInterpreterSettings>
</CommandLine>
<method v="2" />
</configuration>
</component>
31 changes: 31 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Use an official PHP image with Apache
FROM php:8.2-apache

# Install necessary dependencies
RUN apt-get update && apt-get install -y \
git \
unzip \
zip \
libzip-dev \
libxml2-dev \
&& docker-php-ext-install zip dom xml

RUN pecl install pcov && docker-php-ext-enable pcov

# Install Composer globally
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer

# Enable mod_rewrite for Apache
RUN a2enmod rewrite

# Set the working directory inside the container
WORKDIR /var/www/html

# Copy project files into the container
COPY . .

# Set proper permissions for Apache
RUN chown -R www-data:www-data /var/www/html

# Expose port 80 for Apache
EXPOSE 80
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 Chad Payne

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
94 changes: 94 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
# Random Sequence Generator

Welcome to the **Random Sequence Generator** project! This application generates randomized sequences of integers, which can be used for various purposes such as testing, data generation, or fun experiments. The project follows modern PHP standards (PSR-12) and includes automated testing with PHPUnit.

---

## Features

- Generate randomized sequences of integers.
- Define custom ranges for sequence generation.
- Fully tested with PHPUnit, including code coverage reporting.
- Supports PSR-12 coding standards and includes automated code formatting.
- Containerized environment for consistent development and testing.

---

## Getting Started

### Prerequisites

To run this project, ensure you have the following installed:

- [Docker](https://www.docker.com/)
- [Docker Compose](https://docs.docker.com/compose/)
- (Optional) [Composer](https://getcomposer.org/) if running locally.

---

### Installation

1. Clone the repository:
```bash
git clone https://github.com/illmatix/randomSequence10K.git
cd randomSequence10K
```
2. Start the Docker environment:
```bash
docker-compose up -d --build
```
3. Access the container:
```bash
docker exec -it php-dev-container bash
```
4. Install PHP dependencies:
```bash
compose install
```

### Usage

#### Generate a Random Sequence
Run the application to generate a random sequence:
```bash
docker exec -it php-dev-container php public/index.php
```
You can customize the sequence range or length by modifying the SequenceGenerator class.

### Testing
The project includes automated tests written with PHPUnit.

#### Run Tests
Run all tests:
```bash
composer test
```

### Development
#### Code Style and Formatting
This project follows PSR-12 standards and uses tools to enforce coding styles:
* **PHP_CodeSniffer**: Check code for PSR-12 compliance:
```bash
composer lint
```
* **PHP_CodeSniffer Fixer**: Automatically fix linting issues:
```bash
composer lint:fix
```
* **PHP-CS-Fixer**: Automatically format code:
```bash
composer format
```

### Contributing
Contributions are welcome! If you'd like to contribute:

1. Fork the repository.
2. Create a new branch for your changes.
3. Submit a pull request with a description of your changes.

### Licence
This project is licensed under the MIT License. See the [LICENSE](./LICENSE) file for details.

### Contact
For questions or suggestions, feel free to reach out via the repository issues page. Happy coding! 🚀
32 changes: 32 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"description": "Welcome to the Random Sequence 10K Challenge repository! This project is a PHP implementation for generating a sequence of 10,000 random numbers while ensuring uniqueness. It was designed as part of a coding challenge to demonstrate efficient random number generation in PHP.",
"minimum-stability": "stable",
"license": "proprietary",
"authors": [
{
"name": "Chad Payne",
"email": "[email protected]"
}
],
"scripts": {
"test": "./vendor/bin/phpunit --colors=always --testdox",
"lint": "phpcs",
"lint:fix": "phpcbf",
"format": "php-cs-fixer fix"
},
"autoload": {
"psr-4": {
"illmatix\\RandomSequence\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/"
}
},
"require-dev": {
"phpunit/phpunit": "^11.4",
"squizlabs/php_codesniffer": "^3.11",
"friendsofphp/php-cs-fixer": "^3.65"
}
}
13 changes: 13 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
services:
php:
build:
context: .
dockerfile: Dockerfile
ports:
- "8080:80"
volumes:
- .:/var/www/html
environment:
APACHE_RUN_USER: www-data
APACHE_RUN_GROUP: www-data
container_name: php-dev-container
28 changes: 28 additions & 0 deletions phpcs.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0"?>
<ruleset name="Project Coding Standards">
<!-- Include the PSR-12 standard -->
<rule ref="PSR12"/>

<!-- Set paths to include -->
<file>src/</file>
<file>tests/</file>

<!-- Exclude specific rules -->
<rule ref="Generic.WhiteSpace.ScopeIndent">
<properties>
<property name="tabWidth" value="4"/>
</properties>
</rule>

<!-- Ignore files or directories -->
<exclude-pattern>vendor/*</exclude-pattern>
<exclude-pattern>public/assets/*</exclude-pattern>

<!-- Add custom rules or modify severity -->
<rule ref="Generic.Files.LineLength">
<properties>
<property name="lineLimit" value="120"/>
<property name="absoluteLineLimit" value="150"/>
</properties>
</rule>
</ruleset>
23 changes: 23 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/11.0/phpunit.xsd"
bootstrap="./vendor/autoload.php"
colors="true"
verbose="true">
<!-- Define test suites -->
<testsuites>
<testsuite name="Default Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>

<!-- PHP settings -->
<php>
<env name="APP_ENV" value="testing"/>
<env name="APP_DEBUG" value="true"/>
</php>

<!-- Logging (optional) -->
<logging>
<teamcity outputFile="build/logs/teamcity.log"/>
</logging>
</phpunit>
15 changes: 15 additions & 0 deletions public/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
declare(strict_types=1);

require '../vendor/autoload.php';

use illmatix\RandomSequence\SequenceGenerator;

// Create a new generator for numbers 1 to 10,000
$generator = new SequenceGenerator(1, 10000);

// Get the randomized sequence
$sequence = $generator->getSequence();

// Print the sequence
$generator->printSequence();
Loading

0 comments on commit 1dd15a3

Please sign in to comment.