Courses

Testing in Laravel 11: Advanced Level

Testing Strings: Expect toContain

Summary of this lesson:
- Learn string assertion techniques
- Use toContain() and toStartWith() in Pest
- Compare PHPUnit and Pest string testing approaches
- Explore real-world string assertion examples

Now, I will show a few assertions related to text data to check something within a string. We will look at the examples from open-source projects.


Pest and PHPUnit Syntax Differences

In this course, we're trying to cover both tools. String assertion is the (rare) case when the syntax is slightly different.

The assertStringStartsWith() and other similar string assertions are for PHPUnit. We will look at those examples later in the lesson.

With Pest, you would use expectations with equivalent methods. For example, an equivalent to the assertStringContainsString() method in Pest would be toContain().


Laravel.io Example: Look for a Phrase in Email

In the open-source example from a laravelio/laravel.io project, we can see...

The full lesson is only for Premium Members.
Want to access all 31 lessons of this course? (74 min read)

You also get:

  • 69 courses (majority in latest Laravel 11)
  • Premium tutorials
  • Access to repositories
  • Private Discord