Commit bc1e5a4b authored by Michael Iseard's avatar Michael Iseard
Browse files

Remove old tests

parent 4797a197
<?php
namespace Entity;
use Kudos\Entity\DonorEntity;
use Kudos\Entity\SubscriptionEntity;
use Kudos\Entity\TransactionEntity;
use Kudos\Service\MailerService;
use Kudos\Service\MapperService;
use WP_UnitTestCase;
class AbstractEntityTests extends WP_UnitTestCase {
public function test_actions_correctly_registered() {
$this->assertEquals(10, has_action('kudos_donors_remove_secret_action', ['Kudos\Entity\DonorEntity','remove_secret_action']));
$this->assertEquals(10, has_action('kudos_transactions_remove_secret_action', ['Kudos\Entity\TransactionEntity','remove_secret_action']));
$this->assertEquals(10, has_action('kudos_subscriptions_remove_secret_action', ['Kudos\Entity\SubscriptionEntity','remove_secret_action']));
}
public function test_factory_method() {
$object1 = MailerService::factory();
$object2 = MailerService::factory();
$id1 = spl_object_hash($object1);
$id2 = spl_object_hash($object2);
$this->assertEquals($id1, $id2);
$object3 = new MailerService();
$object4 = new MailerService();
$id3 = spl_object_hash($object3);
$id4 = spl_object_hash($object4);
$this->assertNotEquals($id3, $id4);
}
public function test_can_construct_without_parameters() {
$donor = new DonorEntity();
$this->assertInstanceOf( DonorEntity::class, $donor );
}
public function test_can_construct_with_valid_parameters() {
$donor = new DonorEntity( [
'email' => 'test@test.com',
'name' => 'Michael',
] );
$this->assertEquals( 'Michael', $donor->name );
}
public function test_does_not_set_invalid_parameters() {
$donor = new DonorEntity( [
'email' => 'test@test.com',
'name' => 'Michael',
'invalid' => 'Property does not exist',
'postcode' => '1234RE'
] );
$this->assertFalse( property_exists( $donor, 'invalid' ) );
$this->assertEquals('1234RE', $donor->postcode );
}
public function test_can_set_valid_fields() {
$subscription = new SubscriptionEntity();
$subscription->set_fields([
'value' => 12,
'years' => 3,
'status' => 'active'
]);
$this->assertEquals(12, $subscription->value);
}
public function test_cannot_set_invalid_fields() {
$subscription = new SubscriptionEntity();
$subscription->set_fields([
'value' => 12,
'years' => 3,
'status' => 'active',
'invalid_field' => 'This should not be set'
]);
$this->assertFalse( property_exists( $subscription, 'invalid_field' ) );
}
public function test_correct_table_name_without_prefix() {
$this->assertEquals( 'kudos_subscriptions', SubscriptionEntity::get_table_name(false) );
}
public function test_correct_table_name_with_prefix() {
$this->assertEquals( 'wptests_kudos_donors', DonorEntity::get_table_name() );
}
public function test_create_new_secret() {
$donor = new DonorEntity([
'name' => 'Michael',
'email' => 'test@email.com',
]);
$this->assertIsString($donor->create_secret());
}
public function test_verify_secret_success() {
$donor = new DonorEntity();
$secret = $donor->create_secret();
$this->assertTrue($donor->verify_secret($secret));
}
public function test_verify_secret_empty() {
$donor = new DonorEntity();
$this->assertFalse($donor->verify_secret('12345'));
}
public function test_verify_secret_wrong() {
$donor = new DonorEntity();
$donor->create_secret();
$this->assertFalse($donor->verify_secret('12345'));
}
public function test_create_secret_not_overwritten() {
$donor = new DonorEntity([
'email' => 'test@test.com',
'name' => 'Michael'
]);
$mapper = new MapperService();
$mapper->save($donor);
$secret1 = $donor->create_secret();
$clone = clone $donor;
$secret2 = $clone->create_secret();
$this->assertEquals($donor->verify_secret($secret1), $donor->verify_secret($secret2));
}
public function test_clear_secret() {
$donor = new DonorEntity();
$secret = $donor->create_secret();
$donor->clear_secret();
$this->assertFalse($donor->verify_secret($secret));
}
public function test_to_array_returns_array() {
$transaction = new TransactionEntity([
'value' => 32,
'mode' => 'live',
'currency' => 'EUR'
]);
$this->assertIsArray( $transaction->to_array() );
}
public function test_to_string_returns_id() {
$transaction = new TransactionEntity([
'id' => 12,
'mode' => 'live',
'currency' => 'EUR'
]);
$this->assertEquals(12, (string) $transaction );
}
}
\ No newline at end of file
<?php
namespace Entity;
use Kudos\Entity\DonorEntity;
use Kudos\Entity\TransactionEntity;
use Kudos\Service\MapperService;
use WP_UnitTestCase;
class DonorEntityTests extends WP_UnitTestCase {
public function test_return_null_when_no_transactions() {
$mapper = new MapperService();
$donor = new DonorEntity([
'name' => 'Michael',
'email' => 'test@email.com',
'customer_id' => '1'
]);
$mapper->save( $donor );
$this->assertNull($donor->get_transactions());
}
public function test_can_get_transactions() {
$mapper = new MapperService();
$donor = new DonorEntity([
'name' => 'Michael',
'email' => 'test@email.com',
'customer_id' => '1'
]);
$mapper->save( $donor );
$transaction = new TransactionEntity( [
'order_id' => 'kdo_12345',
'customer_id' => '1',
'value' => '20',
'currency' => 'EUR',
'status' => 'open',
'mode' => 'test',
'sequence_type' => 'oneoff',
'campaign_label' => 'label',
] );
$mapper->save( $transaction );
$transaction = new TransactionEntity( [
'order_id' => 'kdo_67890',
'customer_id' => '1',
'value' => '10',
'currency' => 'EUR',
'status' => 'open',
'mode' => 'test',
'sequence_type' => 'oneoff',
'campaign_label' => 'label',
] );
$mapper->save( $transaction );
$this->assertCount(2, $donor->get_transactions());
}
}
\ No newline at end of file
<?php
namespace Entity;
use Kudos\Entity\DonorEntity;
use Kudos\Entity\SubscriptionEntity;
use Kudos\Service\MapperService;
use WP_UnitTestCase;
class SubscriptionEntityTests extends WP_UnitTestCase {
public function test_get_donor_returns_donor() {
$mapper = new MapperService();
$mapper->save(new DonorEntity([
'customer_id' => 12345,
'email' => 'test@email.com',
'name' => 'Michael'
]));
$subscription = new SubscriptionEntity([
'customer_id' => 12345,
'value' => 20,
'frequency' => '12 months',
'years' => 3
]);
$mapper->save($subscription);
/** @var DonorEntity $donor */
$donor = $subscription->get_donor();
$this->assertEquals('Michael', $donor->name);
}
}
\ No newline at end of file
<?php
namespace Entity;
use Kudos\Entity\DonorEntity;
use Kudos\Entity\TransactionEntity;
use Kudos\Service\MapperService;
use WP_UnitTestCase;
class TransactionEntityTests extends WP_UnitTestCase {
public function test_get_donor_returns_donor() {
$mapper = new MapperService();
$mapper->save(new DonorEntity([
'customer_id' => 12345,
'email' => 'test@email.com',
'name' => 'Michael'
]));
$transaction = new TransactionEntity([
'customer_id' => 12345,
'order_id' => 'kdo_12345',
'value' => 20,
'status' => 'open',
'mode' => 'test',
'sequence_type' => 'oneoff'
]);
$mapper->save($transaction);
/** @var DonorEntity $donor */
$donor = $transaction->get_donor();
$this->assertEquals('Michael', $donor->name);
}
public function test_get_refund() {
$transaction = new TransactionEntity([
'refunds' => json_encode([
'refunded' => 5,
'remaining' => 20
])
]);
$this->assertEquals( 20, $transaction->get_refund()->remaining);
}
}
\ No newline at end of file
<?php
namespace Service;
use Kudos\Entity\TransactionEntity;
use Kudos\Exceptions\MapperException;
use Kudos\Service\MapperService;
use Kudos\Service\MollieService;
use WP_UnitTestCase;
class MapperServiceTests extends WP_UnitTestCase {
public function test_can_instantiate_with_no_parameters() {
$mapper = new MapperService();
$this->assertNull($mapper->get_repository());
}
public function test_can_instantiate_with_valid_entity() {
$mapper = new MapperService(TransactionEntity::class);
$this->assertEquals(TransactionEntity::class, $mapper->get_repository());
}
public function test_repository_null_when_instantiated_with_invalid_class() {
$mapper = new MapperService('InvalidClass');
$this->assertEquals(NULL, $mapper->get_repository());
}
public function test_repository_null_when_instantiated_with_incompatible_class() {
$mapper = new MapperService(MollieService::class);
$this->assertEquals(NULL, $mapper->get_repository());
}
public function test_correct_table_name_returned() {
$mapper = new MapperService(TransactionEntity::class);
$this->assertEquals('wptests_kudos_transactions', $mapper->get_table_name());
}
/**
* @throws MapperException
*/
public function test_set_repository_throws_mapper_exception() {
$this->expectException(MapperException::class);
$mapper = new MapperService();
$mapper->set_repository(MollieService::class);
}
/**
* @throws MapperException
*/
public function test_set_and_get_repository_works_for_entity() {
$mapper = new MapperService();
$mapper->set_repository(TransactionEntity::class);
$this->assertEquals(TransactionEntity::class, $mapper->get_repository());
}
public function test_save_new_entity() {
$entity = new TransactionEntity([
'currency' => 'EUR',
'customer_id' => 'cst_1234',
'mode' => 'test',
'sequence_type' => 'oneoff',
'value' => 20,
'status' => 'open',
'transaction_id' => 't_12345',
'order_id' => 'kdo_12345',
]);
$mapper = new MapperService();
$save = $mapper->save($entity);
$this->assertNotFalse($save );
}
public function test_update_entity() {
$mapper = new MapperService(TransactionEntity::class);
$entity = new TransactionEntity([
'currency' => 'EUR',
'customer_id' => 'cst_1234',
'mode' => 'test',
'sequence_type' => 'oneoff',
'value' => 20,
'status' => 'open',
'transaction_id' => 't_12345',
'order_id' => 'kdo_12345',
]);
$mapper->save($entity);
/** @var TransactionEntity $transaction */
$transaction = $mapper->get_one_by([ 'order_id' => 'kdo_12345']);
$transaction->set_fields([
'mode' => 'live',
'customer_id' => NULL
]);
$mapper->save($transaction);
$transaction = $mapper->get_one_by(['order_id' => 'kdo_12345']);
$this->assertEquals('live', $transaction->mode );
$this->assertEquals('cst_1234', $transaction->customer_id );
}
public function test_update_entity_keep_null() {
$mapper = new MapperService(TransactionEntity::class);
$entity = new TransactionEntity([
'currency' => 'EUR',
'customer_id' => 'cst_1234',
'mode' => 'test',
'sequence_type' => 'oneoff',
'value' => 20,
'status' => 'open',
'transaction_id' => 't_12345',
'order_id' => 'kdo_12345',
]);
$mapper->save($entity);
/** @var TransactionEntity $transaction */
$transaction = $mapper->get_one_by([ 'order_id' => 'kdo_12345']);
$transaction->set_fields([
'mode' => 'live',
'customer_id' => NULL,
'currency' => '',
]);
$mapper->save($transaction, false);
$transaction = $mapper->get_one_by(['order_id' => 'kdo_12345']);
$this->assertEquals('live', $transaction->mode );
$this->assertEquals('', $transaction->customer_id );
$this->assertEquals('', $transaction->currency );
}
public function test_entity_missing_required_properties_on_save() {
$entity = new TransactionEntity([
'currency' => 'EUR',
'customer_id' => 'cst_1234',
'mode' => 'test',
'invalid_property' => 'ignore me',
'sequence_type' => 'oneoff',
// 'value' => 20, // Required and disabled
'status' => 'open',
'transaction_id' => 't_12345',
'order_id' => 'kdo_12345',
]);
$mapper = new MapperService(TransactionEntity::class);
$save = $mapper->save($entity);
$this->assertFalse($save);
}
public function test_invalid_properties_ignored() {
$entity = new TransactionEntity([
'currency' => 'EUR',
'customer_id' => 'cst_1234',
'mode' => 'test',
'invalid_property' => 'ignore me',
'sequence_type' => 'oneoff',
'value' => 20,
'status' => 'open',
'transaction_id' => 't_12345',
'order_id' => 'kdo_12345',
]);
$mapper = new MapperService(TransactionEntity::class);
$mapper->save($entity);
/** @var TransactionEntity $transaction */
$transaction = $mapper->get_one_by([ 'order_id' => 'kdo_12345']);
$this->assertEquals('t_12345', $transaction->transaction_id );
}
public function test_get_one_no_result() {
$mapper = new MapperService(TransactionEntity::class);
$entity = new TransactionEntity([
'currency' => 'EUR',
'customer_id' => 'cst_1234',
'mode' => 'test',
'sequence_type' => 'oneoff',
'value' => 20,
'status' => 'open',
'transaction_id' => 't_12345',
'order_id' => 'kdo_12345',
]);
$mapper->save($entity);
$transaction = $mapper->get_one_by(['order_id' => 'kdo_67890']);
$this->assertNull($transaction );
}
public function test_get_all_no_result() {
$mapper = new MapperService(TransactionEntity::class);
$entity = new TransactionEntity([
'currency' => 'EUR',
'customer_id' => 'cst_1234',
'mode' => 'test',
'sequence_type' => 'oneoff',
'value' => 20,
'status' => 'open',
'transaction_id' => 't_12345',
'order_id' => 'kdo_12345',
]);
$mapper->save($entity);
$transaction = $mapper->get_all_by(['order_id' => 'kdo_67890']);
$this->assertNull($transaction );
}
public function test_get_one() {
$transaction = new TransactionEntity( [
'order_id' => 'kdo_12345',
'customer_id' => '1',
'value' => '20',
'currency' => 'EUR',
'status' => 'open',
'mode' => 'test',
'sequence_type' => 'oneoff',
'campaign_label' => 'label',
] );
$mapper = new MapperService( TransactionEntity::class );
$mapper->save( $transaction );
/** @var TransactionEntity $transaction */
$transaction = $mapper->get_one_by([ 'order_id' => 'kdo_12345']);
$this->assertEquals('kdo_12345', $transaction->order_id );
}
public function test_get_all_with_no_parameters() {
$mapper = new MapperService( TransactionEntity::class );
$transaction = new TransactionEntity( [
'order_id' => 'kdo_12345',
'customer_id' => '1',
'value' => '20',