Skip to content

Commit f945e59

Browse files
committedMar 19, 2021
Post tests, readme update
1 parent 4ab166e commit f945e59

File tree

3 files changed

+182
-50
lines changed

3 files changed

+182
-50
lines changed
 

‎README.md

+35-49
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,48 @@
11
<p align="center"><a href="https://laravel.com" target="_blank"><img src="https://raw.githubusercontent.com/laravel/art/master/logo-lockup/5%20SVG/2%20CMYK/1%20Full%20Color/laravel-logolockup-cmyk-red.svg" width="400"></a></p>
22

3-
<p align="center">
4-
<a href="https://travis-ci.org/laravel/framework"><img src="https://travis-ci.org/laravel/framework.svg" alt="Build Status"></a>
5-
<a href="https://packagist.org/packages/laravel/framework"><img src="https://img.shields.io/packagist/dt/laravel/framework" alt="Total Downloads"></a>
6-
<a href="https://packagist.org/packages/laravel/framework"><img src="https://img.shields.io/packagist/v/laravel/framework" alt="Latest Stable Version"></a>
7-
<a href="https://packagist.org/packages/laravel/framework"><img src="https://img.shields.io/packagist/l/laravel/framework" alt="License"></a>
8-
</p>
3+
## Requirements
4+
- PHP 7.3^|8.0^
5+
- [Composer](https://getcomposer.org/)
96

10-
## About Laravel
7+
## Installation
118

12-
Laravel is a web application framework with expressive, elegant syntax. We believe development must be an enjoyable and creative experience to be truly fulfilling. Laravel takes the pain out of development by easing common tasks used in many web projects, such as:
9+
- Clone the repo
1310

14-
- [Simple, fast routing engine](https://laravel.com/docs/routing).
15-
- [Powerful dependency injection container](https://laravel.com/docs/container).
16-
- Multiple back-ends for [session](https://laravel.com/docs/session) and [cache](https://laravel.com/docs/cache) storage.
17-
- Expressive, intuitive [database ORM](https://laravel.com/docs/eloquent).
18-
- Database agnostic [schema migrations](https://laravel.com/docs/migrations).
19-
- [Robust background job processing](https://laravel.com/docs/queues).
20-
- [Real-time event broadcasting](https://laravel.com/docs/broadcasting).
11+
```
12+
git clone https://github.com/msbodetti/the-blog.git
13+
```
2114

22-
Laravel is accessible, powerful, and provides tools required for large, robust applications.
15+
- Install dependencies and run packages
16+
```
17+
cd the-blog
18+
composer install
19+
npm install && npm run dev
20+
```
2321

24-
## Learning Laravel
22+
- Copy .env.sample and rename to .env and update env database variables
23+
```dotenv
24+
DB_CONNECTION=mysql
25+
DB_HOST=127.0.0.1
26+
DB_PORT=3310
27+
DB_DATABASE=the-blog
28+
DB_USERNAME=root
29+
DB_PASSWORD=root
30+
```
2531

26-
Laravel has the most extensive and thorough [documentation](https://laravel.com/docs) and video tutorial library of all modern web application frameworks, making it a breeze to get started with the framework.
32+
- Run migrations
33+
```
34+
php artisan migrate
35+
```
2736

28-
If you don't feel like reading, [Laracasts](https://laracasts.com) can help. Laracasts contains over 1500 video tutorials on a range of topics including Laravel, modern PHP, unit testing, and JavaScript. Boost your skills by digging into our comprehensive video library.
37+
- Run tests
38+
```
39+
php artisan test
40+
```
2941

30-
## Laravel Sponsors
42+
You can now register an account & login :)
3143

32-
We would like to extend our thanks to the following sponsors for funding Laravel development. If you are interested in becoming a sponsor, please visit the Laravel [Patreon page](https://patreon.com/taylorotwell).
44+
<img src="https://p30.f1.n0.cdn.getcloudapp.com/items/8LukR9zZ/0f19e66c-fccf-4d81-a92d-a60bf37c2ea8.gif?source=viewer&v=6f13b2cf1ea2cb11a92dbc371decf664" />
3345

34-
### Premium Partners
46+
You can also start adding/editing/deleting posts :)
3547

36-
- **[Vehikl](https://vehikl.com/)**
37-
- **[Tighten Co.](https://tighten.co)**
38-
- **[Kirschbaum Development Group](https://kirschbaumdevelopment.com)**
39-
- **[64 Robots](https://64robots.com)**
40-
- **[Cubet Techno Labs](https://cubettech.com)**
41-
- **[Cyber-Duck](https://cyber-duck.co.uk)**
42-
- **[Many](https://www.many.co.uk)**
43-
- **[Webdock, Fast VPS Hosting](https://www.webdock.io/en)**
44-
- **[DevSquad](https://devsquad.com)**
45-
- **[Curotec](https://www.curotec.com/)**
46-
- **[OP.GG](https://op.gg)**
47-
48-
## Contributing
49-
50-
Thank you for considering contributing to the Laravel framework! The contribution guide can be found in the [Laravel documentation](https://laravel.com/docs/contributions).
51-
52-
## Code of Conduct
53-
54-
In order to ensure that the Laravel community is welcoming to all, please review and abide by the [Code of Conduct](https://laravel.com/docs/contributions#code-of-conduct).
55-
56-
## Security Vulnerabilities
57-
58-
If you discover a security vulnerability within Laravel, please send an e-mail to Taylor Otwell via [taylor@laravel.com](mailto:taylor@laravel.com). All security vulnerabilities will be promptly addressed.
59-
60-
## License
61-
62-
The Laravel framework is open-sourced software licensed under the [MIT license](https://opensource.org/licenses/MIT).
48+
<img src="https://p30.f1.n0.cdn.getcloudapp.com/items/xQunGjXx/a24f909a-e653-46fe-8798-8e98c307d415.gif?source=viewer&v=701c9bf91774db785e8e53d8108cb997" />

‎database/factories/PostsFactory.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
namespace Database\Factories;
44

55
use App\Models\Posts;
6+
use App\Models\User;
67
use Illuminate\Database\Eloquent\Factories\Factory;
8+
use Illuminate\Support\Str;
79

810
class PostsFactory extends Factory
911
{
@@ -21,8 +23,11 @@ class PostsFactory extends Factory
2123
*/
2224
public function definition()
2325
{
26+
$user = User::factory()->create();
2427
return [
25-
//
28+
'title' => $this->faker->title,
29+
'content' => $this->faker->randomHtml(2,3) ,
30+
'user_id' => $user->id
2631
];
2732
}
2833
}

‎tests/Feature/PostTest.php

+141
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
<?php
2+
3+
namespace Tests\Feature;
4+
5+
use App\Models\Posts;
6+
use App\Models\User;
7+
use Illuminate\Foundation\Testing\RefreshDatabase;
8+
use Illuminate\Foundation\Testing\WithFaker;
9+
use Illuminate\Support\Facades\Auth;
10+
use Tests\TestCase;
11+
12+
class PostTest extends TestCase
13+
{
14+
use RefreshDatabase;
15+
16+
public function test_home_screen_can_be_rendered()
17+
{
18+
$response = $this->get('/');
19+
$response->assertViewHas('posts');
20+
$response->assertStatus(200);
21+
}
22+
23+
public function test_home_screen_can_be_rendered_with_posts()
24+
{
25+
$post = Posts::factory(5)->create();
26+
$response = $this->get('/');
27+
$response->assertViewHas('posts');
28+
$response->assertSeeTextInOrder([
29+
date('j F Y', strtotime($post[0]->created_at)),
30+
$post[0]->title,
31+
strlen($post[0]->content) > 100 ? substr(strip_tags($post[0]->content), 0, 100) . '...' : strip_tags($post->content),
32+
'Read more',
33+
$post[0]->user->name
34+
]);
35+
$response->assertStatus(200);
36+
}
37+
38+
public function test_create_post_screen_can_be_rendered()
39+
{
40+
$user = User::factory()->create();
41+
42+
$response = $this->actingAs($user)->get('/posts/create');
43+
44+
$response->assertStatus(200);
45+
}
46+
47+
public function test_user_can_create_post()
48+
{
49+
$user = User::factory()->create();
50+
51+
$response = $this->actingAs($user)->post('/posts', [
52+
'title' => 'Test title',
53+
'content' => '<h1>Testing this out</h1>'
54+
]);
55+
56+
$response->assertRedirect('/posts/6');
57+
58+
$this->assertDatabaseHas('posts', [
59+
'title' => 'Test title',
60+
'content' => '<h1>Testing this out</h1>'
61+
]);
62+
}
63+
64+
public function test_create_post_title_validation()
65+
{
66+
$user = User::factory()->create();
67+
68+
$response = $this->actingAs($user)->post('/posts', [
69+
'title' => null,
70+
'content' => '<h1>Testing this out</h1>'
71+
]);
72+
73+
$response->assertSessionHasErrors(['title']);
74+
}
75+
76+
public function test_create_post_content_validation()
77+
{
78+
$user = User::factory()->create();
79+
80+
$response = $this->actingAs($user)->post('/posts', [
81+
'title' => 'Test title',
82+
'content' => null
83+
]);
84+
85+
$response->assertSessionHasErrors(['content']);
86+
}
87+
88+
public function test_edit_post_screen_can_be_rendered()
89+
{
90+
$post = Posts::factory()->create();
91+
$user = User::factory()->create();
92+
93+
$response = $this->actingAs($user)->get('/posts/' . $post->id . '/edit');
94+
95+
$response->assertStatus(200);
96+
}
97+
98+
public function test_user_can_edit_post()
99+
{
100+
$post = Posts::factory()->create();
101+
$user = User::factory()->create();
102+
103+
$response = $this->actingAs($user)->put('/posts/' . $post->id, [
104+
'title' => 'Changing Post',
105+
'content' => '<h1>Changing the content for post</h1>'
106+
]);
107+
108+
$response->assertRedirect('/posts/' . $post->id);
109+
110+
$this->assertDatabaseHas('posts', [
111+
'title' => 'Changing Post',
112+
'content' => '<h1>Changing the content for post</h1>'
113+
]);
114+
}
115+
116+
public function test_edit_post_title_validation()
117+
{
118+
$post = Posts::factory()->create();
119+
$user = User::factory()->create();
120+
121+
$response = $this->actingAs($user)->put('/posts/' . $post->id, [
122+
'title' => null,
123+
'content' => '<h1>Testing this out</h1>'
124+
]);
125+
126+
$response->assertSessionHasErrors(['title']);
127+
}
128+
129+
public function test_edit_post_content_validation()
130+
{
131+
$post = Posts::factory()->create();
132+
$user = User::factory()->create();
133+
134+
$response = $this->actingAs($user)->put('/posts/' . $post->id, [
135+
'title' => 'Test title',
136+
'content' => null
137+
]);
138+
139+
$response->assertSessionHasErrors(['content']);
140+
}
141+
}

0 commit comments

Comments
 (0)
Please sign in to comment.