Skip to content

Commit

Permalink
bugfix/default (#30)
Browse files Browse the repository at this point in the history
* Add coverage to project.

* Update `Describe.php`.

* Fix `describe` not working.

* Fix `describe` not working.

* Add coverage to `test.yml`.

* Update `phpunit.xml`.

* revert.

* Update `phpunit.xml`.

---------

Co-authored-by: david_smith <david_smith@sweetwater.com>
  • Loading branch information
zero-to-prod and david_smith authored Oct 27, 2024
1 parent 759b38b commit 6dc210d
Show file tree
Hide file tree
Showing 15 changed files with 150 additions and 30 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ vendor
.idea
composer.lock
.phpunit.result.cache
.php-cs-fixer.cache
.php-cs-fixer.cache
/.phpunit.cache/
8 changes: 4 additions & 4 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ services:
target: debug
volumes:
- ./:/app
- ./docker/php81:/usr/local/etc/php

php81composer:
build:
context: docker/php81
target: composer
volumes:
- ./:/app
- ./docker/php81:/usr/local/etc/php

php82:
build:
Expand All @@ -36,14 +36,14 @@ services:
target: debug
volumes:
- ./:/app
- ./docker/php82:/usr/local/etc/php

php82composer:
build:
context: docker/php82
target: composer
volumes:
- ./:/app
- ./docker/php82:/usr/local/etc/php

php83:
build:
Expand All @@ -59,11 +59,11 @@ services:
target: debug
volumes:
- ./:/app
- ./docker/php82:/usr/local/etc/php

php83composer:
build:
context: docker/php83
target: composer
volumes:
- ./:/app
- ./docker/php83:/usr/local/etc/php
- ./:/app
8 changes: 6 additions & 2 deletions docker/php81/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
FROM php:8.1-cli AS base

WORKDIR /app

RUN apt-get update && apt-get install -y \
git \
unzip \
&& rm -rf /var/lib/apt/lists/*

FROM base AS composer

RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

FROM base AS debug

RUN pecl channel-update pecl.php.net && \
pecl install xdebug && \
docker-php-ext-enable xdebug && \
echo "xdebug.mode=coverage" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
docker-php-ext-enable xdebug

WORKDIR /app

CMD ["bash"]
2 changes: 2 additions & 0 deletions docker/php81/php.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
zend_extension=xdebug.so
xdebug.mode=coverage
5 changes: 5 additions & 0 deletions docker/php82/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
FROM php:8.2-cli AS base

WORKDIR /app

RUN apt-get update && apt-get install -y \
git \
unzip \
&& rm -rf /var/lib/apt/lists/*

FROM base AS composer

RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

FROM base AS debug

RUN pecl channel-update pecl.php.net && \
pecl install xdebug && \
docker-php-ext-enable xdebug && \
echo "xdebug.mode=coverage" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini

WORKDIR /app

CMD ["bash"]
2 changes: 2 additions & 0 deletions docker/php82/php.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
zend_extension=xdebug.so
xdebug.mode=coverage
5 changes: 5 additions & 0 deletions docker/php83/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
FROM php:8.3-cli AS base

WORKDIR /app

RUN apt-get update && apt-get install -y \
git \
unzip \
&& rm -rf /var/lib/apt/lists/*

FROM base AS composer

RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

FROM base AS debug

RUN pecl channel-update pecl.php.net && \
pecl install xdebug && \
docker-php-ext-enable xdebug && \
echo "xdebug.mode=coverage" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini

WORKDIR /app

CMD ["bash"]
2 changes: 2 additions & 0 deletions docker/php83/php.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
zend_extension=xdebug.so
xdebug.mode=coverage
36 changes: 23 additions & 13 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,21 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd"
displayDetailsOnTestsThatTriggerDeprecations="true"
displayDetailsOnTestsThatTriggerErrors="true"
displayDetailsOnTestsThatTriggerNotices="true"
displayDetailsOnTestsThatTriggerWarnings="true"
colors="true"
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd"
bootstrap="vendor/autoload.php"
cacheDirectory=".phpunit.cache"
executionOrder="depends,defects"
displayDetailsOnPhpunitDeprecations="true"
failOnPhpunitDeprecation="true"
failOnRisky="true"
failOnWarning="true"
displayDetailsOnTestsThatTriggerDeprecations="true"
displayDetailsOnTestsThatTriggerErrors="true"
displayDetailsOnTestsThatTriggerNotices="true"
displayDetailsOnTestsThatTriggerWarnings="true"
colors="true"
>
<testsuites>
<testsuite name="Unit">
<directory>./tests/Unit</directory>
<testsuite name="default">
<directory>tests</directory>
</testsuite>
</testsuites>
<source>

<source restrictDeprecations="true"
restrictNotices="true"
restrictWarnings="true"
>
<include>
<directory>./app</directory>
<directory>src</directory>
</include>
</source>
</phpunit>
</phpunit>
22 changes: 15 additions & 7 deletions src/DataModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ public static function from(iterable|object|null $context = []): self
/** @var Describe $Describe */
$Describe = $Attribute?->newInstance();

if(isset($Describe->ignore) && $Describe->ignore){
if (isset($Describe->ignore) && $Describe->ignore) {
continue;
}

Expand All @@ -220,6 +220,16 @@ public static function from(iterable|object|null $context = []): self

$property_name = $ReflectionProperty->getName();

if (isset($Describe->default) && !isset($context[$context_key])) {
$self->{$property_name} = $Describe->default;

if (isset($Describe->post)) {
($Describe->post)($self->{$property_name}, $context, $Attribute, $ReflectionProperty);
}

continue;
}

/** Property-level Cast */
if (isset($Describe->cast) && $context) {
$self->{$property_name} = ($Describe->cast)($context[$context_key] ?? [], $context, $Attribute, $ReflectionProperty);
Expand All @@ -245,21 +255,19 @@ public static function from(iterable|object|null $context = []): self
$self->{$methods[$property_name]}($context[$context_key] ?? null, $context, $Attribute, $ReflectionProperty);
continue;
}

/** When a property name does not match a key name */
if (!array_key_exists($context_key, $context)) {
if (isset($Describe->default)) {
$self->{$property_name} = $Describe->default;
continue;
}
if (!isset($context[$context_key])) {
if (isset($Describe->required) && $Describe->required) {
$lineNumber = static function (string $filename, string $property_name): ?int {
foreach (file($filename) as $line_number => $content) {
if (preg_match("/\\$$property_name/", $content)) {
return $line_number + 1;
}
}

// @codeCoverageIgnoreStart
return null;
// @codeCoverageIgnoreEnd
};
throw new PropertyRequiredException(
sprintf(
Expand Down
6 changes: 3 additions & 3 deletions src/Describe.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@ class Describe
public string $from;
public string|array $cast;
public bool $required;
public mixed $default;
public mixed $pre;
public mixed $post;
public $default;
public $pre;
public $post;
public bool $missing_as_null;
public bool $ignore;

Expand Down
26 changes: 26 additions & 0 deletions tests/Unit/Describe/DefaultPost/DefaultTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Tests\Unit\Describe\DefaultPost;

use PHPUnit\Framework\Attributes\Test;
use RuntimeException;
use Tests\TestCase;

class DefaultTest extends TestCase
{
#[Test] public function from(): void
{
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('James');
User::from();
}

#[Test] public function does_not_override(): void
{
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('John');
User::from([
'name' => 'John'
]);
}
}
25 changes: 25 additions & 0 deletions tests/Unit/Describe/DefaultPost/User.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Tests\Unit\Describe\DefaultPost;

use RuntimeException;
use Tests\Unit\Examples\ExtendsTrait\DataModel;
use Zerotoprod\DataModel\Describe;

class User
{
use DataModel;

public string $age;

#[Describe([
'default' => 'James',
'post' => [self::class, 'post']
])]
public string $name;

public static function post($value): string
{
throw new RuntimeException($value);
}
}
16 changes: 16 additions & 0 deletions tests/Unit/Examples/Default/DefaultTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Tests\Unit\Examples\Default;

use PHPUnit\Framework\Attributes\Test;
use Tests\TestCase;

class DefaultTest extends TestCase
{
#[Test] public function from(): void
{
$user = User::from();

$this->assertEquals('James', $user->name);
}
}
14 changes: 14 additions & 0 deletions tests/Unit/Examples/Default/User.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Tests\Unit\Examples\Default;

use Tests\Unit\Examples\ExtendsTrait\DataModel;
use Zerotoprod\DataModel\Describe;

class User
{
use DataModel;

#[Describe(['default' => 'James'])]
public string $name;
}

0 comments on commit 6dc210d

Please sign in to comment.