Skip to content

Commit

Permalink
Move the Solarium test class to its own file
Browse files Browse the repository at this point in the history
  • Loading branch information
danielbachhuber committed May 22, 2019
1 parent 65373b7 commit ee80e68
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 18 deletions.
3 changes: 2 additions & 1 deletion tests/phpunit/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ function _manually_load_plugin() {
tests_add_filter( 'muplugins_loaded', '_manually_load_plugin' );

require $_tests_dir . '/includes/bootstrap.php';
require 'class-solr-test-base.php';
require 'class-solr-test-base.php';
require 'class-mock-solarium-client-with-error.php';
9 changes: 9 additions & 0 deletions tests/phpunit/class-mock-solarium-client-with-error.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

class Mock_Solarium_Client_With_Error extends Solarium\Client {
public static $error_msg = "";

public function update(Solarium\Core\Query\QueryInterface $update, $endpoint = null) {
throw new Exception(self::$error_msg);
}
}
22 changes: 5 additions & 17 deletions tests/phpunit/test-batch-index.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,18 +140,17 @@ public function test_batch_index_restart_after_delete_all() {
}

public function mock_solarium_client($solr) {
return new MockSolariumClient($solr->getOptions());
return new Mock_Solarium_Client_With_Error($solr->getOptions());
}

public function test_returns_contents_of_h1_tags_if_present_in_error() {
// can i create a new class, return it via s4wp_solr, and throw an exception in __construct ?
add_filter( 's4wp_solr', array( $this, 'mock_solarium_client' ), 1, 1 );

$this->__create_multiple( 1 );
$batch_index = new SolrPower_Batch_Index( array( 'posts_per_page' => 2 ) );
$batch_index->have_posts();

MockSolariumClient::$error_msg = "<h1>danger will robinson</h1>";
Mock_Solarium_Client_With_Error::$error_msg = "<h1>danger will robinson</h1>";
$result = $batch_index->index_post();

$this->assertEquals( "danger will robinson", $result['message'] );
Expand All @@ -164,7 +163,7 @@ public function test_returns_contents_of_title_tags_if_present_in_error() {
$batch_index = new SolrPower_Batch_Index( array( 'posts_per_page' => 2 ) );
$batch_index->have_posts();

MockSolariumClient::$error_msg = "<title>vague error</title>";
Mock_Solarium_Client_With_Error::$error_msg = "<title>vague error</title>";
$result = $batch_index->index_post();

$this->assertEquals( "vague error", $result['message'] );
Expand All @@ -177,7 +176,7 @@ public function test_h1_takes_precedence_over_title_in_error() {
$batch_index = new SolrPower_Batch_Index( array( 'posts_per_page' => 2 ) );
$batch_index->have_posts();

MockSolariumClient::$error_msg = "<title>vague error</title><h1>specific error</h1>";
Mock_Solarium_Client_With_Error::$error_msg = "<title>vague error</title><h1>specific error</h1>";
$result = $batch_index->index_post();

$this->assertEquals( "specific error", $result['message'] );
Expand All @@ -190,21 +189,10 @@ public function test_returns_full_error_message_if_no_title_or_h1_is_present() {
$batch_index = new SolrPower_Batch_Index( array( 'posts_per_page' => 2 ) );
$batch_index->have_posts();

MockSolariumClient::$error_msg = "full error";
Mock_Solarium_Client_With_Error::$error_msg = "full error";
$result = $batch_index->index_post();

$this->assertEquals( "full error", $result['message'] );
}

}


class MockSolariumClient extends Solarium\Client {
public static $error_msg = "";

public function update(Solarium\Core\Query\QueryInterface $update, $endpoint = null) {
throw new Exception(self::$error_msg);
}
}


0 comments on commit ee80e68

Please sign in to comment.