Skip to content
This repository has been archived by the owner on Jul 8, 2023. It is now read-only.

Commit

Permalink
Fix call argument reference preservation issue
Browse files Browse the repository at this point in the history
  • Loading branch information
ezzatron committed Apr 8, 2023
1 parent 5cd405c commit d900758
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 26 deletions.
5 changes: 4 additions & 1 deletion src/Call/Arguments.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ public static function create(...$arguments): self
*/
public function __construct(array $arguments)
{
$this->arguments = $arguments;
$this->count = count($arguments);
$this->positionalCount = 0;
$this->positional = [];
Expand All @@ -65,6 +64,10 @@ public function __construct(array $arguments)
}

$this->firstPositionOrName = $firstPositionOrName;

// do not move this above other assignments
// PHP does extremely weird things with references if you do
$this->arguments = $arguments;
}

/**
Expand Down
6 changes: 3 additions & 3 deletions test/fixture/verification/completed/none-completed/expected
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
%RESET%Expected %BOLD%stub#0[label]%RESET% to complete.
Matched 0 of 2:
%RED%✗%RESET% Call #0 - stub#0[label](#1[~3]):
%RED%✗%RESET% Returned #1[~3], then:
%RED%✗%RESET% Returned #2[~3], then:
%RED%✗%RESET% Never started iterating
%RED%✗%RESET% Call #1 - stub#0[label](#2[~3]):
%RED%✗%RESET% Returned #1[~3], then:
%RED%✗%RESET% Call #1 - stub#0[label](#3[~3]):
%RED%✗%RESET% Returned #2[~3], then:
- Started iterating
- Produced 0 => "aardvark"
- Produced 1 => "bonobo"
Expand Down
41 changes: 19 additions & 22 deletions test/fixture/verification/in-order/normal-events/expected
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,30 @@
- Returned "a" from stub#0[label]("a", "b")
- Threw Exception#1{message: "C"} from stub#0[label]("c", "d")
- Called stub#0[label](#2["e", "f"])
- stub#0[label](#3["e", "f"]) started iterating
- Produced 0 => "e" from stub#0[label](#4["e", "f"])
- stub#0[label](#5["e", "f"]) finished iterating
- Called spy#6[first]()
- stub#0[label](#2["e", "f"]) started iterating
- Produced 0 => "e" from stub#0[label](#2["e", "f"])
- stub#0[label](#2["e", "f"]) finished iterating
- Called spy#3[first]()
Actual order:
- Called spy#6[first]()
- Called spy#3[first]()
- Called stub#0[label]("a", "b")
- Returned "a" from stub#0[label]("a", "b")
- Threw Exception#1{message: "C"} from stub#0[label]("c", "d")
- Called stub#0[label](#4["e", "f"])
- Returned #5["e", "f"] from stub#0[label](#7["e", "f"])
- stub#0[label](#3["e", "f"]) started iterating
- Produced 0 => "e" from stub#0[label](#8["e", "f"])
- Produced 1 => "f" from stub#0[label](#9["e", "f"])
- stub#0[label](#9["e", "f"]) finished iterating
- Called stub#0[label](#2["e", "f"])
- Returned #4["e", "f"] from stub#0[label](#2["e", "f"])
- stub#0[label](#2["e", "f"]) started iterating
- Produced 0 => "e" from stub#0[label](#2["e", "f"])
- Produced 1 => "f" from stub#0[label](#2["e", "f"])
- stub#0[label](#2["e", "f"]) finished iterating
Difference:
- %YELLOW%%FAINT%{+%RESET%%YELLOW%Called spy#6[first]()%FAINT%+}%RESET%
- %YELLOW%%FAINT%{+%RESET%%YELLOW%Called spy#3[first]()%FAINT%+}%RESET%
%GREEN%✓%RESET% Called stub#0[label]("a", "b")
%GREEN%✓%RESET% Returned "a" from stub#0[label]("a", "b")
%GREEN%✓%RESET% Threw Exception#1{message: "C"} from stub#0[label]("c", "d")
%RED%✗%RESET% %CYAN%%FAINT%[-%RESET%%CYAN%Called stub#0[label](#2["e", "f"])%FAINT%-]%RESET%
- %YELLOW%%FAINT%{+%RESET%%YELLOW%Called stub#0[label](#4["e", "f"])%FAINT%+}%RESET%
- %YELLOW%%FAINT%{+%RESET%%YELLOW%Returned #5["e", "f"] from stub#0[label](#7["e", "f"])%FAINT%+}%RESET%
%GREEN%✓%RESET% stub#0[label](#3["e", "f"]) started iterating
%RED%✗%RESET% %CYAN%%FAINT%[-%RESET%%CYAN%Produced 0 => "e" from stub#0[label](#4["e", "f"])%FAINT%-]%RESET%
%RED%✗%RESET% %CYAN%%FAINT%[-%RESET%%CYAN%stub#0[label](#5["e", "f"]) finished iterating%FAINT%-]%RESET%
%RED%✗%RESET% %CYAN%%FAINT%[-%RESET%%CYAN%Called spy#6[first]()%FAINT%-]%RESET%
- %YELLOW%%FAINT%{+%RESET%%YELLOW%Produced 0 => "e" from stub#0[label](#8["e", "f"])%FAINT%+}%RESET%
- %YELLOW%%FAINT%{+%RESET%%YELLOW%Produced 1 => "f" from stub#0[label](#9["e", "f"])%FAINT%+}%RESET%
- %YELLOW%%FAINT%{+%RESET%%YELLOW%stub#0[label](#9["e", "f"]) finished iterating%FAINT%+}%RESET%
%GREEN%✓%RESET% Called stub#0[label](#2["e", "f"])
- %YELLOW%%FAINT%{+%RESET%%YELLOW%Returned #4["e", "f"] from stub#0[label](#2["e", "f"])%FAINT%+}%RESET%
%GREEN%✓%RESET% stub#0[label](#2["e", "f"]) started iterating
%GREEN%✓%RESET% Produced 0 => "e" from stub#0[label](#2["e", "f"])
- %YELLOW%%FAINT%{+%RESET%%YELLOW%Produced 1 => "f" from stub#0[label](#2["e", "f"])%FAINT%+}%RESET%
%GREEN%✓%RESET% stub#0[label](#2["e", "f"]) finished iterating
%RED%✗%RESET% %CYAN%%FAINT%[-%RESET%%CYAN%Called spy#3[first]()%FAINT%-]%RESET%
28 changes: 28 additions & 0 deletions test/suite/FunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1492,6 +1492,34 @@ public function testExporterExamplesIdentifierPersistenceArrays()
$this->assertSame($id1, $id5);
}

public function testCallArgumentReferencePreservation()
{
$spy = spy();
$array = ['a'];
$spy($array);
$argumentsObj = $spy->firstCall()->arguments();
$argumentsA = $argumentsObj->all();
$argumentsB = $argumentsObj->all();
$argumentsA[0][] = 'b';

$this->assertSame(['a', 'b'], $argumentsB[0]);
}

public function testExporterExamplesIdentifierPersistenceCallArguments()
{
$spy = spy();
$spy([]);
$argumentsObj = $spy->firstCall()->arguments();
$argumentsA = $argumentsObj->all();
$argumentsB = $argumentsObj->all();
$actualA = $this->exporter->export($argumentsA, -1);
$actualB = $this->exporter->export($argumentsB, -1);
$innerA = preg_replace('/^#\d+\[(.*)]$/', '$1', $actualA);
$innerB = preg_replace('/^#\d+\[(.*)]$/', '$1', $actualB);

$this->assertSame($innerA, $innerB);
}

public function testExporterExamplesRecursiveValues()
{
$recursiveArray = [];
Expand Down

0 comments on commit d900758

Please sign in to comment.