Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 3e4b49f

Browse files
Add tests for service container methods
1 parent a9bbd20 commit 3e4b49f

File tree

10 files changed

+159
-27
lines changed

10 files changed

+159
-27
lines changed

‎app/Http/Controllers/HomeController.php

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -102,43 +102,50 @@ public function fireEvent()
102102
public function validation(Request $request)
103103
{
104104
$this->validate($request, [
105-
'post_id' => 'required|exists:posts,id',
106-
'postal_code' => 'required|postal_code'
105+
'post_id' => 'required|exists:posts,id',
106+
'postal_code' => 'required|postal_code'
107107
]);
108108

109109
return 'Validation success';
110110
}
111111

112-
/**
113-
* @return string
114-
*/
115-
public function domain()
116-
{
117-
return 'Domain route';
118-
}
112+
/**
113+
* @return string
114+
*/
115+
public function domain()
116+
{
117+
return 'Domain route';
118+
}
119119

120-
/**
121-
* @return string
120+
/**
121+
* @return string
122122
*/
123-
public function subdomain()
124-
{
125-
return 'Subdomain route';
126-
}
123+
public function subdomain()
124+
{
125+
return 'Subdomain route';
126+
}
127127

128-
/**
129-
* @return string
128+
/**
129+
* @return string
130130
*/
131-
public function wildcard()
132-
{
133-
return 'Wildcard route';
134-
}
131+
public function wildcard()
132+
{
133+
return 'Wildcard route';
134+
}
135135

136-
/**
137-
* @return string
136+
/**
137+
* @return string
138138
*/
139-
public function multipleWildcards()
140-
{
141-
return 'Multiple wildcards route';
142-
}
139+
public function multipleWildcards()
140+
{
141+
return 'Multiple wildcards route';
142+
}
143143

144+
/**
145+
* @return \Illuminate\View\View
146+
*/
147+
public function serviceContainer()
148+
{
149+
return view('service-container');
150+
}
144151
}

‎app/Http/routes.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
Route::get('special-characters', 'HomeController@specialCharacters');
2626
Route::get('fire-event', 'HomeController@fireEvent');
2727
Route::get('validation', 'HomeController@validation');
28+
Route::get('service-container', 'HomeController@serviceContainer');
2829
Route::match(['get', 'post'], 'form', 'HomeController@form');
2930

3031
Route::resource('posts', 'PostsController');

‎app/Providers/AppServiceProvider.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ public function register()
3434
'Illuminate\Contracts\Auth\Registrar',
3535
'App\Services\Registrar'
3636
);
37+
38+
$this->app->bind('App\Test\StringConverter', 'App\Test\ToLowercase');
39+
$this->app->addContextualBinding('App\Test\Repeat', '$times', 2);
3740
}
3841

3942
}

‎app/Test/Repeat.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace App\Test;
4+
5+
class Repeat implements StringConverter
6+
{
7+
private $times;
8+
9+
/**
10+
* Repeat constructor.
11+
* @param $times
12+
*/
13+
public function __construct($times)
14+
{
15+
$this->times = $times;
16+
}
17+
18+
/**
19+
* @param $string
20+
* @return string
21+
*/
22+
public function convert($string)
23+
{
24+
return str_repeat($string, $this->times);
25+
}
26+
}

‎app/Test/StringConverter.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace App\Test;
4+
5+
interface StringConverter
6+
{
7+
/**
8+
* @param $string
9+
* @return string
10+
*/
11+
public function convert($string);
12+
}

‎app/Test/ToLowercase.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace App\Test;
4+
5+
class ToLowercase implements StringConverter
6+
{
7+
/**
8+
* @param $string
9+
* @return string
10+
*/
11+
public function convert($string)
12+
{
13+
return strtolower($string);
14+
}
15+
}

‎app/Test/ToUppercase.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace App\Test;
4+
5+
class ToUppercase implements StringConverter
6+
{
7+
/**
8+
* @param $string
9+
* @return string
10+
*/
11+
public function convert($string)
12+
{
13+
return strtoupper($string);
14+
}
15+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
@inject('converter', 'App\Test\StringConverter')
2+
3+
@extends('layouts.scaffold')
4+
5+
@section('main')
6+
<p class="converted">{{ $converter->convert('String To Convert') }}</p>
7+
@stop
File renamed without changes.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
class ServiceContainerCest
4+
{
5+
6+
public function _before(FunctionalTester $I)
7+
{
8+
$I->amOnPage('service-container');
9+
$I->seeElement("//p[text()='string to convert']");
10+
}
11+
12+
public function testHaveBinding(FunctionalTester $I)
13+
{
14+
$I->haveBinding('App\Test\StringConverter', 'App\Test\ToUppercase');
15+
16+
$I->amOnPage('service-container');
17+
$I->seeElement("//p[text()='STRING TO CONVERT']");
18+
}
19+
20+
public function testHaveSingleton(FunctionalTester $I)
21+
{
22+
$I->haveSingleton('App\Test\StringConverter', 'App\Test\ToUppercase');
23+
24+
$I->amOnPage('service-container');
25+
$I->seeElement("//p[text()='STRING TO CONVERT']");
26+
}
27+
28+
public function testHaveInstance(FunctionalTester $I)
29+
{
30+
$converter = new \App\Test\ToUppercase();
31+
$I->haveInstance('App\Test\StringConverter', $converter);
32+
33+
$I->amOnPage('service-container');
34+
$I->seeElement("//p[text()='STRING TO CONVERT']");
35+
}
36+
37+
public function testHaveContextualBinding(FunctionalTester $I)
38+
{
39+
$I->haveContextualBinding('App\Test\Repeat', '$times', 3);
40+
$I->haveBinding('App\Test\StringConverter', 'App\Test\Repeat');
41+
42+
$I->amOnPage('service-container');
43+
$I->seeElement("//p[text()='String To ConvertString To ConvertString To Convert']");
44+
}
45+
46+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /