Skip to content

Can I patch DateTime? #146

Description

@aruku

I'm trying to mock DateTime to capture some output, but I get this error:

Fatal error: Uncaught ReflectionException: Function \DateTime::__construct() does not exist in /workspace/vendor/antecedent/patchwork/src/CallRerouting.php:430
Stack trace:
#0 /workspace/vendor/antecedent/patchwork/src/CallRerouting.php(430): ReflectionFunction->__construct('\\DateTime::__co...')
#1 /workspace/vendor/antecedent/patchwork/Patchwork.php(138): Patchwork\CallRerouting\createStubsForInternals()
#2 /workspace/vendor/composer/autoload_real.php(41): require('/workspace/vend...')
#3 /workspace/vendor/composer/autoload_real.php(45): {closure}('2f3136d1da93fd8...', '/workspace/vend...')
#4 /workspace/vendor/autoload.php(25): ComposerAutoloaderInitd8810229a0ec6a5cd1df17dc9a81c6a4::getLoader()
#5 /workspace/vendor/phpunit/phpunit/phpunit(105): require('/workspace/vend...')
#6 {main}
  thrown in /workspace/vendor/antecedent/patchwork/src/CallRerouting.php on line 430

I do it like this:
redefine('DateTime::__construct', always(new \DateTime('18:10 30-11-2023')));

And this my patchwork.json:

{
    "redefinable-internals": [
        "\\DateTime::__construct"
    ]
}

Should this work or am I trying something impossible?

Also, I installed the library via Composer but I still had to modify composer.json so it is autoloaded:

    "autoload-dev": {
        "files": [
            "vendor/antecedent/patchwork/Patchwork.php"
        ]
}

I'm using version 2.1.26. Thank you in advance!

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions