Connor Mills

Connor Mills

1567067728

Laravel Scout Array Driver for Testing

Originally published at https://github.com

Installation

Install the package via Composer:

composer require sti3bas/laravel-scout-array-driver --dev

Set Scout driver to array in .env.testing file:

SCOUT_DRIVER=array 

or in phpunit.xml file:

<env name="SCOUT_DRIVER" value="array"/>

Usage

The Search facade provides the following methods and assertions:

assertContains($model, $callback = null)

Checks if model exists in the search index.

$user = factory(User::class)->create([
    'name' => 'Oliver',
]);

$user2 = User::withoutSyncingToSearch(function () {
return factory(User::class)->create([
‘name’ => ‘John’,
]);
});

Search::assertContains($user) // passes
->assertContains($user2) // fails
->assertContains($user, function ($record) { // passes
return $record[‘name’] === ‘Oliver’;
})
->assertContains($user, function ($record) { // fails
return $record[‘name’] === ‘John’;
})
->assertContains($user2, function ($record) { // fails
return $record[‘name’] === ‘John’;
});

assertNotContains($model, $callback = null)

Checks if model doesn’t exist in the search index.

$user = factory(User::class)->create([
‘name’ => ‘Oliver’,
]);

$user2 = User::withoutSyncingToSearch(function () {
return factory(User::class)->create([
‘name’ => ‘John’,
]);
});

Search::assertNotContains($user) // fails
->assertNotContains($user2) // passes
->assertNotContains($user, function ($record) { // fails
return $record[‘name’] === ‘Oliver’;
})
->assertNotContains($user, function ($record) { // passes
return $record[‘name’] === ‘John’;
})
->assertNotContains($user2, function ($record) { // passes
return $record[‘name’] === ‘John’;
});

assertContainsIn($index, $model, $callback = null)

Checks if model exists in custom search index.

$user = factory(User::class)->create([
‘name’ => ‘Oliver’,
]);

Search::assertContainsIn(‘users’, $user) // passes
->assertContainsIn(‘non_existing_index’, $user) // fails
->assertContainsIn(‘users’, $user, function ($record) { // passes
return $record[‘name’] === ‘Oliver’;
})
->assertContainsIn(‘users’, $user, function ($record) { // fails
return $record[‘name’] === ‘John’;
});

assertNotContainsIn($index, $model, $callback = null)

Checks if model doesn’t exist in custom search index.

$user = factory(User::class)->create([
‘name’ => ‘Oliver’,
]);

Search::assertNotContainsIn(‘users’, $user) // fails
->assertNotContainsIn(‘non_existing_index’, $user) // passes
->assertNotContainsIn(‘users’, $user, function ($record) { // fails
return $record[‘name’] === ‘Oliver’;
})
->assertNotContainsIn(‘users’, $user, function ($record) { // passes
return $record[‘name’] === ‘John’;
});

assertEmpty()

Checks if all search indexes are empty.

Search::assertEmpty(); // passes

factory(User::class)->create();

Search::assertEmpty(); // fails

assertEmptyIn($index)

Checks if search index is empty.

Search::assertEmptyIn(‘users’); // passes

factory(User::class)->create();

Search::assertEmptyIn(‘users’); // fails

assertNotEmpty()

Checks if there is at least one record in any of search indexes.

Search::assertNotEmpty(); // fails

factory(User::class)->create();

Search::assertNotEmpty(); // passes

assertNotEmptyIn($index)

Checks if search index is not empty.

Search::assertNotEmptyIn(‘users’); // fails

factory(User::class)->create();

Search::assertNotEmptyIn(‘users’); // passes

assertSynced($model, $callback = null)

Checks if model was synced to search index. This assertion checks every record of the given model which was synced during the request.

$user = factory(User::class)->create([
‘name’ => ‘Peter’,
]);

Search::assertSynced($user); // passes

$user->update([‘name’ => ‘John’]);
$user->delete();

Search::assertContains($user) // fails
->assertSynced($user) // passes
->assertSynced($user, function ($record) { // passes
return $record[‘name’] === ‘Peter’;
})
->assertSynced($user, function ($record) { // passes
return $record[‘name’] === ‘John’;
})
->assertSynced($user, function ($record) { // fails
return $record[‘name’] === ‘Oliver’;
});

assertNotSynced($model, $callback = null)

Checks if model wasn’t synced to search index. This assertion checks every record of the given model which was synced during the request.

$user = factory(User::class)->create([
‘name’ => ‘Peter’,
]);

Search::assertNotSynced($user); // fails

$user->update([‘name’ => ‘John’]);
$user->delete();

Search::assertContains($user) // fails
->assertNotSynced($user); // fails

Search::assertNotSynced($user, function ($record) { // fails
return $record[‘name’] === ‘Peter’;
})
->assertNotSynced($user, function ($record) { // fails
return $record[‘name’] === ‘John’;
})
->assertNotSynced($user, function ($record) { // passes
return $record[‘name’] === ‘Oliver’;
});

assertSyncedTo($model, $callback = null)

Checks if model was synced to custom search index. This assertion checks every record of the given model which was synced during the request.

$user = factory(User::class)->create([
‘name’ => ‘Peter’,
]);

Search::assertSyncedTo(‘users’, $user); // passes

$user->update([‘name’ => ‘John’]);
$user->delete();

Search::assertContains($user) // fails
->assertSyncedTo(‘users’, $user) // passes
->assertSyncedTo(‘users’, $user, function ($record) {
return $record[‘name’] === ‘Peter’; // passes
})
->assertSyncedTo(‘users’, $user, function ($record) {
return $record[‘name’] === ‘John’; // passes
})
->assertSyncedTo(‘non_existing_index’, $user, function ($record) {
return $record[‘name’] === ‘John’; // fails
});

assertNotSyncedTo($model, $callback = null)

Checks if model wasn’t synced to custom search index. This assertion checks every record of the given model which was synced during the request.

$user = factory(User::class)->create([
‘name’ => ‘Peter’,
]);

Search::assertNotSyncedTo(‘users’, $user) // fails
->assertNotSyncedTo(‘not_existing_index’, $user); // passes

$user->update([‘name’ => ‘John’]);
$user->delete();

Search::assertContains($user) // fails
->assertNotSyncedTo(‘users’, $user) // fails
->assertNotSyncedTo(‘users’, $user, function ($record) {
return $record[‘name’] === ‘Peter’; // fails
})
->assertNotSyncedTo(‘users’, $user, function ($record) {
return $record[‘name’] === ‘Oliver’; // passes
});

assertSyncedTimes($model, $callback = null)

Checks if model was synced expected number of times. This assertion checks every record of the given model which was synced during the request.

$user = User::withoutSyncingToSearch(function () {
return factory(User::class)->create([
‘name’ => ‘Peter’,
]);
});

Search::assertSyncedTimes($user, 0) // passes
->assertSyncedTimes($user, 1); // fails

$user->searchable();
$user->update([‘name’ => ‘John’]);
$user->delete();

Search::assertContains($user) // fails
->assertSyncedTimes($user, 2) // passes
->assertSyncedTimes($user, 1, function ($record) {
return $record[‘name’] === ‘Peter’; // passes
})
->assertSyncedTimes($user, 1, function ($record) {
return $record[‘name’] === ‘John’; // passes
})
->assertSyncedTimes($user, 1, function ($record) {
return $record[‘name’] === ‘Oliver’; // fails
});

assertSyncedTimesTo($index, $model, $callback = null)

Checks if model was synced to custom search index expected number of times. This assertion checks every record of the given model which was synced during the request.

$user = User::withoutSyncingToSearch(function () {
return factory(User::class)->create([
‘name’ => ‘Peter’,
]);
});

Search::assertSyncedTimesTo(‘users’, $user, 0) // passes
->assertSyncedTimesTo(‘non_existing_index’, $user, 1); // fails

$user->searchable();
$user->update([‘name’ => ‘John’]);
$user->delete();

Search::assertContains($user) // fails
->assertSyncedTimesTo(‘users’, $user, 2) // passes
->assertSyncedTimesTo(‘users’, $user, 1, function ($record) {
return $record[‘name’] === ‘Peter’; // passes
})
->assertSyncedTimesTo(‘non_existing_index’, 1, function ($record) {
return $record[‘name’] === ‘John’; // fails
});

assertNothingSynced()

Checks if nothing was synced to any of search indexes. This assertion checks every record which was synced during the request.

Search::assertNothingSynced(); // passes

factory(User::class)->create();

Search::assertNothingSynced(); // fails

assertNothingSyncedTo()

Checks if nothing was synced to custom search index. This assertion checks every record which was synced during the request.

Search::assertNothingSyncedTo(‘users’); // passes

factory(User::class)->create();

Search::assertNothingSyncedTo(‘users’); // fails

fakeRecord($model, $data, $merge = true, $index = null)

This method allows to fake search index record of the model. It will not affect assertions.

$user = factory(User::class)->create([
‘id’ => 123,
‘name’ => ‘Peter’,
‘email’ => ‘peter@example.com’,
]);

Search::fakeRecord($user, [
‘name’ => ‘John’,
]);

$record = User::search()->where(‘id’, 123)->raw()[‘hits’][0];

$this->assertEquals(‘Peter’, $record[‘name’]); // fails
$this->assertEquals(‘John’, $record[‘name’]); // passes
$this->assertEquals(‘peter@example.com’, $record[‘email’]); // passes

Search::fakeRecord($user, [
‘id’ => 123,
‘name’ => ‘John’,
], false);

$record = User::search()->where(‘id’, 123)->raw()[‘hits’][0];

$this->assertEquals(‘Peter’, $record[‘name’]); // fails
$this->assertEquals(‘John’, $record[‘name’]); // passes
$this->assertTrue(!isset($record[‘email’])); // passes

Thanks for reading

If you liked this post, share it with all of your programming buddies!

Follow me on Facebook | Twitter

Further reading

PHP with Laravel for beginners - Become a Master in Laravel

Projects in Laravel: Learn Laravel Building 10 Projects

Laravel for RESTful: Build Your RESTful API with Laravel

Fullstack Web Development With Laravel and Vue.js

Laravel 5.8 Tutorial for Beginners

Laravel 5.8 Ajax CRUD tutorial using Datatable JS

Laravel 5.8 Tutorial from Scratch for Beginners

Build RESTful API In Laravel 5.8 Example

Login with Google in Laravel 5.8 App using Socialite Package

Upgrading Laravel To 6.0 From 5.8

Laravel 6 Release New Features and Upgrade

#laravel #php #testing #web-development

What is GEEK

Buddha Community

Laravel Scout Array Driver for Testing
Seamus  Quitzon

Seamus Quitzon

1595201363

Php how to delete multiple rows through checkbox using ajax in laravel

First thing, we will need a table and i am creating products table for this example. So run the following query to create table.

CREATE TABLE `products` (
 `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
 `name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
 `description` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
 `created_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
 `updated_at` datetime DEFAULT NULL,
 PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci

Next, we will need to insert some dummy records in this table that will be deleted.

INSERT INTO `products` (`name`, `description`) VALUES

('Test product 1', 'Product description example1'),

('Test product 2', 'Product description example2'),

('Test product 3', 'Product description example3'),

('Test product 4', 'Product description example4'),

('Test product 5', 'Product description example5');

Now we are redy to create a model corresponding to this products table. Here we will create Product model. So let’s create a model file Product.php file under app directory and put the code below.

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Product extends Model
{
    protected $fillable = [
        'name','description'
    ];
}

Step 2: Create Route

Now, in this second step we will create some routes to handle the request for this example. So opeen routes/web.php file and copy the routes as given below.

routes/web.php

Route::get('product', 'ProductController@index');
Route::delete('product/{id}', ['as'=>'product.destroy','uses'=>'ProductController@destroy']);
Route::delete('delete-multiple-product', ['as'=>'product.multiple-delete','uses'=>'ProductController@deleteMultiple']);

#laravel #delete multiple rows in laravel using ajax #laravel ajax delete #laravel ajax multiple checkbox delete #laravel delete multiple rows #laravel delete records using ajax #laravel multiple checkbox delete rows #laravel multiple delete

Tamia  Walter

Tamia Walter

1596754901

Testing Microservices Applications

The shift towards microservices and modular applications makes testing more important and more challenging at the same time. You have to make sure that the microservices running in containers perform well and as intended, but you can no longer rely on conventional testing strategies to get the job done.

This is where new testing approaches are needed. Testing your microservices applications require the right approach, a suitable set of tools, and immense attention to details. This article will guide you through the process of testing your microservices and talk about the challenges you will have to overcome along the way. Let’s get started, shall we?

A Brave New World

Traditionally, testing a monolith application meant configuring a test environment and setting up all of the application components in a way that matched the production environment. It took time to set up the testing environment, and there were a lot of complexities around the process.

Testing also requires the application to run in full. It is not possible to test monolith apps on a per-component basis, mainly because there is usually a base code that ties everything together, and the app is designed to run as a complete app to work properly.

Microservices running in containers offer one particular advantage: universal compatibility. You don’t have to match the testing environment with the deployment architecture exactly, and you can get away with testing individual components rather than the full app in some situations.

Of course, you will have to embrace the new cloud-native approach across the pipeline. Rather than creating critical dependencies between microservices, you need to treat each one as a semi-independent module.

The only monolith or centralized portion of the application is the database, but this too is an easy challenge to overcome. As long as you have a persistent database running on your test environment, you can perform tests at any time.

Keep in mind that there are additional things to focus on when testing microservices.

  • Microservices rely on network communications to talk to each other, so network reliability and requirements must be part of the testing.
  • Automation and infrastructure elements are now added as codes, and you have to make sure that they also run properly when microservices are pushed through the pipeline
  • While containerization is universal, you still have to pay attention to specific dependencies and create a testing strategy that allows for those dependencies to be included

Test containers are the method of choice for many developers. Unlike monolith apps, which lets you use stubs and mocks for testing, microservices need to be tested in test containers. Many CI/CD pipelines actually integrate production microservices as part of the testing process.

Contract Testing as an Approach

As mentioned before, there are many ways to test microservices effectively, but the one approach that developers now use reliably is contract testing. Loosely coupled microservices can be tested in an effective and efficient way using contract testing, mainly because this testing approach focuses on contracts; in other words, it focuses on how components or microservices communicate with each other.

Syntax and semantics construct how components communicate with each other. By defining syntax and semantics in a standardized way and testing microservices based on their ability to generate the right message formats and meet behavioral expectations, you can rest assured knowing that the microservices will behave as intended when deployed.

Ways to Test Microservices

It is easy to fall into the trap of making testing microservices complicated, but there are ways to avoid this problem. Testing microservices doesn’t have to be complicated at all when you have the right strategy in place.

There are several ways to test microservices too, including:

  • Unit testing: Which allows developers to test microservices in a granular way. It doesn’t limit testing to individual microservices, but rather allows developers to take a more granular approach such as testing individual features or runtimes.
  • Integration testing: Which handles the testing of microservices in an interactive way. Microservices still need to work with each other when they are deployed, and integration testing is a key process in making sure that they do.
  • End-to-end testing: Which⁠—as the name suggests⁠—tests microservices as a complete app. This type of testing enables the testing of features, UI, communications, and other components that construct the app.

What’s important to note is the fact that these testing approaches allow for asynchronous testing. After all, asynchronous development is what makes developing microservices very appealing in the first place. By allowing for asynchronous testing, you can also make sure that components or microservices can be updated independently to one another.

#blog #microservices #testing #caylent #contract testing #end-to-end testing #hoverfly #integration testing #microservices #microservices architecture #pact #testing #unit testing #vagrant #vcr

Software Testing 101: Regression Tests, Unit Tests, Integration Tests

Automation and segregation can help you build better software
If you write automated tests and deliver them to the customer, he can make sure the software is working properly. And, at the end of the day, he paid for it.

Ok. We can segregate or separate the tests according to some criteria. For example, “white box” tests are used to measure the internal quality of the software, in addition to the expected results. They are very useful to know the percentage of lines of code executed, the cyclomatic complexity and several other software metrics. Unit tests are white box tests.

#testing #software testing #regression tests #unit tests #integration tests

Dejah  Reinger

Dejah Reinger

1599859380

How to Do API Testing?

Nowadays API testing is an integral part of testing. There are a lot of tools like postman, insomnia, etc. There are many articles that ask what is API, What is API testing, but the problem is How to do API testing? What I need to validate.

Note: In this article, I am going to use postman assertions for all the examples since it is the most popular tool. But this article is not intended only for the postman tool.

Let’s directly jump to the topic.

Let’s consider you have an API endpoint example http://dzone.com/getuserDetails/{{username}} when you send the get request to that URL it returns the JSON response.

My API endpoint is http://dzone.com/getuserDetails/{{username}}

The response is in JSON format like below

JSON

{
  "jobTitle": "string",
  "userid": "string",
  "phoneNumber": "string",
  "password": "string",
  "email": "user@example.com",
  "firstName": "string",
  "lastName": "string",
  "userName": "string",
  "country": "string",
  "region": "string",
  "city": "string",
  "department": "string",
  "userType": 0
}

In the JSON we can see there are properties and associated values.

Now, For example, if we need details of the user with the username ‘ganeshhegde’ we need to send a **GET **request to **http://dzone.com/getuserDetails/ganeshhegde **

dzone.com

Now there are two scenarios.

1. Valid Usecase: User is available in the database and it returns user details with status code 200

2. Invalid Usecase: User is Unavailable/Invalid user in this case it returns status with code 404 with not found message.

#tutorial #performance #api #test automation #api testing #testing and qa #application programming interface #testing as a service #testing tutorial #api test

Aurelie  Block

Aurelie Block

1598916060

Top 10 Automation Testing Tools: 2020 Edition

The demand for delivering quality software faster — or “Quality at Speed” — requires organizations to search for solutions in Agile, continuous integration (CI), and DevOps methodologies. Test automation is an essential part of these aspects. The latest World Quality Report 2018–2019 suggests that test automation is the biggest bottleneck to deliver “Quality at Speed,” as it is an enabler of successful Agile and DevOps adoption.

Test automation cannot be realized without good tools; as they determine how automation is performed and whether the benefits of automation can be delivered. Test automation tools is a crucial component in the DevOps toolchain. The current test automation trends have increased in applying artificial intelligence and machine learning (AI/ML) to offer advanced capabilities for test optimization, intelligent test generation, execution, and reporting. It will be worthwhile to understand which tools are best poised to take advantage of these trends.****

#automation-testing #automation-testing-tools #testing #testing-tools #selenium #open-source #test-automation #automated-testing