2
2
3
3
namespace Saritasa \LaravelRepositories \Tests ;
4
4
5
+ use Illuminate \Contracts \Container \BindingResolutionException ;
6
+ use Illuminate \Contracts \Container \Container ;
5
7
use Illuminate \Database \Eloquent \Model ;
8
+ use Mockery ;
9
+ use Mockery \MockInterface ;
6
10
use PHPUnit \Framework \TestCase ;
11
+ use Saritasa \LaravelRepositories \Contracts \IRepository ;
7
12
use Saritasa \LaravelRepositories \Exceptions \RepositoryException ;
8
13
use Saritasa \LaravelRepositories \Exceptions \RepositoryRegisterException ;
9
14
use Saritasa \LaravelRepositories \Repositories \Repository ;
15
20
*/
16
21
class RepositoryFactoryTest extends TestCase
17
22
{
23
+ /**
24
+ * Connection instance mock.
25
+ *
26
+ * @var Container|MockInterface
27
+ */
28
+ protected $ container ;
29
+
30
+ /**
31
+ * Setup tests setting.
32
+ *
33
+ * @return void
34
+ */
35
+ public function setUp (): void
36
+ {
37
+ parent ::setUp ();
38
+ $ this ->container = Mockery::mock (Container::class);
39
+ }
40
+
18
41
/**
19
42
* Test register custom repositories for model with different cases.
20
43
*
@@ -29,10 +52,11 @@ class RepositoryFactoryTest extends TestCase
29
52
* @throws RepositoryRegisterException
30
53
* @throws RepositoryException
31
54
* @throws InvalidArgumentException
55
+ * @throws BindingResolutionException
32
56
*/
33
57
public function testRegisterCustomRepositories (string $ model , string $ repository , bool $ expectException ): void
34
58
{
35
- $ repositoryFactory = new RepositoryFactory ();
59
+ $ repositoryFactory = new RepositoryFactory ($ this -> container );
36
60
37
61
if ($ expectException ) {
38
62
$ this ->expectException (RepositoryRegisterException::class);
@@ -41,8 +65,10 @@ public function testRegisterCustomRepositories(string $model, string $repository
41
65
$ repositoryFactory ->register ($ model , $ repository );
42
66
43
67
if (!$ expectException ) {
68
+ $ expectedSRepository = Mockery::mock ($ repository );
69
+ $ this ->container ->shouldReceive ('make ' )->andReturn ($ expectedSRepository );
44
70
$ actualRepositoryInstance = $ repositoryFactory ->getRepository ($ model );
45
- $ this ->assertEquals ( $ repository , get_class ( $ actualRepositoryInstance) );
71
+ $ this ->assertSame ( $ expectedSRepository , $ actualRepositoryInstance );
46
72
}
47
73
}
48
74
@@ -73,13 +99,15 @@ public function registerRepositoriesData(): array
73
99
* @throws RepositoryRegisterException
74
100
* @throws RepositoryException
75
101
* @throws InvalidArgumentException
102
+ * @throws BindingResolutionException
76
103
*/
77
104
public function testThatEachTimeReturnsTheSameInstance (): void
78
105
{
79
- $ repositoryFactory = new RepositoryFactory ();
106
+ $ repositoryFactory = new RepositoryFactory ($ this -> container );
80
107
$ modelObject = new class extends Model {
81
108
};
82
109
$ modelClass = get_class ($ modelObject );
110
+ $ this ->container ->shouldReceive ('make ' )->andReturn (Mockery::mock (IRepository::class));
83
111
$ repositoryFactory ->register ($ modelClass , Repository::class);
84
112
$ firstInstance = $ repositoryFactory ->getRepository ($ modelClass );
85
113
$ secondInstance = $ repositoryFactory ->getRepository ($ modelClass );
0 commit comments