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
39 changes: 39 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: Tests

on:
pull_request:
push:
branches:
- master

jobs:
phpunit:
name: PHP ${{ matrix.php }}
runs-on: ubuntu-latest

strategy:
fail-fast: false
matrix:
php:
- '8.4'
- '8.5'

steps:
- name: Checkout
uses: actions/checkout@v4

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

- name: Validate composer.json
run: composer validate --strict --no-check-lock

- name: Install dependencies
run: composer update --prefer-dist --no-interaction --no-progress

- name: Run tests
run: vendor/bin/phpunit --display-all-issues --fail-on-phpunit-deprecation
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
vendor/
vendor/
.phpunit.cache/
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@
}
],
"require": {
"php": ">=5.3",
"php": "^8.4",
"fakerphp/faker": "^1.19",
"symfony/yaml": "^6.4 || ^7.0"
},
"require-dev": {
"doctrine/common": "^3.0",
"symfony/property-access": "~5.0|~6.0",
"phpunit/phpunit": "^6.0"
"phpunit/phpunit": "^11.5"
},
"autoload": {
"psr-0": { "Nelmio\\Alice\\": "src/" }
Expand Down
29 changes: 13 additions & 16 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,25 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
backupGlobals = "false"
backupStaticAttributes = "false"
colors = "true"
convertErrorsToExceptions = "true"
convertNoticesToExceptions = "true"
convertWarningsToExceptions = "true"
processIsolation = "false"
stopOnFailure = "false"
syntaxCheck = "false"
bootstrap = "tests/bootstrap.php">

<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/11.5/phpunit.xsd"
backupGlobals="false"
bootstrap="tests/bootstrap.php"
cacheDirectory=".phpunit.cache"
colors="true"
failOnDeprecation="true"
failOnNotice="true"
failOnWarning="true">
<testsuites>
<testsuite name="Fixtures Test Suite">
<directory>tests/</directory>
</testsuite>
</testsuites>

<filter>
<whitelist>
<source>
<include>
<directory>src</directory>
</whitelist>
</filter>
</include>
</source>
</phpunit>
19 changes: 14 additions & 5 deletions src/Nelmio/Alice/Loader/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -515,15 +515,13 @@ private function populateObject($instance, $class, $name, $data, $classFlags, $i
$generatedVal = $this->checkTypeHints($instance, 'set'.$key, $generatedVal);
if(!is_callable(array($instance, 'set'.$key))) {
$refl = new \ReflectionMethod($instance, 'set'.$key);
$refl->setAccessible(true);
$refl->invoke($instance, $generatedVal);
} else {
$instance->{'set'.$key}($generatedVal);
}
$variables[$key] = $generatedVal;
} elseif (property_exists($instance, $key)) {
$refl = new \ReflectionProperty($instance, $key);
$refl->setAccessible(true);
$refl->setValue($instance, $generatedVal);

$variables[$key] = $generatedVal;
Expand Down Expand Up @@ -559,12 +557,12 @@ private function checkTypeHints($obj, $method, $value, $pNum = 0)
$reflection = new \ReflectionMethod($obj, $method);
$params = $reflection->getParameters();

if (!$params[$pNum]->getClass()) {
$hintedClass = $this->getClassTypeName($params[$pNum]);

if (!$hintedClass) {
return $value;
}

$hintedClass = $params[$pNum]->getClass()->getName();

if ($hintedClass === 'DateTime') {
try {
if (preg_match('{^[0-9]+$}', $value)) {
Expand All @@ -587,6 +585,17 @@ private function checkTypeHints($obj, $method, $value, $pNum = 0)
return $value;
}

private function getClassTypeName(\ReflectionParameter $parameter)
{
$type = $parameter->getType();

if (!$type instanceof \ReflectionNamedType || $type->isBuiltin()) {
return null;
}

return $type->getName();
}

private function process($data, array $variables)
{
if (is_array($data)) {
Expand Down
26 changes: 11 additions & 15 deletions tests/Nelmio/Alice/FixturesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ public function testLoadLoadsYamlFilesAndDoctrineORM()

public function testThatNewLoaderIsCreatedForDifferingOptions()
{
$om = $this->getMock('Doctrine\Persistence\ObjectManager');
$om = $this->createMock('Doctrine\Persistence\ObjectManager');
$om->expects($this->any())
->method('find')->will($this->returnValue(new User()));
->method('find')->willReturn(new User());

$optionsBatch = array(
// default options
Expand Down Expand Up @@ -184,22 +184,19 @@ public function testThatNewLoaderIsCreatedForDifferingOptions()
}

$prop = new \ReflectionProperty('\Nelmio\Alice\Fixtures', 'loaders');
$prop->setAccessible(true);
$loaders = $prop->getValue();

$this->assertEquals(12, count($loaders));
}

public function testThatExceptionIsThrownForInvalidProvider()
{
$om = $this->getMock('Doctrine\Persistence\ObjectManager');
$om = $this->createMock('Doctrine\Persistence\ObjectManager');
$om->expects($this->any())
->method('find')->will($this->returnValue(new User()));
->method('find')->willReturn(new User());

$this->setExpectedException(
'\InvalidArgumentException',
'The provider should be a string or an object, got array instead'
);
$this->expectException('\InvalidArgumentException');
$this->expectExceptionMessage('The provider should be a string or an object, got array instead');

Fixtures::load(
__DIR__.'/fixtures/complete.yml',
Expand Down Expand Up @@ -271,12 +268,11 @@ public function testLoadLoadsPHPfiles()
$this->assertEquals(42, $user->favoriteNumber);
}

/**
* @expectedException \RuntimeException
*/
public function testLoadWithLogger()
{
$om = $this->getMock('Doctrine\Persistence\ObjectManager');
$om = $this->createMock('Doctrine\Persistence\ObjectManager');

$this->expectException('\RuntimeException');

$objects = Fixtures::load(__DIR__.'/fixtures/basic.php', $om, array(
'logger' => 'not callable'
Expand Down Expand Up @@ -317,7 +313,7 @@ public function testMakesOnlyOneFlushWithPersistOnce()

protected function getDoctrineManagerMock($objects = null)
{
$om = $this->getMock('Doctrine\Persistence\ObjectManager');
$om = $this->createMock('Doctrine\Persistence\ObjectManager');

$om->expects($objects ? $this->exactly($objects) : $this->any())
->method('persist');
Expand All @@ -326,7 +322,7 @@ protected function getDoctrineManagerMock($objects = null)
->method('flush');

$om->expects($this->once())
->method('find')->will($this->returnValue(new User()));
->method('find')->willReturn(new User());

return $om;
}
Expand Down
Loading
Loading