Skip to content

Problem with promises when cloning doubles #496

@elvetemedve

Description

@elvetemedve

When a Double object is cloned, the attached promises won't be cloned. Therefore the two objects are stitched together.

The example below demonstrates the problem.

<?php

require_once 'vendor/autoload.php';

use Prophecy\Argument;

// Initialize Prophecy
$prophet = new Prophecy\Prophet;
$prophecy = $prophet->prophesize();

// Setup mock
$prophecy->willExtend('DateTime');
$prophecy->setTimestamp(Argument::any())->will(function($arguments) {
    $this->getTimestamp()->willReturn($arguments[0]);
});

$dummyDateTime = $prophecy->reveal();

// Use the doubles within the test subject

$dummyDateTimeClone = clone $dummyDateTime;

$dummyDateTime->setTimestamp(123);
$dummyDateTimeClone->setTimestamp(456);

echo 'original double: ';
var_dump(get_class($dummyDateTime));

echo 'cloned double: ';
var_dump(get_class($dummyDateTimeClone));

echo 'call getTimestamp() on original = ';
var_dump($dummyDateTime->getTimestamp());

echo 'call getTimestamp() on cloned = ';
var_dump($dummyDateTimeClone->getTimestamp());

Actual Output

original double: /home/gbuza/Downloads/test/test.php:27:
string(18) "Double\DateTime\P1"
cloned double: /home/gbuza/Downloads/test/test.php:30:
string(18) "Double\DateTime\P1"
call getTimestamp() on original = /home/gbuza/Downloads/test/test.php:33:
int(456)
call getTimestamp() on cloned = /home/gbuza/Downloads/test/test.php:36:
int(456)

Expected Output

original double: /home/gbuza/Downloads/test/test.php:27:
string(18) "Double\DateTime\P1"
cloned double: /home/gbuza/Downloads/test/test.php:30:
string(18) "Double\DateTime\P1"
call getTimestamp() on original = /home/gbuza/Downloads/test/test.php:33:
int(123)
call getTimestamp() on cloned = /home/gbuza/Downloads/test/test.php:36:
int(456)

Environment

Prophecy version: 1.11.1
PHP version: 7.4.9

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions