Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
W
webbasics
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Taddeüs Kroes
webbasics
Commits
129dbc4f
Commit
129dbc4f
authored
Oct 06, 2012
by
Taddeus Kroes
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added Session class
parent
f8b5f754
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
223 additions
and
0 deletions
+223
-0
session.php
session.php
+89
-0
tests/SingletonTestCase.php
tests/SingletonTestCase.php
+31
-0
tests/test_session.php
tests/test_session.php
+102
-0
webbasics.php
webbasics.php
+1
-0
No files found.
session.php
0 → 100644
View file @
129dbc4f
<?php
/*
*
*
* @author Taddeus Kroes
* @date 05-10-2012
*/
namespace
webbasics
;
require_once
'base.php'
;
class
Session
{
private
static
$instance
;
static
function
getInstance
()
{
if
(
self
::
$instance
===
null
)
self
::
$instance
=
new
self
;
return
self
::
$instance
;
}
/**
* @codeCoverageIgnore
*/
private
function
__construct
()
{
session_start
();
}
function
set
(
$names
,
$value
=
null
)
{
if
(
is_array
(
$names
))
{
foreach
(
$names
as
$name
=>
$value
)
$this
->
set
(
$name
,
$value
);
}
else
{
$_SESSION
[
$names
]
=
$value
;
}
}
function
get
(
$names
)
{
if
(
is_array
(
$names
))
{
$values
=
array
();
foreach
(
$names
as
$name
)
$values
[]
=
$_SESSION
[
$name
];
return
$values
;
}
return
$_SESSION
[
$names
];
}
function
isRegistered
(
$name
)
{
return
isset
(
$_SESSION
[
$name
]);
}
function
areRegistered
(
array
$names
)
{
foreach
(
$names
as
$name
)
{
if
(
!
isset
(
$_SESSION
[
$name
]))
return
false
;
}
return
true
;
}
function
regenerateId
()
{
session_regenerate_id
();
}
/**
* @codeCoverageIgnore
*/
function
close
()
{
session_write_close
();
}
function
clear
()
{
$_SESSION
=
array
();
}
function
destroy
(
$clear
=
false
)
{
if
(
$clear
)
$this
->
clear
();
session_destroy
();
self
::
$instance
=
null
;
}
}
?>
\ No newline at end of file
tests/SingletonTestCase.php
0 → 100644
View file @
129dbc4f
<?php
abstract
class
SingletonTestCase
extends
PHPUnit_Framework_TestCase
{
private
$rclass
;
abstract
function
getClassName
();
function
setUp
()
{
$this
->
rclass
=
new
ReflectionClass
(
$this
->
getClassName
());
}
function
testConstructorPrivateness
()
{
$rmethod
=
new
ReflectionMethod
(
$this
->
getClassName
(),
'__construct'
);
$this
->
assertTrue
(
$rmethod
->
isPrivate
());
}
function
testInstanceVariable
()
{
$this
->
assertTrue
(
$this
->
rclass
->
hasProperty
(
'instance'
));
$rprop
=
new
ReflectionProperty
(
$this
->
getClassName
(),
'instance'
);
$this
->
assertTrue
(
$rprop
->
isPrivate
());
$this
->
assertTrue
(
$rprop
->
isStatic
());
}
function
testGetInstanceMethod
()
{
$this
->
assertTrue
(
$this
->
rclass
->
hasMethod
(
'getInstance'
));
$rmethod
=
new
ReflectionMethod
(
$this
->
getClassName
(),
'getInstance'
);
$this
->
assertTrue
(
$rmethod
->
isPublic
());
$this
->
assertTrue
(
$rmethod
->
isStatic
());
$this
->
assertEquals
(
0
,
$rmethod
->
getNumberOfParameters
());
}
}
\ No newline at end of file
tests/test_session.php
0 → 100644
View file @
129dbc4f
<?php
require_once
'SingletonTestCase.php'
;
require_once
'session.php'
;
use
webbasics\Session
;
// Turn on outbut buffering to circumvent "headers already sent" error
ob_start
();
class
SessionTest
extends
SingletonTestCase
{
private
$session
;
function
getClassName
()
{
return
'webbasics\Session'
;
}
function
setUp
()
{
parent
::
setUp
();
$this
->
session
=
Session
::
getInstance
();
}
function
tearDown
()
{
$_SESSION
=
array
();
}
function
testSessionStarted
()
{
$this
->
assertNotEquals
(
''
,
session_id
());
}
function
testSetSingle
()
{
$this
->
session
->
set
(
'foo'
,
'bar'
);
$this
->
assertArrayHasKey
(
'foo'
,
$_SESSION
);
$this
->
assertEquals
(
'bar'
,
$_SESSION
[
'foo'
]);
}
function
testSetMultiple
()
{
$this
->
session
->
set
(
array
(
'foo'
=>
'bar'
,
'bar'
=>
'baz'
));
$this
->
assertArrayHasKey
(
'foo'
,
$_SESSION
);
$this
->
assertEquals
(
'bar'
,
$_SESSION
[
'foo'
]);
$this
->
assertArrayHasKey
(
'bar'
,
$_SESSION
);
$this
->
assertEquals
(
'baz'
,
$_SESSION
[
'bar'
]);
}
function
testGetSingle
()
{
$_SESSION
[
'foo'
]
=
'bar'
;
$this
->
assertEquals
(
'bar'
,
$this
->
session
->
get
(
'foo'
));
}
function
testGetMultiple
()
{
$_SESSION
[
'foo'
]
=
'bar'
;
$_SESSION
[
'bar'
]
=
'baz'
;
$this
->
assertEquals
(
array
(
'bar'
,
'baz'
),
$this
->
session
->
get
(
array
(
'foo'
,
'bar'
)));
}
function
testIsRegistered
()
{
$_SESSION
[
'foo'
]
=
'bar'
;
$this
->
assertTrue
(
$this
->
session
->
isRegistered
(
'foo'
));
$this
->
assertFalse
(
$this
->
session
->
isRegistered
(
'bar'
));
}
/**
* @depends testIsRegistered
*/
function
testAreRegistered
()
{
$_SESSION
[
'foo'
]
=
'bar'
;
$_SESSION
[
'bar'
]
=
'baz'
;
$this
->
assertTrue
(
$this
->
session
->
areRegistered
(
array
(
'foo'
,
'bar'
)));
$this
->
assertFalse
(
$this
->
session
->
areRegistered
(
array
(
'foo'
,
'baz'
)));
}
function
testRegenerateId
()
{
$old_id
=
session_id
();
$this
->
session
->
regenerateId
();
$this
->
assertNotEquals
(
$old_id
,
session_id
());
}
function
testClear
()
{
$_SESSION
[
'foo'
]
=
'bar'
;
$this
->
session
->
clear
();
$this
->
assertEmpty
(
$_SESSION
);
}
/**
* @depends testRegenerateId
*/
function
testDestroySimple
()
{
$this
->
session
->
destroy
();
$this
->
assertEquals
(
''
,
session_id
());
}
/**
* @depends testClear
*/
function
testDestroyClear
()
{
$_SESSION
[
'foo'
]
=
'bar'
;
$this
->
session
->
destroy
(
true
);
$this
->
assertEmpty
(
$_SESSION
);
$this
->
assertEquals
(
''
,
session_id
());
}
}
?>
\ No newline at end of file
webbasics.php
View file @
129dbc4f
...
...
@@ -14,6 +14,7 @@ require_once 'autoloader.php';
require_once
'collection.php'
;
require_once
'router.php'
;
require_once
'template.php'
;
require_once
'session.php'
;
// @codeCoverageIgnoreEnd
?>
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment