├── .gitignore
├── .travis.yml
├── LICENSE
├── README.md
├── RoboFile.php
├── composer.json
├── src
└── Session.php
└── tests
├── SessionTest.php
├── bootstrap.php
└── environment
├── globalise.php
└── index.php
/.gitignore:
--------------------------------------------------------------------------------
1 | vendor
2 | composer.lock
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | language: php
2 |
3 | php:
4 | - "5.5"
5 | - "5.4"
6 |
7 | install: "composer install"
8 |
9 | script: "./vendor/bin/robo test"
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) 2014 Brad Jones
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | > Looking for maintainers, I no longer do much if any PHP dev, I have moved on, mostly work in dotnet core, node.js & golang these days. If anyone is keen to take over these projects, get in touch - brad@bjc.id.au
2 |
3 | The Session Gear
4 | ================================================================================
5 | [](https://travis-ci.org/phpgearbox/session)
6 | [](https://packagist.org/packages/gears/session)
7 | [](https://packagist.org/packages/gears/session)
8 | [](https://packagist.org/packages/gears/session)
9 |
10 | **Laravel Sessions Standalone**
11 |
12 | Okay so by now hopefully you have heard of [Laravel](http://laravel.com/),
13 | the PHP framework that just makes things easy. So first things first full credit
14 | goes to [Taylor Otwell](https://github.com/taylorotwell) for the Session API.
15 |
16 | How to Install
17 | --------------------------------------------------------------------------------
18 | Installation via composer is easy:
19 |
20 | composer require gears/session:*
21 |
22 | How to Use
23 | --------------------------------------------------------------------------------
24 | In your *legacy* - non Laravel application.
25 | You can use the Laravel Session API like so:
26 |
27 | ```php
28 | // Make sure you have composer included
29 | require('vendor/autoload.php');
30 |
31 | // Create a new gears session.
32 | $session = new Gears\Session();
33 |
34 | // Configure the session container
35 | $session->dbConfig =
36 | [
37 | 'driver' => 'mysql',
38 | 'host' => 'localhost',
39 | 'database' => 'db_name',
40 | 'username' => 'db_user',
41 | 'password' => 'abc123',
42 | 'charset' => 'utf8',
43 | 'collation' => 'utf8_unicode_ci',
44 | 'prefix' => '',
45 | ];
46 |
47 | // Install the session api
48 | $session->install();
49 |
50 | // Next you will probably want to make the session object global.
51 | $session->globalise();
52 | ```
53 |
54 | > NOTE: The dbConfig array must describe a valid db connection. This array is
55 | > passed directly to $capsule->addConnection For more info on this see:
56 | >
57 | > - http://laravel.com/docs/database
58 | > - https://github.com/laravel/framework/tree/master/src/Illuminate/Database
59 |
60 | Now you can use code like the following:
61 |
62 | ```php
63 | // Storing An Item In The Session
64 | Session::put('key', 'value');
65 |
66 | // Push A Value Onto An Array Session Value
67 | Session::push('user.teams', 'developers');
68 |
69 | // Retrieving An Item From The Session
70 | $value = Session::get('key');
71 |
72 | // Retrieving An Item Or Returning A Default Value
73 | $value = Session::get('key', 'default');
74 | $value = Session::get('key', function() { return 'default'; });
75 |
76 | // Retrieving An Item And Forgetting It
77 | $value = Session::pull('key', 'default');
78 |
79 | // Retrieving All Data From The Session
80 | $data = Session::all();
81 |
82 | // Determining If An Item Exists In The Session
83 | if (Session::has('users'))
84 | {
85 | //
86 | }
87 |
88 | // Removing An Item From The Session
89 | Session::forget('key');
90 |
91 | // Removing All Items From The Session
92 | Session::flush();
93 |
94 | // Regenerating The Session ID
95 | Session::regenerate();
96 |
97 | // Flashing Data
98 | Session::flash('key', 'value');
99 |
100 | // Reflashing The Current Flash Data For Another Request
101 | Session::reflash();
102 |
103 | // Reflashing Only A Subset Of Flash Data
104 | Session::keep(array('username', 'email'));
105 | ```
106 |
107 | For more info on the Session API it's self see:
108 | http://laravel.com/docs/session
109 |
110 | *NOTE: While the Laravel Session API does provide support for many different
111 | drivers. This package only supports the database driver (for now).*
112 |
113 | **WARINING: Do not use the built in native PHP session
114 | functions and / or the global $_SESSION array**
115 |
116 | Our Extra Method: hasExpired
117 | --------------------------------------------------------------------------------
118 | To my current knowledge of Laravel, there is no built in way to work out if a
119 | Session has been set but then expired. So in a normal Laravel app if you wanted
120 | to display a "Your Session has expired!" message you would need to do some
121 | custom filters or something... see:
122 |
123 | http://stackoverflow.com/questions/14688853/check-for-session-timeout-in-laravel
124 |
125 | But with *Gears\Session* just call:
126 |
127 | ```php
128 | if (Session::hasExpired())
129 | {
130 | echo 'Due to inactivity, your session has expired!';
131 | echo 'Please click here to login again.';
132 | }
133 | ```
134 |
135 | So now for the why?
136 | --------------------------------------------------------------------------------
137 | While laravel is so awesomely cool and great. If you want to pull a feature out
138 | and use it in another project it can become difficult. Firstly you have to have
139 | an innate understanding of the [IoC Container](http://laravel.com/docs/ioc).
140 |
141 | You then find that this class needs that class which then requires some other
142 | config variable that is normally present in the IoC when run inside a normal
143 | Laravel App but in your case you haven't defined it and don't really want
144 | to define that value because it makes no sense in your lets say *legacy*
145 | application.
146 |
147 | Perfect example is when I tried to pull the session API out to use in WordPress.
148 | It wanted to know about a ```booted``` method, which I think comes from
149 | ```Illuminate\Foundation\Application```. At this point in time I already had to
150 | add various other things into the IoC to make it happy and it was the last straw
151 | that broke the camels back, I chucked a coders tantrum, walked to the fridge,
152 | grabbed another Redbull and sat back down with a new approach.
153 |
154 | The result is this project.
155 |
156 | --------------------------------------------------------------------------------
157 | Developed by Brad Jones - brad@bjc.id.au
158 |
--------------------------------------------------------------------------------
/RoboFile.php:
--------------------------------------------------------------------------------
1 | > \_\ \ ___/ / __ \| | \/ | ( <_> > <
7 | // |____| |___| / __/ \______ /\___ >____ /__| |______ /\____/__/\_ \
8 | // \/|__| \/ \/ \/ \/ \/
9 | // -----------------------------------------------------------------------------
10 | // Designed and Developed by Brad Jones
11 | // -----------------------------------------------------------------------------
12 | ////////////////////////////////////////////////////////////////////////////////
13 |
14 | class RoboFile extends Robo\Tasks
15 | {
16 | /**
17 | * Property: serverPort
18 | * =========================================================================
19 | * The port the built in PHP server will run on for our acceptance testing.
20 | */
21 | public static $serverPort = 8000;
22 |
23 | /**
24 | * Method: test
25 | * =========================================================================
26 | * This will run our unit / acceptance testing. All the *gears* within
27 | * the **PhpGearBox** utlise PhpUnit as the basis for our testing with the
28 | * addition of the built in PHP Web Server, making the acceptance tests
29 | * almost as portable as standard unit tests.
30 | *
31 | * Just run: ```php ./vendor/bin/robo test```
32 | *
33 | * Parameters:
34 | * -------------------------------------------------------------------------
35 | * n/a
36 | *
37 | * Returns:
38 | * -------------------------------------------------------------------------
39 | * void
40 | */
41 | public function test()
42 | {
43 | $this->taskServer(self::$serverPort)
44 | ->dir('./tests/environment')
45 | ->background(true)
46 | ->run();
47 |
48 | exit($this->taskPHPUnit()->bootstrap('./tests/bootstrap.php')->arg('./tests')->run()->getExitCode());
49 | }
50 | }
--------------------------------------------------------------------------------
/composer.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "gears/session",
3 | "description": "Laravel Sessions Standalone",
4 | "homepage": "https://github.com/phpgearbox/session",
5 | "keywords": ["laravel", "sessions", "standalone"],
6 | "license": "MIT",
7 | "autoload":
8 | {
9 | "psr-4":
10 | {
11 | "Gears\\": "src"
12 | }
13 | },
14 | "require":
15 | {
16 | "gears/di": "*",
17 | "illuminate/session": "4.*",
18 | "illuminate/database": "4.*"
19 | },
20 | "require-dev":
21 | {
22 | "phpunit/phpunit": "4.*",
23 | "codegyre/robo": "*",
24 | "guzzlehttp/guzzle": "4.*"
25 | }
26 | }
--------------------------------------------------------------------------------
/src/Session.php:
--------------------------------------------------------------------------------
1 | > \_\ \ ___/ / __ \| | \/ | ( <_> > <
7 | // |____| |___| / __/ \______ /\___ >____ /__| |______ /\____/__/\_ \
8 | // \/|__| \/ \/ \/ \/ \/
9 | // -----------------------------------------------------------------------------
10 | // Designed and Developed by Brad Jones
11 | // -----------------------------------------------------------------------------
12 | ////////////////////////////////////////////////////////////////////////////////
13 |
14 | use RuntimeException;
15 | use Gears\Di\Container;
16 | use Illuminate\Database\Capsule\Manager as LaravelDb;
17 | use Illuminate\Session\Store;
18 | use Illuminate\Session\DatabaseSessionHandler;
19 |
20 | class Session extends Container
21 | {
22 | /**
23 | * Property: name
24 | * =========================================================================
25 | * Used to identify the session, the name of the actual session cookie.
26 | */
27 | protected $injectName;
28 |
29 | /**
30 | * Property: lifetime
31 | * =========================================================================
32 | * The time in seconds before garbage collection is run on the server.
33 | */
34 | protected $injectLifetime;
35 |
36 | /**
37 | * Property: path
38 | * =========================================================================
39 | * This is passed directly to setcookie.
40 | * See: http://php.net/manual/en/function.setcookie.php
41 | */
42 | protected $injectPath;
43 |
44 | /**
45 | * Property: domain
46 | * =========================================================================
47 | * This is passed directly to setcookie.
48 | * See: http://php.net/manual/en/function.setcookie.php
49 | */
50 | protected $injectDomain;
51 |
52 | /**
53 | * Property: secure
54 | * =========================================================================
55 | * This is passed directly to setcookie.
56 | * See: http://php.net/manual/en/function.setcookie.php
57 | */
58 | protected $injectSecure;
59 |
60 | /**
61 | * Property: dbConfig
62 | * =========================================================================
63 | * This is an array of configuration data that can be used to create the
64 | * dbConnection. This **must** be injected, if you do not inject your very
65 | * own dbConnection.
66 | */
67 | protected $injectDbConfig;
68 |
69 | /**
70 | * Property: table
71 | * =========================================================================
72 | * The name of the database table to use for session storage.
73 | */
74 | protected $injectTable;
75 |
76 | /**
77 | * Property: dbConnection
78 | * =========================================================================
79 | * An instance of ```\Illuminate\Database\Connection```.
80 | */
81 | protected $injectDbConnection;
82 |
83 | /**
84 | * Property: databaseSessionHandler
85 | * =========================================================================
86 | * An instance of ```Illuminate\Session\DatabaseSessionHandler```.
87 | */
88 | protected $injectDatabaseSessionHandler;
89 |
90 | /**
91 | * Property: sessionStore
92 | * =========================================================================
93 | * An instance of ```Illuminate\Session\Store```.
94 | */
95 | protected $injectSessionStore;
96 |
97 | /**
98 | * Property: expired
99 | * =========================================================================
100 | * We have added in some extra functionality. We can now easily check to
101 | * see if the session has expired. If it has we reset the cookie with a
102 | * new id, etc.
103 | */
104 | private $expired = false;
105 |
106 | /**
107 | * Property: instance
108 | * =========================================================================
109 | * This is used as part of the globalise functionality.
110 | */
111 | private static $instance;
112 |
113 | /**
114 | * Method: setDefaults
115 | * =========================================================================
116 | * This is where we set all our defaults. If you need to customise this
117 | * container this is a good place to look to see what can be configured
118 | * and how to configure it.
119 | *
120 | * Parameters:
121 | * -------------------------------------------------------------------------
122 | * n/a
123 | *
124 | * Returns:
125 | * -------------------------------------------------------------------------
126 | * void
127 | */
128 | protected function setDefaults()
129 | {
130 | $this->name = 'gears-session';
131 |
132 | $this->table = 'sessions';
133 |
134 | $this->lifetime = 120;
135 |
136 | $this->path = '/';
137 |
138 | $this->dbConnection = function()
139 | {
140 | if (!is_array($this->dbConfig))
141 | {
142 | throw new RuntimeException
143 | (
144 | 'Invalid Database Connection Provided'
145 | );
146 | }
147 |
148 | $capsule = new LaravelDb;
149 | $capsule->addConnection($this->dbConfig);
150 | return $capsule->getConnection('default');
151 | };
152 |
153 | $this->databaseSessionHandler = function()
154 | {
155 | return new DatabaseSessionHandler
156 | (
157 | $this->dbConnection,
158 | $this->table
159 | );
160 | };
161 |
162 | $this->sessionStore = function()
163 | {
164 | return new Store
165 | (
166 | $this->name,
167 | $this->databaseSessionHandler
168 | );
169 | };
170 | }
171 |
172 | /**
173 | * Method: install
174 | * =========================================================================
175 | * Once the container has been configured. Please call this method to
176 | * install the session api into your application.
177 | *
178 | * Parameters:
179 | * -------------------------------------------------------------------------
180 | * - $global: If set to true we will also run globalise after setup.
181 | *
182 | * Returns:
183 | * -------------------------------------------------------------------------
184 | * void
185 | */
186 | public function install($global = false)
187 | {
188 | // Make sure we have a sessions table
189 | $schema = $this->dbConnection->getSchemaBuilder();
190 | if (!$schema->hasTable($this->table))
191 | {
192 | $schema->create($this->table, function($t)
193 | {
194 | $t->string('id')->unique();
195 | $t->text('payload');
196 | $t->integer('last_activity');
197 | });
198 | }
199 |
200 | // Run the garbage collection
201 | $this->sessionStore->getHandler()->gc($this->lifetime);
202 |
203 | // Check for our session cookie
204 | if (isset($_COOKIE[$this->name]))
205 | {
206 | // Grab the session id from the cookie
207 | $cookie_id = $_COOKIE[$this->name];
208 |
209 | // Does the session exist in the db?
210 | $session = (object) $this->dbConnection
211 | ->table($this->table)
212 | ->find($cookie_id)
213 | ;
214 |
215 | if (isset($session->payload))
216 | {
217 | // Set the id of the session
218 | $this->sessionStore->setId($cookie_id);
219 | }
220 | else
221 | {
222 | // Set the expired flag
223 | $this->expired = true;
224 |
225 | // NOTE: We do not need to set the id here.
226 | // As it has already been set by the constructor of the Store.
227 | }
228 | }
229 |
230 | // Set / reset the session cookie
231 | if (!isset($_COOKIE[$this->name]) || $this->expired)
232 | {
233 | setcookie
234 | (
235 | $this->name,
236 | $this->sessionStore->getId(),
237 | 0,
238 | $this->path,
239 | $this->domain,
240 | $this->secure,
241 | true
242 | );
243 | }
244 |
245 | // Start the session
246 | $this->sessionStore->start();
247 |
248 | // Save the session on shutdown
249 | register_shutdown_function([$this->sessionStore, 'save']);
250 |
251 | // Run globalise
252 | if ($global) $this->globalise();
253 | }
254 |
255 | /**
256 | * Method: hasExpired
257 | * =========================================================================
258 | * Pretty simple, if the session has previously been set and now has been
259 | * expired by means of garbage collection on the server, this will return
260 | * true, otherwise false.
261 | *
262 | * Parameters:
263 | * -------------------------------------------------------------------------
264 | * n/a
265 | *
266 | * Returns:
267 | * -------------------------------------------------------------------------
268 | * boolean
269 | */
270 | public function hasExpired()
271 | {
272 | return $this->expired;
273 | }
274 |
275 | /**
276 | * Method: regenerate
277 | * =========================================================================
278 | * When the session id is regenerated we need to reset the cookie.
279 | *
280 | * Parameters:
281 | * -------------------------------------------------------------------------
282 | * - $destroy: If set to true the previous session will be deleted.
283 | *
284 | * Returns:
285 | * -------------------------------------------------------------------------
286 | * boolean
287 | */
288 | public function regenerate($destroy = false)
289 | {
290 | if ($this->sessionStore->regenerate($destroy))
291 | {
292 | setcookie
293 | (
294 | $this->sessionStore->getName(),
295 | $this->sessionStore->getId(),
296 | 0,
297 | $this->path,
298 | $this->domain,
299 | $this->secure,
300 | true
301 | );
302 |
303 | return true;
304 | }
305 | else
306 | {
307 | return false;
308 | }
309 | }
310 |
311 | /**
312 | * Method: globalise
313 | * =========================================================================
314 | * Now in a normal laravel application you can call the session api like so:
315 | *
316 | * ```php
317 | * Session::push('key', 'value');
318 | * ```
319 | *
320 | * This is because laravel has the IoC container with Service Providers and
321 | * Facades and other intresting things that work some magic to set this up
322 | * for you. Have a look in you main app.php config file and checkout the
323 | * aliases section.
324 | *
325 | * If you want to be able to do the same in your
326 | * application you need to call this method.
327 | *
328 | * Parameters:
329 | * -------------------------------------------------------------------------
330 | * - $alias: This is the name of the alias to create. Defaults to Session.
331 | *
332 | * Returns:
333 | * -------------------------------------------------------------------------
334 | * void
335 | *
336 | * Throws:
337 | * -------------------------------------------------------------------------
338 | * - RuntimeException: When a class of the same name as the alias
339 | * already exists.
340 | */
341 | public function globalise($alias = 'Session')
342 | {
343 | // Create the alias name
344 | if (substr($alias, 0, 1) != '\\')
345 | {
346 | // This ensures the alias is created in the global namespace.
347 | $alias = '\\'.$alias;
348 | }
349 |
350 | // Check if a class already exists
351 | if (class_exists($alias))
352 | {
353 | // Bail out, a class already exists with the same name.
354 | throw new RuntimeException('Class already exists!');
355 | }
356 |
357 | // Create the alias
358 | class_alias('\Gears\Session', $alias);
359 |
360 | // Save our instance
361 | self::$instance = $this;
362 | }
363 |
364 | /**
365 | * Method: __call
366 | * =========================================================================
367 | * This will pass any unresolved method calls
368 | * through to the main session store object.
369 | *
370 | * Parameters:
371 | * -------------------------------------------------------------------------
372 | * - $name: The name of the method to call.
373 | * - $args: The argumnent array that is given to us.
374 | *
375 | * Returns:
376 | * -------------------------------------------------------------------------
377 | * mixed
378 | */
379 | public function __call($name, $args)
380 | {
381 | return call_user_func_array([$this->sessionStore, $name], $args);
382 | }
383 |
384 | /**
385 | * Method: __callStatic
386 | * =========================================================================
387 | * This will pass any unresolved static method calls
388 | * through to the saved instance.
389 | *
390 | * Parameters:
391 | * -------------------------------------------------------------------------
392 | * - $name: The name of the method to call.
393 | * - $args: The argumnent array that is given to us.
394 | *
395 | * Returns:
396 | * -------------------------------------------------------------------------
397 | * mixed
398 | *
399 | * Throws:
400 | * -------------------------------------------------------------------------
401 | * - RuntimeException: When we have not been globalised.
402 | */
403 | public static function __callStatic($name, $args)
404 | {
405 | if (empty(self::$instance))
406 | {
407 | throw new RuntimeException('You need to run globalise first!');
408 | }
409 |
410 | return call_user_func_array([self::$instance, $name], $args);
411 | }
412 | }
--------------------------------------------------------------------------------
/tests/SessionTest.php:
--------------------------------------------------------------------------------
1 | > \_\ \ ___/ / __ \| | \/ | ( <_> > <
7 | // |____| |___| / __/ \______ /\___ >____ /__| |______ /\____/__/\_ \
8 | // \/|__| \/ \/ \/ \/ \/
9 | // -----------------------------------------------------------------------------
10 | // Designed and Developed by Brad Jones
11 | // -----------------------------------------------------------------------------
12 | ////////////////////////////////////////////////////////////////////////////////
13 |
14 | use Illuminate\Database\Capsule\Manager as LaravelDb;
15 |
16 | class SessionTest extends PHPUnit_Framework_TestCase
17 | {
18 | /**
19 | * Property: $db
20 | * =========================================================================
21 | * We store an instance of Illuminate\Database\Connection here.
22 | */
23 | protected $db;
24 |
25 | /**
26 | * Property: $http
27 | * =========================================================================
28 | * We store an instance of GuzzleHttp\Client here.
29 | */
30 | protected $http;
31 |
32 | /**
33 | * Method: setUp
34 | * =========================================================================
35 | * This is run before our tests. It creates the above properties.
36 | *
37 | * Parameters:
38 | * -------------------------------------------------------------------------
39 | * n/a
40 | *
41 | * Returns:
42 | * -------------------------------------------------------------------------
43 | * void
44 | */
45 | protected function setUp()
46 | {
47 | // Create a blank sqlite db
48 | // Laravel complains if the actual file does not exist.
49 | touch('/tmp/gears-session-test.db');
50 |
51 | // Grab a laravel db connection
52 | $capsule = new LaravelDb;
53 | $capsule->addConnection
54 | ([
55 | 'driver' => 'sqlite',
56 | 'database' => '/tmp/gears-session-test.db',
57 | 'prefix' => ''
58 | ]);
59 | $this->db = $capsule->getConnection('default');
60 |
61 | // Get a new guzzle client
62 | $this->http = GuzzleTester();
63 | }
64 |
65 | /**
66 | * Method: testDefaultSession
67 | * =========================================================================
68 | * This test simply checks to make sure the basics are working.
69 | * Please see ./tests/environment/index.php for it's counterpart.
70 | *
71 | * Parameters:
72 | * -------------------------------------------------------------------------
73 | * n/a
74 | *
75 | * Returns:
76 | * -------------------------------------------------------------------------
77 | * void
78 | */
79 | public function testDefaultSession()
80 | {
81 | // Make an intial request
82 | $response = $this->http->get();
83 |
84 | // Check to see if the db schema is valid
85 | $schema = $this->db->getSchemaBuilder();
86 | $this->assertTrue($schema->hasTable('sessions'));
87 |
88 | // NOTE: The following 3 assertions fail due to a bug
89 | // in the laravel code. Hence commented out for now.
90 | //$this->assertTrue($schema->hasColumn('sessions', 'id'));
91 | //$this->assertTrue($schema->hasColumn('sessions', 'payload'));
92 | //$this->assertTrue($schema->hasColumn('sessions', 'last_activity'));
93 |
94 | // Check for the session cookie
95 | $headers = $response->getHeader('Set-Cookie', true);
96 | $this->assertContains('gears-session', $headers[0]);
97 |
98 | // Check that we have only one bar
99 | $this->assertEquals(['bar'], $response->json()['foo']);
100 |
101 | // Make a new request
102 | $response = $this->http->get();
103 |
104 | // Now we should have 2 bars - this proves the session is working
105 | $this->assertEquals(['bar', 'bar'], $response->json()['foo']);
106 | }
107 |
108 | /**
109 | * Method: testGlobalise
110 | * =========================================================================
111 | * This test checks that the globalise functionality works as expected.
112 | * Please see ./tests/environment/globalise.php for it's counterpart.
113 | *
114 | * Parameters:
115 | * -------------------------------------------------------------------------
116 | * n/a
117 | *
118 | * Returns:
119 | * -------------------------------------------------------------------------
120 | * void
121 | */
122 | public function testGlobalise()
123 | {
124 | // Make a new request
125 | $response = $this->http->get('/globalise.php');
126 |
127 | // Check for the global key
128 | $this->assertArrayHasKey('global', $response->json());
129 | }
130 |
131 | /**
132 | * Method: tearDown
133 | * =========================================================================
134 | * This is run after all our tests and removes the test sqlite db.
135 | *
136 | * Parameters:
137 | * -------------------------------------------------------------------------
138 | * n/a
139 | *
140 | * Returns:
141 | * -------------------------------------------------------------------------
142 | * void
143 | */
144 | protected function tearDown()
145 | {
146 | // Clean up, delete the tmp db
147 | unlink('/tmp/gears-session-test.db');
148 | }
149 | }
--------------------------------------------------------------------------------
/tests/bootstrap.php:
--------------------------------------------------------------------------------
1 | > \_\ \ ___/ / __ \| | \/ | ( <_> > <
7 | // |____| |___| / __/ \______ /\___ >____ /__| |______ /\____/__/\_ \
8 | // \/|__| \/ \/ \/ \/ \/
9 | // -----------------------------------------------------------------------------
10 | // Designed and Developed by Brad Jones
11 | // -----------------------------------------------------------------------------
12 | ////////////////////////////////////////////////////////////////////////////////
13 |
14 | /*
15 | * Include the robo file so we know what port
16 | * the built in php server is running on.
17 | */
18 | require(__DIR__.'/../RoboFile.php');
19 |
20 | /*
21 | * Create the base Guzzle Client we will use for all our acceptance testing.
22 | * NOTE: We return a new client each time so that we don't have any chance of
23 | * cross contamination.
24 | */
25 | function GuzzleTester()
26 | {
27 | return new GuzzleHttp\Client
28 | ([
29 | 'base_url' => 'http://127.0.0.1:'.RoboFile::$serverPort,
30 | 'defaults' => ['cookies' => true]
31 | ]);
32 | }
--------------------------------------------------------------------------------
/tests/environment/globalise.php:
--------------------------------------------------------------------------------
1 | > \_\ \ ___/ / __ \| | \/ | ( <_> > <
7 | // |____| |___| / __/ \______ /\___ >____ /__| |______ /\____/__/\_ \
8 | // \/|__| \/ \/ \/ \/ \/
9 | // -----------------------------------------------------------------------------
10 | // Designed and Developed by Brad Jones
11 | // -----------------------------------------------------------------------------
12 | ////////////////////////////////////////////////////////////////////////////////
13 |
14 | namespace FooBar
15 | {
16 | function test()
17 | {
18 | // Create a new session object.
19 | // Note how we are inside another namespace.
20 | $session = new \Gears\Session();
21 |
22 | // Configure the session object
23 | $session->dbConfig =
24 | [
25 | 'driver' => 'sqlite',
26 | 'database' => '/tmp/gears-session-test.db',
27 | 'prefix' => ''
28 | ];
29 |
30 | // Install the session api
31 | $session->install();
32 |
33 | // Globalise the session
34 | $session->globalise();
35 | }
36 | }
37 |
38 | namespace
39 | {
40 | // Load the composer autoloader
41 | require('../../vendor/autoload.php');
42 |
43 | // Call the FooBar\test function to create the session
44 | FooBar\test();
45 |
46 | // Note how we have access to the session api globally
47 | Session::put('global', true);
48 |
49 | // Output the session as json for testing
50 | echo json_encode(Session::all());
51 | }
--------------------------------------------------------------------------------
/tests/environment/index.php:
--------------------------------------------------------------------------------
1 | > \_\ \ ___/ / __ \| | \/ | ( <_> > <
7 | // |____| |___| / __/ \______ /\___ >____ /__| |______ /\____/__/\_ \
8 | // \/|__| \/ \/ \/ \/ \/
9 | // -----------------------------------------------------------------------------
10 | // Designed and Developed by Brad Jones
11 | // -----------------------------------------------------------------------------
12 | ////////////////////////////////////////////////////////////////////////////////
13 |
14 | // Load the composer autoloader
15 | require('../../vendor/autoload.php');
16 |
17 | // Create a new session object
18 | $session = new Gears\Session();
19 |
20 | // Configure the session object
21 | $session->dbConfig =
22 | [
23 | 'driver' => 'sqlite',
24 | 'database' => '/tmp/gears-session-test.db',
25 | 'prefix' => ''
26 | ];
27 |
28 | // Install the session api
29 | $session->install();
30 |
31 | // Add a value to the session
32 | $session->push('foo', 'bar');
33 |
34 | // Output the session as json for testing
35 | echo json_encode($session->all());
--------------------------------------------------------------------------------