Skip to content

Commit d98156e

Browse files
author
James Wigger
committed
Added remaining layout tests. Updated readme and add changelog
1 parent ed76bb6 commit d98156e

19 files changed

+259
-46
lines changed

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Change Log
2+
3+
## v2.0.0 - 08/08/2017
4+
5+
* Renamed `mix()` to `base_asset()`
6+
* Renamed `public_path()` to `base_public_path()`
7+
* Added `base_http_host()` function
8+
* Renamed `Base::class` to `BaseLayout::class`
9+
* Added `Base::class` for path configuration
10+
* Added test coverage for helper functions

README.md

Lines changed: 51 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# BaseLayout-PHP-Helpers
22

3-
PHP Helper functions for building sites using BaseLayout 5
3+
PHP Helper functions for building sites using Base 5.
44

55
## Installation
66

@@ -14,19 +14,27 @@ Add the following to your `composer.json` file:
1414
}
1515
],
1616
"require": {
17-
"rootstudio/base-php-helpers": "^1.1",
17+
"rootstudio/base-php-helpers": "^2.0",
18+
}
19+
```
20+
21+
The v2.x version of the library requires PHP 7 or higher. For versions below this change the require statement to:
22+
23+
```json
24+
"require": {
25+
"rootstudio/base-php-helpers": "^1.2",
1826
}
1927
```
2028

2129
## Features
2230

2331
This package includes PHP functions that streamline building sites using the BaseLayout 5 boilerplate.
2432

25-
* `mix()` and `public_path()` functions for asset management
33+
* `base_asset()` and `base_public_path()` functions for asset management
2634
* API compatibility with `perch_layout_*` functions
2735
* Additional PHP libraries for working with Strings and Dates.
2836

29-
### Mix
37+
### Assets
3038

3139
The mix helper should be used to call in static assets compiled with Laravel Mix. For example:
3240

@@ -39,15 +47,15 @@ The mix helper should be used to call in static assets compiled with Laravel Mix
3947

4048
<title>BaseLayout NG</title>
4149

42-
<link rel="stylesheet" href="<?php echo mix('assets/css/global.css'); ?>">
50+
<link rel="stylesheet" href="<?php echo base_asset('assets/css/global.css'); ?>">
4351
</head>
4452

4553
<body>
4654
<!-- Page Content... -->
4755

48-
<script src="<?php echo mix('assets/js/manifest.js'); ?>"></script>
49-
<script src="<?php echo mix('assets/js/vendor.js'); ?>"></script>
50-
<script src="<?php echo mix('assets/js/app.js'); ?>"></script>
56+
<script src="<?php echo base_asset('assets/js/manifest.js'); ?>"></script>
57+
<script src="<?php echo base_asset('assets/js/vendor.js'); ?>"></script>
58+
<script src="<?php echo base_asset('assets/js/app.js'); ?>"></script>
5159
</body>
5260
</html>
5361
```
@@ -77,6 +85,30 @@ Variables can also be checked with:
7785
<?php base_layout_has('class'); ?>
7886
```
7987

88+
### Mock Data
89+
90+
The Faker library allows you to generate random content for a wide variety of patterns. For a full list see [the documention](https://github.com/fzaninotto/Faker#formatters).
91+
92+
```php
93+
$faker = base_faker_factory();
94+
95+
echo $faker->email;
96+
```
97+
98+
If you only need a single use you can shorthand the above to:
99+
100+
```php
101+
echo base_faker_factory()->email;
102+
```
103+
104+
### URLs
105+
106+
The fully qualified domain name can be returned using `base_http_host()`. The function will automatically add the correct protocol if SSL is enabled:
107+
108+
```php
109+
<link rel="icon" sizes="152x152" href="<?php echo base_http_host(); ?>/images/meta/touch-icon.png">
110+
```
111+
80112
**Perch**
81113

82114
Migrating a static site to Perch is simply a matter of replacing `base_` with `perch_` on each of the above functions. This is deliberate to reduce the time spent converting files to use Perch's template system.
@@ -108,4 +140,14 @@ if(Stringy::create('Tom Bradley ordered 6 Products')->contains('Tom Bradley')) {
108140
}
109141
```
110142

111-
View Documentation: [https://github.com/danielstjules/Stringy](https://github.com/danielstjules/Stringy)
143+
View Documentation: [https://github.com/danielstjules/Stringy](https://github.com/danielstjules/Stringy)
144+
145+
### Faker
146+
147+
Generates mock content for for a wide variety of patterns including names, addresses, emails, urls and even images.
148+
149+
```php
150+
$faker = Faker\Factory::create();
151+
echo $faker->sentence;
152+
```
153+
View Documentation: [https://github.com/fzaninotto/Faker](https://github.com/fzaninotto/Faker)

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
}
1111
],
1212
"require": {
13-
"php": ">=5.4.0",
13+
"php": ">=7.0.0",
1414
"nesbot/carbon": "^1.22",
1515
"danielstjules/stringy": "^3.0",
1616
"fzaninotto/faker": "^1.7"

helpers.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* @return string
1212
*/
1313
if (!function_exists('base_public_path')) {
14-
function base_public_path($path = '')
14+
function base_public_path(string $path = ''): string
1515
{
1616
$Base = new Base(realpath(__DIR__ . '/../../../'));
1717

@@ -31,7 +31,7 @@ function base_public_path($path = '')
3131
* @throws Exception
3232
*/
3333
if (!function_exists('base_asset')) {
34-
function base_asset($path, $manifestDirectory = '')
34+
function base_asset(string $path, string $manifestDirectory = ''): string
3535
{
3636
static $manifest;
3737

@@ -76,7 +76,7 @@ function base_asset($path, $manifestDirectory = '')
7676
* @throws Exception
7777
*/
7878
if (!function_exists('base_layout')) {
79-
function base_layout($file, array $data = [], $return = false)
79+
function base_layout(string $file, array $data = [], bool $return = false)
8080
{
8181
$Base = new Base(realpath(__DIR__ . '/../../../'));
8282

@@ -117,7 +117,7 @@ function base_layout($file, array $data = [], $return = false)
117117
* @return string
118118
*/
119119
if (!function_exists('base_layout_var')) {
120-
function base_layout_var($key, $return = false)
120+
function base_layout_var(string $key, bool $return = false)
121121
{
122122
$BaseLayout = BaseLayout::fetch();
123123

@@ -137,7 +137,7 @@ function base_layout_var($key, $return = false)
137137
* @return bool
138138
*/
139139
if (!function_exists('base_layout_has')) {
140-
function base_layout_has($key)
140+
function base_layout_has(string $key): bool
141141
{
142142
$BaseLayout = BaseLayout::fetch();
143143

@@ -171,7 +171,7 @@ function base_faker_factory()
171171
* @return string
172172
*/
173173
if (!function_exists('base_http_host')) {
174-
function base_http_host()
174+
function base_http_host(): string
175175
{
176176
return 'http' . ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') ? 's' : '') . '://' . $_SERVER['HTTP_HOST'];
177177
}

src/Base.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function __construct($basePath = null)
3434
*
3535
* @return $this
3636
*/
37-
public function setBasePath($basePath)
37+
public function setBasePath(string $basePath)
3838
{
3939
$this->basePath = rtrim($basePath, '\/');
4040

@@ -46,7 +46,7 @@ public function setBasePath($basePath)
4646
*
4747
* @return string
4848
*/
49-
public function getBasePath()
49+
public function getBasePath(): string
5050
{
5151
return $this->basePath;
5252
}
@@ -56,7 +56,7 @@ public function getBasePath()
5656
*
5757
* @return string
5858
*/
59-
public function getPublicPath()
59+
public function getPublicPath(): string
6060
{
6161
return $this->getBasePath() . DIRECTORY_SEPARATOR . 'public';
6262
}
@@ -66,7 +66,7 @@ public function getPublicPath()
6666
*
6767
* @return string
6868
*/
69-
public function getLayoutPath()
69+
public function getLayoutPath(): string
7070
{
7171
return $this->getPublicPath() . DIRECTORY_SEPARATOR . 'layouts';
7272
}

src/BaseLayout.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public function setLayoutVars(array $data)
6363
*
6464
* @return array
6565
*/
66-
public function getLayoutVars()
66+
public function getLayoutVars(): array
6767
{
6868
return $this->layoutVars;
6969
}
@@ -75,10 +75,10 @@ public function getLayoutVars()
7575
*
7676
* @return mixed|string
7777
*/
78-
public function getLayoutVar($key)
78+
public function getLayoutVar(string $key)
7979
{
80-
if (isset($this->layoutVars[$key])) {
81-
return $this->layoutVars[$key];
80+
if (isset($this->getLayoutVars()[$key])) {
81+
return $this->getLayoutVars()[$key];
8282
}
8383

8484
return '';

tests/HelpersTest.php

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,47 +7,30 @@ class HelpersTest extends TestCase
77
*/
88
public function public_path_returns_valid_path()
99
{
10-
$basePath = '/path/to/user/document/root';
11-
$publicPath = '/public';
12-
13-
$externalMock = Mockery::mock('overload:RootStudio\Base');
14-
$externalMock->shouldReceive('setBasePath')->with($basePath)->andReturnSelf();
15-
$externalMock->shouldReceive('getPublicPath')->andReturn($basePath . $publicPath);
16-
1710
$result = base_public_path();
1811

1912
$this->assertInternalType('string', $result);
20-
$this->assertEquals($basePath . $publicPath, $result);
13+
$this->assertEquals(__DIR__, $result);
2114
}
2215

2316
/**
2417
* @test
2518
*/
2619
public function public_path_can_be_appended_to()
2720
{
28-
$publicPath = '/public';
29-
$testDir = '/it/never/ends';
30-
31-
$externalMock = Mockery::mock('overload:RootStudio\Base');
32-
$externalMock->shouldReceive('getPublicPath')->andReturn($publicPath);
33-
34-
$result = base_public_path($testDir);
21+
$result = base_public_path('/my_directory');
3522

3623
$this->assertInternalType('string', $result);
37-
$this->assertEquals($publicPath . $testDir, $result);
24+
$this->assertEquals(__DIR__ . '/my_directory', $result);
3825
}
3926

4027
/**
4128
* @test
4229
*/
4330
public function it_will_output_versioned_asset_path()
4431
{
45-
$publicPath = __DIR__;
4632
$file = 'unversioned.css';
4733

48-
$externalMock = Mockery::mock('overload:RootStudio\Base');
49-
$externalMock->shouldReceive('getPublicPath')->andReturn($publicPath);
50-
5134
touch(base_public_path('mix-manifest.json'));
5235

5336
file_put_contents(base_public_path('mix-manifest.json'), json_encode([
@@ -100,6 +83,14 @@ public function it_will_output_ssl_protocol_on_http_host()
10083
$this->assertEquals('https://' . $host, $result);
10184
}
10285

86+
public function setUp()
87+
{
88+
$externalMock = Mockery::mock('overload:RootStudio\Base');
89+
$externalMock->shouldReceive('getPublicPath')->andReturn(__DIR__);
90+
91+
parent::setUp();
92+
}
93+
10394
public function tearDown()
10495
{
10596
Mockery::close();

0 commit comments

Comments
 (0)