Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
COMPOSE_FILE=compose.yaml:compose.dev.yaml
PHP_VERSION=8.3
47 changes: 47 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
name: CI Pipeline

on:
push:
branches: [ "master" ]
pull_request:
branches: [ "master" ]

permissions:
contents: read

jobs:
build:
strategy:
matrix:
php_version:
- 8.3
- 8.4
runs-on: ubuntu-latest
env:
COMPOSE_FILE: compose.yaml
PHP_VERSION: ${{ matrix.php_version }}

steps:
- uses: actions/checkout@v4

- name: Validate composer.json and composer.lock
run: composer validate --strict

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Build and push Docker image
uses: docker/build-push-action@v5
with:
context: .
target: ci
push: false
load: true
tags: recruiterphp/byte-units-php:php-${{ matrix.php_version }}-latest
build-args: |
PHP_VERSION=${{ matrix.php_version }}
cache-from: type=gha
cache-to: type=gha,mode=max

- name: Run test suite
run: make test
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.*
.*.cache
.idea/
composer.phar
vendor/
composer.lock
43 changes: 43 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
ARG PHP_VERSION=8.4

FROM php:${PHP_VERSION}-cli AS base

# Install system dependencies
RUN apt-get update && apt-get install -y \
git \
unzip \
libssl-dev \
libcurl4-openssl-dev \
pkg-config \
&& rm -rf /var/lib/apt/lists/*

RUN docker-php-ext-install -j$(nproc) \
bcmath

# Copy Composer from official image
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer

# Set working directory
WORKDIR /app

# Set environment variable for Composer
ENV COMPOSER_ALLOW_SUPERUSER=1

CMD ["tail", "-f", "/dev/null"]

FROM base AS dev

# Install XDebug extension
RUN pecl install xdebug \
&& docker-php-ext-enable xdebug

FROM base AS ci

# Copy composer files
COPY composer.json composer.lock* ./

# Install dependencies including dev dependencies for testing
RUN composer install --optimize-autoloader

# Copy application code
COPY . .
47 changes: 47 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
.PHONY: build up down test test-coverage phpstan rector fix-cs install shell logs clean

# Build the Docker image
build:
docker compose build

# Start the services
up:
docker compose up -d

# Stop the services
down:
docker compose down

# Install dependencies
install:
docker compose run --rm php composer install

# Run all tests
test: up
docker compose exec php vendor/bin/phpunit

# Run unit tests with coverage
test-coverage: up
docker compose exec -e XDEBUG_MODE=coverage php vendor/bin/phpunit --coverage-html var/coverage/

phpstan: up
docker compose exec php vendor/bin/phpstan

rector: up
docker compose exec php vendor/bin/rector

fix-cs: up
docker compose exec php vendor/bin/php-cs-fixer fix -v

# Open a shell in the PHP container
shell:
docker compose exec php bash

# View logs
logs:
docker compose logs -f php

# Clean up containers and volumes
clean:
docker compose down -v
docker compose rm -f
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,12 @@ echo ByteUnits\bytes(1322000)->numberOfBytes(); // outputs 1322000

### Compare
There are a few methods that could be used to compare bytes in various units and systems

```php
<?php

ByteUnits\Metric::kilobytes(1)->isLessThan(ByteUnits\Binary::kilobytes(1)); // it's true
ByteUnits\Metric::kilobytes(1)->isEqualTo(ByteUnits\Binary::bytes(1000)); // it's true
ByteUnits\Metric::kilobytes(1)->equals(ByteUnits\Binary::bytes(1000)); // it's true
ByteUnits\Metric::kilobytes(1.3)->isGreaterThan(ByteUnits\Binary::kilobytes(1)); // it's true
```

Expand Down
9 changes: 9 additions & 0 deletions compose.dev.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
services:
php:
build:
context: .
args:
- PHP_VERSION=${PHP_VERSION}
target: dev
volumes:
- .:/app
9 changes: 9 additions & 0 deletions compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
services:
php:
build:
context: .
args:
- PHP_VERSION=${PHP_VERSION}
tags:
- latest
working_dir: /app
51 changes: 36 additions & 15 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,38 @@
{
"name": "gabrielelana/byte-units",
"name": "recruiterphp/byte-units",
"description": "Library to parse, format and convert byte units",
"type": "library",
"version": "0.5.0",
"keywords": ["byte", "units", "format", "parse", "convert", "size"],
"homepage": "https://github.com/gabrielelana/byte-units",
"license": "MIT",
"authors": [{
"name": "Gabriele Lana",
"email": "gabriele.lana@gmail.com"
}],
"type": "library",
"keywords": [
"byte",
"units",
"format",
"parse",
"convert",
"size"
],
"authors": [
{
"name": "Gabriele Lana",
"email": "gabriele.lana@gmail.com"
},
{
"name": "Contributors",
"homepage": "https://github.com/recruiterphp/byte-units/graphs/contributors"
}
],
"homepage": "https://github.com/recruiterphp/byte-units",
"require": {
"php": "^8.3",
"ext-bcmath": "*"
},
"require-dev": {
"ergebnis/composer-normalize": "^2.47",
"phpunit/phpunit": "^12.3"
},
"replace": {
"gabrielelana/byte-units": "self.version"
},
"autoload": {
"psr-4": {
"ByteUnits\\": "src/ByteUnits"
Expand All @@ -18,11 +41,9 @@
"src/ByteUnits/functions.php"
]
},
"require": {
"php": ">=5.4.0",
"ext-bcmath": "*"
},
"require-dev": {
"phpunit/phpunit": ">=4.0,<6.0"
"config": {
"allow-plugins": {
"ergebnis/composer-normalize": true
}
}
}
25 changes: 25 additions & 0 deletions phpunit.dist.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="vendor/autoload.php"
cacheDirectory=".phpunit.cache"
executionOrder="depends,defects"
requireCoverageMetadata="false"
beStrictAboutCoverageMetadata="true"
beStrictAboutOutputDuringTests="true"
displayDetailsOnPhpunitDeprecations="true"
failOnPhpunitDeprecation="true"
failOnRisky="true"
failOnWarning="true">
<testsuites>
<testsuite name="default">
<directory>tests</directory>
</testsuite>
</testsuites>

<source ignoreIndirectDeprecations="true" restrictNotices="true" restrictWarnings="true">
<include>
<directory>src</directory>
</include>
</source>
</phpunit>
7 changes: 0 additions & 7 deletions phpunit.xml

This file was deleted.

8 changes: 4 additions & 4 deletions tests/ByteUnits/ArithmeticTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace ByteUnits;

class ArithmeticTest extends \PHPUnit_Framework_TestCase
use PHPUnit\Framework\TestCase;

class ArithmeticTest extends TestCase
{
public function testAddInSameUnitSystem()
{
Expand All @@ -24,11 +26,9 @@ public function testAutoboxing()
$this->assertEquals(Metric::bytes(3), Metric::bytes(5)->remove('2B'));
}

/**
* @expectedException ByteUnits\NegativeBytesException
*/
public function testCannotRemoveMoreBytesThanYouHave()
{
$this->expectException(NegativeBytesException::class);
Metric::bytes(5)->remove(Metric::bytes(10));
}

Expand Down
8 changes: 4 additions & 4 deletions tests/ByteUnits/BinarySystemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace ByteUnits;

class BinarySystemTest extends \PHPUnit_Framework_TestCase
use PHPUnit\Framework\TestCase;

class BinarySystemTest extends TestCase
{
public function testKilobytesConstructor()
{
Expand Down Expand Up @@ -34,11 +36,9 @@ public function testExabytesConstructor()
$this->assertEquals(Binary::bytes(1152921504606846976), Binary::exabytes(1));
}

/**
* @expectedException ByteUnits\NegativeBytesException
*/
public function testCannotBeNegative()
{
$this->expectException(NegativeBytesException::class);
Binary::bytes(-1);
}
}
8 changes: 4 additions & 4 deletions tests/ByteUnits/BoxingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace ByteUnits;

class BoxingTest extends \PHPUnit_Framework_TestCase
use PHPUnit\Framework\TestCase;

class BoxingTest extends TestCase
{
public function testBoxAnInteger()
{
Expand All @@ -22,11 +24,9 @@ public function testBoxAByteUnit()
$this->assertEquals($byteUnitInBinarySystem, box($byteUnitInBinarySystem));
}

/**
* @expectedException ByteUnits\ConversionException
*/
public function testBoxAnObjectThatIsNotAByteUnit()
{
$this->expectException(ConversionException::class);
box(new \StdClass());
}
}
4 changes: 3 additions & 1 deletion tests/ByteUnits/CompareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace ByteUnits;

class CompareTest extends \PHPUnit_Framework_TestCase
use PHPUnit\Framework\TestCase;

class CompareTest extends TestCase
{
public function testCompareWithSameUnitSystem()
{
Expand Down
4 changes: 3 additions & 1 deletion tests/ByteUnits/ConversionBetweenSystemsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace ByteUnits;

class ConversionBetweenSystemsTest extends \PHPUnit_Framework_TestCase
use PHPUnit\Framework\TestCase;

class ConversionBetweenSystemsTest extends TestCase
{
public function testBytesAreInMetricStystem()
{
Expand Down
4 changes: 3 additions & 1 deletion tests/ByteUnits/FormatInBinarySystemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace ByteUnits;

class FormatInBinarySystemTest extends \PHPUnit_Framework_TestCase
use PHPUnit\Framework\TestCase;

class FormatInBinarySystemTest extends TestCase
{
public function testBytesNamedConstructor()
{
Expand Down
4 changes: 3 additions & 1 deletion tests/ByteUnits/FormatInMetricSystemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace ByteUnits;

class FormatInMetricSystemTest extends \PHPUnit_Framework_TestCase
use PHPUnit\Framework\TestCase;

class FormatInMetricSystemTest extends TestCase
{
public function testBytesNamedConstructor()
{
Expand Down
Loading
Loading