Skip to content

Commit 9c94f27

Browse files
author
Ilya Sakovich
authored
Merge pull request #1 from klimov-paul/travis
Add unit tests and Travis support
2 parents 37d74ec + 3c199f9 commit 9c94f27

File tree

6 files changed

+132
-5
lines changed

6 files changed

+132
-5
lines changed

.gitattributes

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
/.gitignore export-ignore
33
/.styleci.yml export-ignore
44
/phpunit.xml.dist export-ignore
5-
/tests export-ignore
5+
/tests export-ignore
6+
/.travis.yml export-ignore

.travis.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
language: php
2+
3+
php:
4+
- 7.1
5+
- 7.2
6+
- 7.3
7+
8+
# faster builds on new travis setup not using sudo
9+
sudo: false
10+
11+
# cache vendor dirs
12+
cache:
13+
directories:
14+
- $HOME/.composer/cache
15+
16+
install:
17+
- travis_retry composer self-update && composer --version
18+
- export PATH="$HOME/.composer/vendor/bin:$PATH"
19+
- travis_retry composer install --prefer-dist --no-interaction
20+
21+
script:
22+
- phpunit $PHPUNIT_FLAGS

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
<p align="center"><b>Laravel Shared Data</b> provides an easy way to share the data from your backend to the JavaScript.</p>
44

5+
[![Latest Stable Version](https://img.shields.io/packagist/v/coderello/laravel-shared-data.svg)](https://packagist.org/packages/coderello/laravel-shared-data)
6+
[![Total Downloads](https://img.shields.io/packagist/dt/coderello/laravel-shared-data.svg)](https://packagist.org/packages/coderello/laravel-shared-data)
7+
[![Build Status](https://travis-ci.org/coderello/laravel-shared-data.svg?branch=master)](https://travis-ci.org/coderello/laravel-shared-data)
8+
59
## Installation
610

711
You can install this package via composer using this command:

src/SharedData.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ public function get($key = null)
3939
{
4040
if (is_null($key)) {
4141
return $this->data;
42-
} else {
43-
return Arr::get($this->data, $key);
4442
}
43+
44+
return Arr::get($this->data, $key);
4545
}
4646

4747
protected function getNamespace()

tests/AbstractTestCase.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
namespace Coderello\SharedData\Tests;
44

5+
use Orchestra\Testbench\TestCase;
56
use Coderello\SharedData\Providers\SharedDataServiceProvider;
6-
use Orchestra\Testbench\TestCase as OrchestraTestCase;
77

8-
abstract class AbstractTestCase extends OrchestraTestCase
8+
abstract class AbstractTestCase extends TestCase
99
{
1010
protected function getPackageProviders($app)
1111
{

tests/SharedDataTest.php

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
<?php
2+
3+
namespace Coderello\SharedData\Tests;
4+
5+
use Coderello\SharedData\SharedData;
6+
7+
class SharedDataTest extends AbstractTestCase
8+
{
9+
/**
10+
* @var \Coderello\SharedData\SharedData instance under test
11+
*/
12+
protected $sharedData;
13+
14+
/**
15+
* {@inheritdoc}
16+
*/
17+
protected function setUp(): void
18+
{
19+
parent::setUp();
20+
21+
$this->sharedData = $this->app->make(SharedData::class);
22+
}
23+
24+
public function testSetupData()
25+
{
26+
$this->sharedData->put('foo', 'bar');
27+
$this->assertSame('bar', $this->sharedData->get('foo'));
28+
29+
$this->sharedData->put([
30+
'scalar' => 'scalar-value',
31+
'array' => ['nested' => 'value'],
32+
]);
33+
$this->assertSame('scalar-value', $this->sharedData->get('scalar'));
34+
$this->assertSame(['nested' => 'value'], $this->sharedData->get('array'));
35+
36+
$object = new \stdClass;
37+
$object->objectScalar = 'object-scalar';
38+
$object->objectArray = ['nested' => 'object-scalar'];
39+
$this->sharedData->put($object);
40+
$this->assertSame('object-scalar', $this->sharedData->get('objectScalar'));
41+
$this->assertSame(['nested' => 'object-scalar'], $this->sharedData->get('objectArray'));
42+
}
43+
44+
/**
45+
* @depends testSetupData
46+
*/
47+
public function testGetAll()
48+
{
49+
$values = [
50+
'scalar' => 'scalar-value',
51+
'array' => ['nested' => 'value'],
52+
];
53+
54+
$this->sharedData->put($values);
55+
56+
$this->assertSame($values, $this->sharedData->get());
57+
}
58+
59+
/**
60+
* @depends testSetupData
61+
*/
62+
public function testToJson()
63+
{
64+
$this->sharedData->put([
65+
'scalar' => 'scalar-value',
66+
'array' => ['nested' => 'value'],
67+
]);
68+
69+
$json = $this->sharedData->toJson();
70+
$expectedJson = '{"scalar":"scalar-value","array":{"nested":"value"}}';
71+
72+
$this->assertSame($expectedJson, $json);
73+
}
74+
75+
/**
76+
* @depends testSetupData
77+
*/
78+
public function testRender()
79+
{
80+
$this->sharedData->put([
81+
'scalar' => 'scalar-value',
82+
'array' => ['nested' => 'value'],
83+
]);
84+
85+
$html = $this->sharedData->render();
86+
$expectedHtml = '<script>window[\'sharedData\'] = {"scalar":"scalar-value","array":{"nested":"value"}};</script>';
87+
88+
$this->assertSame($expectedHtml, $html);
89+
}
90+
91+
/**
92+
* @depends testRender
93+
*/
94+
public function testToString()
95+
{
96+
$this->sharedData->put('foo', 'bar');
97+
98+
$this->assertSame($this->sharedData->render(), (string) $this->sharedData);
99+
}
100+
}

0 commit comments

Comments
 (0)