Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
S
syna
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Thomas Flori
syna
Commits
688a08c1
Unverified
Commit
688a08c1
authored
6 years ago
by
Thomas Flori
Browse files
Options
Downloads
Patches
Plain Diff
test handling of sections
parent
c05dd9be
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/View.php
+6
-5
6 additions, 5 deletions
src/View.php
tests/View/HandlingSectionsTest.php
+166
-0
166 additions, 0 deletions
tests/View/HandlingSectionsTest.php
with
172 additions
and
5 deletions
src/View.php
+
6
−
5
View file @
688a08c1
...
...
@@ -37,6 +37,7 @@ class View
* The string representation of a view is it's rendered content
*
* @return string
* @codeCoverageIgnore Just a wrapper
*/
public
function
__toString
()
{
...
...
@@ -196,7 +197,7 @@ class View
*/
public
function
provide
(
string
$name
,
string
$content
,
bool
$append
=
false
)
{
$content
=
trim
(
$content
,
"
\
t\
n\r\0\x0b
"
);
$content
=
trim
(
$content
,
"
\n\r\0\x0b
"
);
if
(
$append
&&
isset
(
$this
->
sections
[
$name
]))
{
$this
->
sections
[
$name
]
.
=
$content
;
}
else
{
...
...
@@ -205,16 +206,16 @@ class View
}
/**
* Get the content of section $name or $
default
* Get the content of section $name or $
alternative
*
* @param string $name
* @param string|null $
default
* @param string|null $
alternative
* @return string
*/
public
function
section
(
string
$name
,
string
$
default
=
null
):
string
public
function
section
(
string
$name
,
string
$
alternative
=
''
):
string
{
if
(
!
isset
(
$this
->
sections
[
$name
]))
{
return
$
default
;
return
$
alternative
;
}
return
$this
->
sections
[
$name
];
...
...
This diff is collapsed.
Click to expand it.
tests/View/HandlingSectionsTest.php
0 → 100644
+
166
−
0
View file @
688a08c1
<?php
namespace
Syna\Test\View
;
use
Syna\Factory
;
use
Syna\Test\TestCase
;
use
Mockery
as
m
;
use
Syna\ViewLocator
;
class
HandlingSectionsTest
extends
TestCase
{
/** @var m\Mock|Factory */
protected
$factory
;
protected
function
setUp
()
{
parent
::
setUp
();
$this
->
factory
=
m
::
mock
(
Factory
::
class
,
[
new
ViewLocator
(
$this
->
templatePath
)])
->
makePartial
();
}
/** @test */
public
function
contentBetweenStartAndEndBelongsToSection
()
{
$this
->
createTemplate
(
'section.php'
,
'<?php $v->start("foo") ?>Foo<?php $v->end() ?>'
);
$view
=
$this
->
factory
->
view
(
'section'
);
$content
=
$view
->
render
();
self
::
assertSame
(
''
,
$content
);
self
::
assertSame
(
'Foo'
,
$view
->
getSections
()[
'foo'
]);
}
/** @test */
public
function
sectionNameContentIsNotAllowed
()
{
$this
->
createTemplate
(
'section.php'
,
'<?php $v->start("content") ?>Foo<?php $v->end() ?>'
);
$view
=
$this
->
factory
->
view
(
'section'
);
self
::
expectException
(
\LogicException
::
class
);
self
::
expectExceptionMessage
(
'The section name "content" is reserved.'
);
$view
->
render
();
}
/** @test */
public
function
sectionsCanNotBeNested
()
{
$this
->
createTemplate
(
'section.php'
,
'<?php $v->start("foo") ?>
Foo
<?php $v->start("bar"); ?>
Bar
<?php $v->end() ?>
<?php $v->end() ?>'
);
$view
=
$this
->
factory
->
view
(
'section'
);
self
::
expectException
(
\LogicException
::
class
);
self
::
expectExceptionMessage
(
'You cannot nest sections within other sections.'
);
$view
->
render
();
}
/** @test */
public
function
simpleSectionsCanBeCreatedWithProvide
()
{
$this
->
createTemplate
(
'section.php'
,
'<?php $v->provide("foo", "Foo") ?>'
);
$view
=
$this
->
factory
->
view
(
'section'
);
$view
->
render
();
self
::
assertSame
(
'Foo'
,
$view
->
getSections
()[
'foo'
]);
}
/** @test */
public
function
sectionsAreTrimmedExceptSpacesAndTabs
()
{
$this
->
createTemplate
(
'section.php'
,
'<?php $v->start("foo") ?>'
.
PHP_EOL
.
' Foo'
.
PHP_EOL
.
'<?php $v->end() ?>'
);
$view
=
$this
->
factory
->
view
(
'section'
);
$view
->
render
();
self
::
assertSame
(
' Foo'
,
$view
->
getSections
()[
'foo'
]);
}
/** @test */
public
function
sectionsCanBeAccessedUsingSection
()
{
$this
->
createTemplate
(
'parent.php'
,
'<?= $this->section("foo") ?>'
);
$view
=
$this
->
factory
->
view
(
'parent'
);
$view
->
setSections
([
'foo'
=>
'Foo'
]);
$result
=
$view
->
render
();
self
::
assertSame
(
'Foo'
,
$result
);
}
/** @test */
public
function
undefinedSectionsAreEmpty
()
{
$this
->
createTemplate
(
'parent.php'
,
'<?= $this->section("foo") ?>'
);
$view
=
$this
->
factory
->
view
(
'parent'
);
$result
=
$view
->
render
();
self
::
assertSame
(
''
,
$result
);
}
/** @test */
public
function
alternativeUsedForUndefinedSection
()
{
$this
->
createTemplate
(
'parent.php'
,
'<?= $this->section("foo", "Unknown") ?>'
);
$view
=
$this
->
factory
->
view
(
'parent'
);
$result
=
$view
->
render
();
self
::
assertSame
(
'Unknown'
,
$result
);
}
/** @test */
public
function
alternativeIsNotUsedForEmptySections
()
{
$this
->
createTemplate
(
'parent.php'
,
'<?= $this->section("foo", "Unknown") ?>'
);
$view
=
$this
->
factory
->
view
(
'parent'
);
$view
->
setSections
([
'foo'
=>
''
]);
$result
=
$view
->
render
();
self
::
assertSame
(
''
,
$result
);
}
/** @test */
public
function
sectionContentCanBeAppended
()
{
$this
->
createTemplate
(
'section.php'
,
'<?php $v->provide("foo", "Bar", true) ?>'
.
PHP_EOL
.
'<?php $v->start("foo", true) ?>Baz<?php $v->end() ?>'
);
$view
=
$this
->
factory
->
view
(
'section'
);
$view
->
setSections
([
'foo'
=>
'Foo'
]);
$view
->
render
();
self
::
assertSame
(
'FooBarBaz'
,
$view
->
getSections
()[
'foo'
]);
}
/** @test */
public
function
sectionHasToBeStartedBeforeEnding
()
{
$this
->
createTemplate
(
'section.php'
,
'<?php $v->end() ?>'
);
$view
=
$this
->
factory
->
view
(
'section'
);
self
::
expectException
(
\LogicException
::
class
);
self
::
expectExceptionMessage
(
'You must start a section before you can end it.'
);
$view
->
render
();
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment