-
-
Notifications
You must be signed in to change notification settings - Fork 329
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
Converting types #1096
Comments
I think it should be:
But maybe you could make it both more reliable and elegant by registering a custom faker provider |
I'm running into the same issue with Doctrine being strict about this. However what surprises me is that the fixture dates are not generated as |
Your suggestion doesn't work btw @theofidry:
I don't think it can be fixed right now. |
I think your best shot then is register your own faker provider, either a new one or override the faker built-in one |
For Symfony, this leads to create this kind of service and adapt every function to support DateTimeImmutable. This should be a little harder since the DateTime provider check the instance of the input object against <?php declare(strict_types=1);
namespace App\Tests\Resources\Services\Faker;
use Faker\Provider\DateTime;
final class DateTimeImmutableProvider extends DateTime
{
public static function dateTimeImmutableBetween($startDate = '-30 years', $endDate = 'now', $timezone = null): \DateTimeImmutable
{
$startTimestamp = $startDate instanceof \DateTimeInterface ? $startDate->getTimestamp() : $startDate;
$dateTime = parent::dateTimeBetween($startTimestamp, $endDate, $timezone);
return \DateTimeImmutable::createFromMutable($dateTime);
}
} |
@NicolasGuilloux this could also be made easier by contributing to Faker in order to provide |
Actually |
Also, the Symfony |
Can be done: be it here or Faker. PRs for it are welcomed |
Having alternative functions for it seems like a weak workaround placing the burden on the fixture developer. Given that there already is a decorated property accessor I think it should be possible to use the proper converter functions transparently? (one could argue that this should even be part of the underlying symfony/propertyaccessor library transparently) |
It's a way to see it, but I also it as a missing piece from those functions. Faker is not meant to be used only in Alice, having functions to generate DateTimeImmutable classes instead of DateTime seems pretty basic.
I really don't think it's the property accessor's job to convert a type to another. The only reason why it might be acceptable here is because you are in a very specific context. |
I don't fundamentally disagree with either of those things, it's a pragmatism vs idealism situation methinks.
But when using Faker directly one can easily use
It happily converts integers to floats, and booleans to integers. It supports every type of type coercion natively supported by PHP. It's a weakness of PHP that it doesn't support cast overloading like many other languages, otherwise it would be able to do this conversion itself on explicit assignment. Whether the PR I made for PropertyAccess ends up behind an option or not (I'm impartial as to whether it should be default behavior), it solves this issue for Alice and many other cases like Symfony Forms which has similar issues when mapping Doctrine entities. |
I'm struggling to find a way to generate a date using
dateTimeBetween
and convert it toDateTimeImmutable
.Example configuration:
Using it leads to doctrine error:
I tried to use the following, bit it also didn't work:
Throws
Call to undefined function dateTimeBetween()
error.How can I convert
DateTime
toDateTimeImmutable
?The text was updated successfully, but these errors were encountered: