Repository: laravel/framework (php)

Issue:
Test using `$this->mock(Request::class)` always returning empty GET request
- Laravel Version: 8.48.1
- PHP Version: 7.4.13

### Description:

In a Unit test for a Controller I mocked the request using the following;

```php
$request = $this->mock(Request::class, static function (MockInterface $mock) use ($valid): void {
    $mock->shouldReceive('validated')->once()->andReturn($valid);
});
```

Which gave a valid result until updating Laravel from 8.47 to 8.48.

It seems that the introduction of the `singletonInstance` in the commit [bind mock instances as singletons so they are not overwritten ](https://github.com/laravel/framework/commit/8debb3b34fac26085496dc5a32aad1950a8b3510) and the use of that in the `mock()`, `partialMock()` and `spy()` methods of the `InteractsWithContainer` trait. This seems to mean that an empty `GET` request is always used.

I have been able to resolve my failing tests by going back to the [longer version of the code to mock an object](https://laravel.com/docs/8.x/mocking#mocking-objects), as found in the documentation.

```php
$request = $this->instance(
    Request::class,
    Mockery::mock(Request::class, static function (MockInterface $mock) use ($valid): void {
        $mock->shouldReceive('validated')->once()->andReturn($valid);
    })
);
```

### Steps To Reproduce:

```php
$request = $this->mock(Request::class, static function (MockInterface $mock) use ($valid): void {
    $mock->shouldReceive('validated')->once()->andReturn($valid);
});
```

returns different `$request` compared to 

```php
$request = $this->instance(
    Request::class,
    Mockery::mock(Request::class, static function (MockInterface $mock) use ($valid): void {
        $mock->shouldReceive('validated')->once()->andReturn($valid);
    })
);
```

Pull request:
[8.x] Ensure alias is rebound when mocking items in the container
Closes #37789.

Patch:
diff --git a/.github/ISSUE_TEMPLATE/1_Bug_report.md b/.github/ISSUE_TEMPLATE/1_Bug_report.md
index 2c7803339b..276d2235fc 100644
--- a/.github/ISSUE_TEMPLATE/1_Bug_report.md
+++ b/.github/ISSUE_TEMPLATE/1_Bug_report.md
@@ -6,8 +6,8 @@
 <!-- DO NOT THROW THIS AWAY -->
 <!-- Fill out the FULL versions with patch versions -->
 
-- Laravel Version: #.#.# <!-- php artisan --version -->
-- PHP Version: #.#.# <!-- php --version -->
+- Laravel Version: #.#.#
+- PHP Version: #.#.#
 - Database Driver & Version:
 
 ### Description:
diff --git a/src/Illuminate/Database/Eloquent/Factories/Sequence.php b/src/Illuminate/Database/Eloquent/Factories/Sequence.php
index 5efa73c105..169cc1d196 100644
--- a/src/Illuminate/Database/Eloquent/Factories/Sequence.php
+++ b/src/Illuminate/Database/Eloquent/Factories/Sequence.php
@@ -16,14 +16,14 @@ class Sequence
      *
      * @var int
      */
-    protected $count;
+    public $count;
 
     /**
-     * The current index of the sequence.
+     * The current index of the sequence iteration.
      *
      * @var int
      */
-    protected $index = 0;
+    public $index = 0;
 
     /**
      * Create a new sequence instance.
@@ -44,11 +44,7 @@ public function __construct(...$sequence)
      */
     public function __invoke()
     {
-        if ($this->index >= $this->count) {
-            $this->index = 0;
-        }
-
-        return tap(value($this->sequence[$this->index]), function () {
+        return tap(value($this->sequence[$this->index % $this->count], $this), function () {
             $this->index = $this->index + 1;
         });
     }


Test patch:
diff --git a/src/Illuminate/Foundation/Testing/Concerns/InteractsWithContainer.php b/src/Illuminate/Foundation/Testing/Concerns/InteractsWithContainer.php
index b86ad5ac36..f17208ae6d 100644
--- a/src/Illuminate/Foundation/Testing/Concerns/InteractsWithContainer.php
+++ b/src/Illuminate/Foundation/Testing/Concerns/InteractsWithContainer.php
@@ -86,6 +86,8 @@ protected function spy($abstract, Closure $mock = null)
      */
     protected function singletonInstance($abstract, $instance)
     {
+        $abstract = $this->app->getAlias($abstract);
+
         $this->app->singleton($abstract, function () use ($instance) {
             return $instance;
         });
diff --git a/tests/Database/DatabaseEloquentFactoryTest.php b/tests/Database/DatabaseEloquentFactoryTest.php
index 2d1def2395..80e7518488 100644
--- a/tests/Database/DatabaseEloquentFactoryTest.php
+++ b/tests/Database/DatabaseEloquentFactoryTest.php
@@ -404,6 +404,13 @@ public function test_sequences()
         $this->assertCount(2, $user->roles->filter(function ($role) {
             return $role->pivot->admin === 'N';
         }));
+
+        $users = FactoryTestUserFactory::times(2)->sequence(function ($sequence) {
+            return ['name' => 'index: '.$sequence->index];
+        })->create();
+
+        $this->assertSame('index: 0', $users[0]->name);
+        $this->assertSame('index: 1', $users[1]->name);
     }
 
     public function test_resolve_nested_model_factories()
diff --git a/tests/Foundation/Testing/Concerns/InteractsWithContainerTest.php b/tests/Foundation/Testing/Concerns/InteractsWithContainerTest.php
index d8f9e6d254..5bb975ea97 100644
--- a/tests/Foundation/Testing/Concerns/InteractsWithContainerTest.php
+++ b/tests/Foundation/Testing/Concerns/InteractsWithContainerTest.php
@@ -3,6 +3,8 @@
 namespace Illuminate\Tests\Foundation\Testing\Concerns;
 
 use Illuminate\Foundation\Mix;
+use Illuminate\Http\Request;
+use Mockery\MockInterface;
 use Orchestra\Testbench\TestCase;
 use stdClass;
 
@@ -35,4 +37,16 @@ public function testSingletonBoundInstancesCanBeResolved()
         $this->assertEquals('bar', $this->app->make('foo'));
         $this->assertEquals('bar', $this->app->make('foo', ['with' => 'params']));
     }
+
+    public function testSingletonRebindsAlias()
+    {
+        $this->app->instance('request', Request::create('/example'));
+
+        $this->mock(Request::class, function (MockInterface $mock) {
+            //
+        });
+
+        $this->assertInstanceOf(MockInterface::class, resolve(Request::class));
+        $this->assertInstanceOf(MockInterface::class, resolve('request'));
+    }
 }