Skip to content

Commit 6d14f76

Browse files
committed
Split container methods
1 parent 430608a commit 6d14f76

File tree

2 files changed

+159
-150
lines changed

2 files changed

+159
-150
lines changed

src/Codeception/Module/Laravel.php

Lines changed: 2 additions & 150 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace Codeception\Module;
66

7-
use Closure;
87
use Codeception\Configuration;
98
use Codeception\Exception\ModuleConfigException;
109
use Codeception\Lib\Connector\Laravel as LaravelConnector;
@@ -14,6 +13,7 @@
1413
use Codeception\Lib\ModuleContainer;
1514
use Codeception\Module\Laravel\InteractsWithAuthentication;
1615
use Codeception\Module\Laravel\InteractsWithConsole;
16+
use Codeception\Module\Laravel\InteractsWithContainer;
1717
use Codeception\Subscriber\ErrorHandler;
1818
use Codeception\TestInterface;
1919
use Codeception\Util\ReflectionHelper;
@@ -131,6 +131,7 @@ class Laravel extends Framework implements ActiveRecord, PartedModule
131131
{
132132
use InteractsWithAuthentication;
133133
use InteractsWithConsole;
134+
use InteractsWithContainer;
134135

135136
/**
136137
* @var Application
@@ -292,24 +293,6 @@ protected function revertErrorHandler(): void
292293
set_error_handler([$handler, 'errorHandler']);
293294
}
294295

295-
/**
296-
* Provides access the Laravel application object.
297-
*
298-
* @return \Illuminate\Contracts\Foundation\Application
299-
*/
300-
public function getApplication()
301-
{
302-
return $this->app;
303-
}
304-
305-
/**
306-
* @param \Illuminate\Contracts\Foundation\Application $app
307-
*/
308-
public function setApplication($app): void
309-
{
310-
$this->app = $app;
311-
}
312-
313296
/**
314297
* Enable Laravel exception handling.
315298
*
@@ -763,32 +746,6 @@ public function seeFormErrorMessage(string $field, $errorMessage = null): void
763746
}
764747
}
765748

766-
/**
767-
* Return an instance of a class from the Laravel service container.
768-
* (https://laravel.com/docs/master/container)
769-
*
770-
* ``` php
771-
* <?php
772-
* // In Laravel
773-
* App::bind('foo', function($app) {
774-
* return new FooBar;
775-
* });
776-
*
777-
* // Then in test
778-
* $service = $I->grabService('foo');
779-
*
780-
* // Will return an instance of FooBar, also works for singletons.
781-
* ```
782-
*
783-
* @param string $class
784-
* @return mixed
785-
*/
786-
public function grabService(string $class)
787-
{
788-
return $this->app[$class];
789-
}
790-
791-
792749
/**
793750
* Inserts record into the database.
794751
* If you pass the name of a database table as the first argument, this method returns an integer ID.
@@ -1231,109 +1188,4 @@ private function buildQuery(string $table, $attributes = [])
12311188
return $query;
12321189
}
12331190

1234-
/**
1235-
* Add a binding to the Laravel service container.
1236-
* (https://laravel.com/docs/master/container)
1237-
*
1238-
* ``` php
1239-
* <?php
1240-
* $I->haveBinding('My\Interface', 'My\Implementation');
1241-
* ```
1242-
*
1243-
* @param string $abstract
1244-
* @param Closure|string|null $concrete
1245-
* @param bool $shared
1246-
*/
1247-
public function haveBinding(string $abstract, $concrete = null, bool $shared = false): void
1248-
{
1249-
$this->client->haveBinding($abstract, $concrete, $shared);
1250-
}
1251-
1252-
/**
1253-
* Add a singleton binding to the Laravel service container.
1254-
* (https://laravel.com/docs/master/container)
1255-
*
1256-
* ``` php
1257-
* <?php
1258-
* $I->haveSingleton('App\MyInterface', 'App\MySingleton');
1259-
* ```
1260-
*
1261-
* @param string $abstract
1262-
* @param Closure|string|null $concrete
1263-
*/
1264-
public function haveSingleton(string $abstract, $concrete): void
1265-
{
1266-
$this->client->haveBinding($abstract, $concrete, true);
1267-
}
1268-
1269-
/**
1270-
* Add a contextual binding to the Laravel service container.
1271-
* (https://laravel.com/docs/master/container)
1272-
*
1273-
* ``` php
1274-
* <?php
1275-
* $I->haveContextualBinding('My\Class', '$variable', 'value');
1276-
*
1277-
* // This is similar to the following in your Laravel application
1278-
* $app->when('My\Class')
1279-
* ->needs('$variable')
1280-
* ->give('value');
1281-
* ```
1282-
*
1283-
* @param string $concrete
1284-
* @param string $abstract
1285-
* @param Closure|string $implementation
1286-
*/
1287-
public function haveContextualBinding(string $concrete, string $abstract, $implementation): void
1288-
{
1289-
$this->client->haveContextualBinding($concrete, $abstract, $implementation);
1290-
}
1291-
1292-
/**
1293-
* Add an instance binding to the Laravel service container.
1294-
* (https://laravel.com/docs/master/container)
1295-
*
1296-
* ``` php
1297-
* <?php
1298-
* $I->haveInstance('App\MyClass', new App\MyClass());
1299-
* ```
1300-
*
1301-
* @param string $abstract
1302-
* @param mixed $instance
1303-
*/
1304-
public function haveInstance(string $abstract, $instance): void
1305-
{
1306-
$this->client->haveInstance($abstract, $instance);
1307-
}
1308-
1309-
/**
1310-
* Register a handler than can be used to modify the Laravel application object after it is initialized.
1311-
* The Laravel application object will be passed as an argument to the handler.
1312-
*
1313-
* ``` php
1314-
* <?php
1315-
* $I->haveApplicationHandler(function($app) {
1316-
* $app->make('config')->set(['test_value' => '10']);
1317-
* });
1318-
* ```
1319-
*
1320-
* @param callable $handler
1321-
*/
1322-
public function haveApplicationHandler(callable $handler): void
1323-
{
1324-
$this->client->haveApplicationHandler($handler);
1325-
}
1326-
1327-
/**
1328-
* Clear the registered application handlers.
1329-
*
1330-
* ``` php
1331-
* <?php
1332-
* $I->clearApplicationHandlers();
1333-
* ```
1334-
*/
1335-
public function clearApplicationHandlers(): void
1336-
{
1337-
$this->client->clearApplicationHandlers();
1338-
}
13391191
}
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Codeception\Module\Laravel;
6+
7+
trait InteractsWithContainer
8+
{
9+
/**
10+
* Clear the registered application handlers.
11+
*
12+
* ``` php
13+
* <?php
14+
* $I->clearApplicationHandlers();
15+
* ```
16+
*/
17+
public function clearApplicationHandlers(): void
18+
{
19+
$this->client->clearApplicationHandlers();
20+
}
21+
22+
/**
23+
* Provides access the Laravel application object.
24+
*
25+
* @return \Illuminate\Contracts\Foundation\Application
26+
*/
27+
public function getApplication()
28+
{
29+
return $this->app;
30+
}
31+
32+
/**
33+
* Return an instance of a class from the Laravel service container.
34+
* (https://laravel.com/docs/master/container)
35+
*
36+
* ``` php
37+
* <?php
38+
* // In Laravel
39+
* App::bind('foo', function($app) {
40+
* return new FooBar;
41+
* });
42+
*
43+
* // Then in test
44+
* $service = $I->grabService('foo');
45+
*
46+
* // Will return an instance of FooBar, also works for singletons.
47+
* ```
48+
*
49+
* @param string $class
50+
* @return mixed
51+
*/
52+
public function grabService(string $class)
53+
{
54+
return $this->app[$class];
55+
}
56+
57+
/**
58+
* Register a handler than can be used to modify the Laravel application object after it is initialized.
59+
* The Laravel application object will be passed as an argument to the handler.
60+
*
61+
* ``` php
62+
* <?php
63+
* $I->haveApplicationHandler(function($app) {
64+
* $app->make('config')->set(['test_value' => '10']);
65+
* });
66+
* ```
67+
*
68+
* @param callable $handler
69+
*/
70+
public function haveApplicationHandler(callable $handler): void
71+
{
72+
$this->client->haveApplicationHandler($handler);
73+
}
74+
75+
/**
76+
* Add a binding to the Laravel service container.
77+
* (https://laravel.com/docs/master/container)
78+
*
79+
* ``` php
80+
* <?php
81+
* $I->haveBinding('My\Interface', 'My\Implementation');
82+
* ```
83+
*
84+
* @param string $abstract
85+
* @param Closure|string|null $concrete
86+
* @param bool $shared
87+
*/
88+
public function haveBinding(string $abstract, $concrete = null, bool $shared = false): void
89+
{
90+
$this->client->haveBinding($abstract, $concrete, $shared);
91+
}
92+
93+
/**
94+
* Add a contextual binding to the Laravel service container.
95+
* (https://laravel.com/docs/master/container)
96+
*
97+
* ``` php
98+
* <?php
99+
* $I->haveContextualBinding('My\Class', '$variable', 'value');
100+
*
101+
* // This is similar to the following in your Laravel application
102+
* $app->when('My\Class')
103+
* ->needs('$variable')
104+
* ->give('value');
105+
* ```
106+
*
107+
* @param string $concrete
108+
* @param string $abstract
109+
* @param Closure|string $implementation
110+
*/
111+
public function haveContextualBinding(string $concrete, string $abstract, $implementation): void
112+
{
113+
$this->client->haveContextualBinding($concrete, $abstract, $implementation);
114+
}
115+
116+
/**
117+
* Add an instance binding to the Laravel service container.
118+
* (https://laravel.com/docs/master/container)
119+
*
120+
* ``` php
121+
* <?php
122+
* $I->haveInstance('App\MyClass', new App\MyClass());
123+
* ```
124+
*
125+
* @param string $abstract
126+
* @param mixed $instance
127+
*/
128+
public function haveInstance(string $abstract, $instance): void
129+
{
130+
$this->client->haveInstance($abstract, $instance);
131+
}
132+
133+
/**
134+
* Add a singleton binding to the Laravel service container.
135+
* (https://laravel.com/docs/master/container)
136+
*
137+
* ``` php
138+
* <?php
139+
* $I->haveSingleton('App\MyInterface', 'App\MySingleton');
140+
* ```
141+
*
142+
* @param string $abstract
143+
* @param Closure|string|null $concrete
144+
*/
145+
public function haveSingleton(string $abstract, $concrete): void
146+
{
147+
$this->client->haveBinding($abstract, $concrete, true);
148+
}
149+
150+
/**
151+
* @param \Illuminate\Contracts\Foundation\Application $app
152+
*/
153+
public function setApplication($app): void
154+
{
155+
$this->app = $app;
156+
}
157+
}

0 commit comments

Comments
 (0)