Skip to content
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

Implement initial version #2

Merged
merged 35 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
f7a986f
Can create an empty class
acoulton Nov 16, 2024
b104e4f
Support defining a custom factory for a whole class using attributes
acoulton Nov 16, 2024
622fab9
Support overriding properties when building instance
acoulton Nov 16, 2024
99a2c3b
Initialise nullable props to null unless there's a class default
acoulton Nov 16, 2024
2bb2721
Allow stubbing incremental ints for id fields
acoulton Nov 16, 2024
dc88abc
Allow stubbing to a fixed default value
acoulton Nov 16, 2024
04ee3bb
Refactor how we create factories to be more configurable
acoulton Nov 16, 2024
11db4cb
Refactor creating default value providers
acoulton Nov 16, 2024
6a87e26
Refactor guessing defaults when none are tagged
acoulton Nov 16, 2024
af2e0e0
Move the guesser config to a standalone class
acoulton Nov 16, 2024
937fa8d
Refactor guesser interface & shared test
acoulton Nov 16, 2024
d1c8916
Fix test namespace and autoloading
acoulton Nov 16, 2024
f3b3fdf
Add ability to guess a default value for date/time fields
acoulton Nov 16, 2024
c1a581e
Guess random values for strings unless told otherwise
acoulton Nov 16, 2024
d6e8961
Guess that any referenced classes we can stub default to empty
acoulton Nov 16, 2024
f90f350
Group the DefaultValue attributes
acoulton Nov 16, 2024
4169e18
Rename the standard config
acoulton Nov 16, 2024
5b3e836
Start implementing ability to cast values to the expected type
acoulton Nov 16, 2024
9617eda
Support auto-detecting how to cast a value
acoulton Nov 17, 2024
17873e8
Move all `caster` code to `StubAs` naming
acoulton Nov 17, 2024
8ebe90f
Move all `default value provider` code to `StubDefault` naming
acoulton Nov 17, 2024
a30b67e
Rename main object / actual factories for clarity
acoulton Nov 17, 2024
366f6fe
Implement recursive stubbing of single objects
acoulton Nov 17, 2024
f387363
Pull configurable dependencies up to top level
acoulton Nov 17, 2024
151e7c7
Cache the object structure for building stubs
acoulton Nov 17, 2024
68f95f3
Allow stubs to specify a custom function to merge defaults
acoulton Nov 17, 2024
b714093
Automatically hydrate collections of stubs from arrays
acoulton Nov 18, 2024
5612209
Support stubbing a class as a mock child
acoulton Nov 18, 2024
a337ef9
Add github actions tests flow
acoulton Nov 18, 2024
916cff7
[WIP] cope with untyped properties e.g. in legacy code
acoulton Nov 18, 2024
11b0998
[WIP] Support stubbing value as json_encoded string
acoulton Nov 18, 2024
fcf6272
Explicitly tagged defaults should take precedence over class defaults
acoulton Nov 19, 2024
4ba4ae3
Use an interface rather than a callable for custom merging
acoulton Nov 22, 2024
1fa9b57
Don't allow stubbing DateTime from an empty string
acoulton Nov 22, 2024
f650322
Support stubbing collections as native `array`
acoulton Nov 26, 2024
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
63 changes: 63 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
name: Run tests
on:
push:
branches:
# Only mainline branches, features etc are covered on the pull_request trigger
- '*.x'
pull_request:
schedule:
# Build every Monday morning in case of external issues
- cron: '0 8 * * 1'

jobs:
run-tests:
runs-on: ubuntu-latest
name: Run tests
strategy:
fail-fast: false
matrix:
php_version:
- '8.3'
- '8.4'
dependencies:
- 'default'
include:
- php_version: '8.3'
dependencies: 'lowest'

steps:
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php_version }}
tools: composer:v2

- name: Checkout
uses: actions/checkout@v4

- name: Get Composer Cache Directory
id: composer-cache
run: |
echo "::set-output name=dir::$(composer config cache-files-dir)"

- uses: actions/cache@v4
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ matrix.dependencies }}-${{ hashFiles('**/composer.lock') }}
restore-keys: |
${{ runner.os }}-composer-${{ matrix.dependencies }}

- name: Install composer dependencies
env:
DEPENDENCIES: ${{ matrix.dependencies }}
run: |
if [ $DEPENDENCIES == 'lowest' ]
then
composer update --prefer-lowest --no-interaction --no-progress
else
composer install --no-interaction --no-progress
fi

- name: Run unit tests
run: |
vendor/bin/phpunit
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
},
"autoload-dev": {
"psr-4": {
"test\\unit\\Ingenerator\\StubObjects\\": "test"
"test\\": "test/"
}
},
"config": {
Expand Down
3 changes: 3 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,8 @@
<testsuite name="unit">
<directory>test/unit</directory>
</testsuite>
<testsuite name="integration">
<directory>test/integration</directory>
</testsuite>
</testsuites>
</phpunit>
11 changes: 11 additions & 0 deletions src/Attribute/StubAs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php
declare(strict_types=1);

namespace Ingenerator\StubObjects\Attribute;

use Ingenerator\StubObjects\StubbingContext;

interface StubAs
{
public function cast(string $property, mixed $value, StubbingContext $context): mixed;
}
44 changes: 44 additions & 0 deletions src/Attribute/StubAs/StubAsCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Ingenerator\StubObjects\Attribute\StubAs;

use Attribute;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Ingenerator\StubObjects\Attribute\StubAs;
use Ingenerator\StubObjects\StubbingContext;

#[Attribute(Attribute::TARGET_CLASS)]
class StubAsCollection implements StubAs
{
public function __construct(
private readonly string $collection_class,
private readonly string $item_class,
) {

}

public function cast(string $property, mixed $value, StubbingContext $context): mixed
{
// @todo also need an integration test covering this
if ($value === NULL) {
return $value;
}

if (($this->collection_class !== 'array') && $value instanceof $this->collection_class) {
// Assume they have built the collection object with the expected items
return $value;
}

$items = array_map(
fn($v) => $v instanceof $this->item_class ? $v : $context->stub_objects->stub($this->item_class, $v),
$value
);

return match ($this->collection_class) {
'array' => $items,
Collection::class => new ArrayCollection($items)
};
}

}
30 changes: 30 additions & 0 deletions src/Attribute/StubAs/StubAsDateTime.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
declare(strict_types=1);

namespace Ingenerator\StubObjects\Attribute\StubAs;

use Attribute;
use DateTimeImmutable;
use Ingenerator\StubObjects\Attribute\StubAs;
use Ingenerator\StubObjects\StubbingContext;
use InvalidArgumentException;

#[Attribute(Attribute::TARGET_PROPERTY)]
class StubAsDateTime implements StubAs
{
public function cast(string $property, mixed $value, StubbingContext $context): mixed
{
// @todo sanity checks for the potential types of input
// @todo support our DateParam syntax
if (($value === NULL) || ($value instanceof DateTimeImmutable)) {
return $value;
}

if ($value === '') {
throw new InvalidArgumentException('Cannot stub a DateTime value from an empty string');
}

return new DateTimeImmutable($value);
}

}
22 changes: 22 additions & 0 deletions src/Attribute/StubAs/StubAsJsonEncoded.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Ingenerator\StubObjects\Attribute\StubAs;

use Attribute;
use Ingenerator\StubObjects\Attribute\StubAs;
use Ingenerator\StubObjects\StubbingContext;

#[Attribute(Attribute::TARGET_PROPERTY)]
class StubAsJsonEncoded implements StubAs
{
public function cast(string $property, mixed $value, StubbingContext $context): mixed
{
// @todo test this, also do we definitely want it in core?? And if we do would we ever guess it?
if (($value === null) || is_string($value)) {
return $value;
}

return json_encode($value, JSON_THROW_ON_ERROR);
}

}
27 changes: 27 additions & 0 deletions src/Attribute/StubAs/StubAsStubObject.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Ingenerator\StubObjects\Attribute\StubAs;

use Attribute;
use Ingenerator\StubObjects\Attribute\StubAs;
use Ingenerator\StubObjects\StubbingContext;

#[Attribute(Attribute::TARGET_PROPERTY)]
class StubAsStubObject implements StubAs
{
public function __construct(private readonly string $as_class)
{

}

public function cast(string $property, mixed $value, StubbingContext $context): mixed
{
// @todo: direct tests for the stubAs methods
if (($value instanceof $this->as_class) || ($value === NULL)) {
return $value;
}

return $context->stub_objects->stub($this->as_class, $value);
}

}
9 changes: 9 additions & 0 deletions src/Attribute/StubDefault.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php
declare(strict_types=1);

namespace Ingenerator\StubObjects\Attribute;

interface StubDefault
{
public function getValue(array $specified_values): mixed;
}
14 changes: 14 additions & 0 deletions src/Attribute/StubDefault/StubDefaultNull.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Ingenerator\StubObjects\Attribute\StubDefault;

use Ingenerator\StubObjects\Attribute\StubDefault;

class StubDefaultNull implements StubDefault
{
public function getValue(array $specified_values): mixed
{
return NULL;
}

}
26 changes: 26 additions & 0 deletions src/Attribute/StubDefault/StubDefaultRandomString.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Ingenerator\StubObjects\Attribute\StubDefault;

use Attribute;
use Ingenerator\StubObjects\Attribute\StubDefault;
use Random\Randomizer;

#[Attribute(Attribute::TARGET_PROPERTY)]
readonly class StubDefaultRandomString implements StubDefault
{
public const ALPHANUMERIC = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ12345678';

public function __construct(
private int $length = 16,
private string $chars = self::ALPHANUMERIC,
private Randomizer $randomizer = new Randomizer()
) {

}

public function getValue(array $specified_values): string
{
return $this->randomizer->getBytesFromString($this->chars, $this->length);
}
}
19 changes: 19 additions & 0 deletions src/Attribute/StubDefault/StubDefaultSequentialId.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
declare(strict_types=1);

namespace Ingenerator\StubObjects\Attribute\StubDefault;

use Attribute;
use Ingenerator\StubObjects\Attribute\StubDefault;

#[Attribute(Attribute::TARGET_PROPERTY)]
class StubDefaultSequentialId implements StubDefault
{
private static int $next_id = 1;

public function getValue(array $specified_values): int
{
return ++self::$next_id;
}

}
22 changes: 22 additions & 0 deletions src/Attribute/StubDefault/StubDefaultValue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
declare(strict_types=1);

namespace Ingenerator\StubObjects\Attribute\StubDefault;

use Attribute;
use Ingenerator\StubObjects\Attribute\StubDefault;

#[Attribute(Attribute::TARGET_PROPERTY)]
readonly class StubDefaultValue implements StubDefault
{
public function __construct(private mixed $value)
{

}

public function getValue(array $specified_values): mixed
{
return $this->value;
}

}
26 changes: 26 additions & 0 deletions src/Attribute/StubFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
declare(strict_types=1);

namespace Ingenerator\StubObjects\Attribute;

use Attribute;
use Ingenerator\StubObjects\Factory\CallableStubFactory;

#[Attribute(Attribute::TARGET_CLASS)]
readonly class StubFactory
{
public CallableStubFactory $factory;

public function __construct(
callable $factory,
) {
// We can't use the first-class callable syntax in an attribute definition, so we have to pass
// legacy callables (e.g. [SomeClass::class, 'methodToCall']) and convert them here
$this->factory = new CallableStubFactory($factory(...));
}

public function getFactory(): CallableStubFactory
{
return $this->factory;
}
}
16 changes: 16 additions & 0 deletions src/Attribute/StubMergeDefaultsWith.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Ingenerator\StubObjects\Attribute;

use Attribute;
use Ingenerator\StubObjects\StubFinalValuesMerger;

#[Attribute(Attribute::TARGET_CLASS)]
class StubMergeDefaultsWith
{

public function __construct(public readonly StubFinalValuesMerger $merger)
{
}

}
17 changes: 17 additions & 0 deletions src/Attribute/StubMockClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
declare(strict_types=1);

namespace Ingenerator\StubObjects\Attribute;

use Attribute;

#[Attribute(Attribute::TARGET_CLASS)]
readonly class StubMockClass
{

public function __construct(public string $class)
{

}

}
Loading
Loading