11<?php
22namespace Codeception \Lib \Connector ;
33
4+ use Illuminate \Database \Eloquent \Model ;
45use Illuminate \Foundation \Application ;
56use Illuminate \Http \Request ;
6- use Symfony \Component \HttpFoundation \Request as SyfmonyRequest ;
7+ use Symfony \Component \HttpFoundation \Request as SymfonyRequest ;
78use Symfony \Component \HttpFoundation \Response ;
89use Symfony \Component \HttpKernel \Client ;
9- use Symfony \Component \HttpKernel \HttpKernelInterface ;
10- use Symfony \Component \HttpKernel \TerminableInterface ;
1110
12- class Laravel5 extends Client implements HttpKernelInterface, TerminableInterface
11+ class Laravel5 extends Client
1312{
1413
1514 /**
@@ -18,61 +17,96 @@ class Laravel5 extends Client implements HttpKernelInterface, TerminableInterfac
1817 private $ app ;
1918
2019 /**
21- * @var HttpKernelInterface
20+ * @var \Codeception\Module\Laravel5
2221 */
23- private $ httpKernel ;
22+ private $ module ;
2423
2524 /**
2625 * Constructor.
27- *
28- * @param Application $app
26+ * @param \Codeception\Module\Laravel5 $module
2927 */
30- public function __construct (Application $ app )
28+ public function __construct ($ module )
3129 {
32- $ this ->app = $ app ;
33- $ this ->httpKernel = $ this ->app ->make ('Illuminate\Contracts\Http\Kernel ' );
34- $ this ->httpKernel ->bootstrap ();
35- $ this ->app ->boot ();
30+ $ this ->module = $ module ;
31+ $ this ->initialize ();
3632
37- $ url = $ this ->app ->config ->get ('app.url ' , 'http://localhost ' );
38- $ this ->app ->instance ('request ' , Request::createFromBase (SyfmonyRequest::create ($ url )));
39-
40- $ components = parse_url ($ url );
33+ $ components = parse_url ($ this ->app ['config ' ]->get ('app.url ' , 'http://localhost ' ));
4134 $ host = isset ($ components ['host ' ]) ? $ components ['host ' ] : 'localhost ' ;
4235
43- parent ::__construct ($ this , ['HTTP_HOST ' => $ host ]);
36+ parent ::__construct ($ this -> app , ['HTTP_HOST ' => $ host ]);
4437
45- $ this ->followRedirects (true ); // Parent constructor sets this to false by default
38+ // Parent constructor defaults to not following redirects
39+ $ this ->followRedirects (true );
4640 }
4741
4842 /**
49- * Handle a request.
50- *
51- * @param SyfmonyRequest $request
52- * @param int $type
53- * @param bool $catch
43+ * @param SymfonyRequest $request
5444 * @return Response
5545 */
56- public function handle ( SyfmonyRequest $ request, $ type = self :: MASTER_REQUEST , $ catch = true )
46+ protected function doRequest ( $ request )
5747 {
48+ $ this ->initialize ();
49+
5850 $ request = Request::createFromBase ($ request );
59- $ request ->enableHttpMethodParameterOverride ();
51+ $ response = $ this ->kernel ->handle ($ request );
52+ $ this ->app ->make ('Illuminate\Contracts\Http\Kernel ' )->terminate ($ request , $ response );
53+
54+ return $ response ;
55+ }
6056
61- $ this ->app ->bind ('request ' , $ request );
57+ /**
58+ * Initialize the Laravel framework.
59+ */
60+ private function initialize ()
61+ {
62+ // Store a reference to the database object
63+ // so the database connection can be reused during tests
64+ $ oldDb = null ;
65+ if ($ this ->app ['db ' ] && $ this ->app ['db ' ]->connection ()) {
66+ $ oldDb = $ this ->app ['db ' ];
67+ }
6268
63- return $ this ->httpKernel ->handle ($ request );
69+ // The module can login a user with the $I->amLoggedAs() method,
70+ // but this is not persisted between requests. Store a reference
71+ // to the logged in user to simulate this.
72+ $ loggedInUser = null ;
73+ if ($ this ->app ['auth ' ] && $ this ->app ['auth ' ]->check ()) {
74+ $ loggedInUser = $ this ->app ['auth ' ]->user ();
75+ }
76+
77+ $ this ->app = $ this ->kernel = $ this ->loadApplication ();
78+ $ this ->app ->make ('Illuminate\Contracts\Http\Kernel ' )->bootstrap ();
79+
80+ // Set the base url for the Request object
81+ $ url = $ this ->app ['config ' ]->get ('app.url ' , 'http://localhost ' );
82+ $ this ->app ->instance ('request ' , Request::createFromBase (SymfonyRequest::create ($ url )));
83+
84+ if ($ oldDb ) {
85+ $ this ->app ['db ' ] = $ oldDb ;
86+ Model::setConnectionResolver ($ this ->app ['db ' ]);
87+ }
88+
89+ // If there was a user logged in restore this user.
90+ // Also reload the user object from the user provider to prevent stale user data.
91+ if ($ loggedInUser ) {
92+ $ refreshed = $ this ->app ['auth ' ]->getProvider ()->retrieveById ($ loggedInUser ->getAuthIdentifier ());
93+ $ this ->app ['auth ' ]->setUser ($ refreshed ?: $ loggedInUser );
94+ }
95+
96+ $ this ->module ->setApplication ($ this ->app );
6497 }
6598
6699 /**
67- * Terminates a request/response cycle.
68- *
69- * @param SyfmonyRequest $request A Request instance
70- * @param Response $response A Response instance
71- *
72- * @api
100+ * Boot the Laravel application object.
101+ * @return Application
102+ * @throws ModuleConfig
73103 */
74- public function terminate ( SyfmonyRequest $ request , Response $ response )
104+ private function loadApplication ( )
75105 {
76- $ this ->httpKernel ->terminate (Request::createFromBase ($ request ), $ response );
106+ $ app = require $ this ->module ->config ['bootstrap_file ' ];
107+ $ app ->loadEnvironmentFrom ($ this ->module ->config ['environment_file ' ]);
108+ $ app ->instance ('request ' , new Request ());
109+
110+ return $ app ;
77111 }
78112}
0 commit comments