Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure expectException is always the last call #499

Merged
merged 1 commit into from Apr 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
104 changes: 46 additions & 58 deletions tests/Galette/Core/tests/units/Db.php
Expand Up @@ -153,22 +153,18 @@ public function testGrant(): void
*/
public function testGrantWException(): void
{
$atoum = $this;

//test insert failing
$this->db = $this->getMockBuilder(\Galette\Core\Db::class)
->onlyMethods(array('execute'))
->getMock();

$this->db->method('execute')
->will(
$this->returnCallback(
function ($o): void {
if ($o instanceof \Laminas\Db\Sql\Insert) {
throw new \LogicException('Error executing query!', 123);
}
->willReturnCallback(
function ($o): void {
if ($o instanceof \Laminas\Db\Sql\Insert) {
throw new \LogicException('Error executing query!', 123);
}
)
}
);

$result = $this->db->grantCheck('u');
Expand All @@ -187,21 +183,19 @@ function ($o): void {
->getMock();

$this->db->method('execute')
->will(
$this->returnCallback(
function ($o) {
if ($o instanceof \Laminas\Db\Sql\Select) {
throw new \LogicException('Error executing query!', 123);
} else {
$rs = $this->getMockBuilder(\Laminas\Db\ResultSet\ResultSet::class)
->onlyMethods(array('count'))
->getMock();
$rs->method('count')
->willReturn(1);
return $rs;
}
->willReturnCallback(
function ($o) {
if ($o instanceof \Laminas\Db\Sql\Select) {
throw new \LogicException('Error executing query!', 123);
} else {
$rs = $this->getMockBuilder(\Laminas\Db\ResultSet\ResultSet::class)
->onlyMethods(array('count'))
->getMock();
$rs->method('count')
->willReturn(1);
return $rs;
}
)
}
);

$result = $this->db->grantCheck('u');
Expand All @@ -220,21 +214,19 @@ function ($o) {
->getMock();

$this->db->method('execute')
->will(
$this->returnCallback(
function ($o) {
if ($o instanceof \Laminas\Db\Sql\Update) {
throw new \LogicException('Error executing query!', 123);
} else {
$rs = $this->getMockBuilder(\Laminas\Db\ResultSet\ResultSet::class)
->onlyMethods(array('count'))
->getMock();
$rs->method('count')
->willReturn(1);
return $rs;
}
->willReturnCallback(
function ($o) {
if ($o instanceof \Laminas\Db\Sql\Update) {
throw new \LogicException('Error executing query!', 123);
} else {
$rs = $this->getMockBuilder(\Laminas\Db\ResultSet\ResultSet::class)
->onlyMethods(array('count'))
->getMock();
$rs->method('count')
->willReturn(1);
return $rs;
}
)
}
);

$result = $this->db->grantCheck('u');
Expand All @@ -253,21 +245,19 @@ function ($o) {
->getMock();

$this->db->method('execute')
->will(
$this->returnCallback(
function ($o) {
if ($o instanceof \Laminas\Db\Sql\Delete) {
throw new \LogicException('Error executing query!', 123);
} else {
$rs = $this->getMockBuilder(\Laminas\Db\ResultSet\ResultSet::class)
->onlyMethods(array('count'))
->getMock();
$rs->method('count')
->willReturn(1);
return $rs;
}
->willReturnCallback(
function ($o) {
if ($o instanceof \Laminas\Db\Sql\Delete) {
throw new \LogicException('Error executing query!', 123);
} else {
$rs = $this->getMockBuilder(\Laminas\Db\ResultSet\ResultSet::class)
->onlyMethods(array('count'))
->getMock();
$rs->method('count')
->willReturn(1);
return $rs;
}
)
}
);

$result = $this->db->grantCheck('u');
Expand Down Expand Up @@ -494,12 +484,10 @@ public function testDbVersionWException(): void
->onlyMethods(array('execute'))
->getMock();
$this->db->method('execute')
->will(
$this->returnCallback(
function ($table, $where): void {
throw new \LogicException('Error executing query!', 123);
}
)
->willReturnCallback(
function ($table, $where): void {
throw new \LogicException('Error executing query!', 123);
}
);

$this->expectException('LogicException');
Expand Down Expand Up @@ -651,7 +639,7 @@ public function testExecuteWException(): void
$select->where(['p.notknown' => 'azerty']);

$this->expectException('\PDOException');
$results = $this->db->execute($select);
$this->db->execute($select);
}

/**
Expand Down
50 changes: 20 additions & 30 deletions tests/Galette/Core/tests/units/Password.php
Expand Up @@ -207,12 +207,10 @@ public function testGenerateNewPasswordWException(): void
->getMock();

$this->zdb->method('execute')
->will(
$this->returnCallback(
function ($o): void {
throw new \LogicException('Error executing query!', 123);
}
)
->willReturnCallback(
function ($o): void {
throw new \LogicException('Error executing query!', 123);
}
);

$pass = new \Galette\Core\Password($this->zdb, false);
Expand All @@ -232,12 +230,10 @@ public function testGenerateNewPasswordWFalseInsert(): void
->getMock();

$this->zdb->method('execute')
->will(
$this->returnCallback(
function ($o): void {
throw new \LogicException('Error executing query!', 123);
}
)
->willReturnCallback(
function ($o): void {
throw new \LogicException('Error executing query!', 123);
}
);

$pass = new \Galette\Core\Password($this->zdb, false);
Expand All @@ -257,12 +253,10 @@ public function testCleanExpiredWException(): void
->getMock();

$this->zdb->method('execute')
->will(
$this->returnCallback(
function ($o): void {
throw new \LogicException('Error executing query!', 123);
}
)
->willReturnCallback(
function ($o): void {
throw new \LogicException('Error executing query!', 123);
}
);

$pass = new \Galette\Core\Password($this->zdb, false);
Expand All @@ -281,12 +275,10 @@ public function testIsHashValidWException(): void
->getMock();

$this->zdb->method('execute')
->will(
$this->returnCallback(
function ($o): void {
throw new \LogicException('Error executing query!', 123);
}
)
->willReturnCallback(
function ($o): void {
throw new \LogicException('Error executing query!', 123);
}
);

$pass = new \Galette\Core\Password($this->zdb, false);
Expand All @@ -306,12 +298,10 @@ public function testRemoveHashWException(): void
->getMock();

$this->zdb->method('execute')
->will(
$this->returnCallback(
function ($o): void {
throw new \LogicException('Error executing query!', 123);
}
)
->willReturnCallback(
function ($o): void {
throw new \LogicException('Error executing query!', 123);
}
);

$pass = new \Galette\Core\Password($this->zdb, false);
Expand Down
20 changes: 18 additions & 2 deletions tests/Galette/Core/tests/units/Plugins.php
Expand Up @@ -190,14 +190,30 @@ public function testModuleActivation(): void
$modules = $plugins->getModules();
$this->assertCount(3, $modules);
$this->assertTrue(isset($modules['plugin-test2']));
}

/**
* Test non-existant module activation
*
* @return void
*/
public function testNonExistantModuleActivation(): void
{
$plugins = $this->getPlugins();
$this->expectExceptionMessage(_T('No such module.'));
$plugins->deactivateModule('nonexistant');
$plugins->activateModule('nonexistant');
}

/**
* Test non-existant module de-activation
*
* @return void
*/
public function testNonExistantModuleDeactivation(): void
{
$plugins = $this->getPlugins();
$this->expectExceptionMessage(_T('No such module.'));
$plugins->activateModule('nonexistant');
$plugins->deactivateModule('nonexistant');
}

/**
Expand Down
6 changes: 3 additions & 3 deletions tests/Galette/DynamicFields/tests/units/DynamicField.php
Expand Up @@ -596,10 +596,10 @@ public function testRemove(): void

$this->assertTrue($df->remove());

$this->expectException('\PDOException');
$results = $this->zdb->execute($select);

$this->assertFalse(\Galette\DynamicFields\DynamicField::loadFieldType($this->zdb, $df_id));

$this->expectException('\PDOException');
$this->zdb->execute($select);
}

/**
Expand Down
4 changes: 0 additions & 4 deletions tests/Galette/Entity/tests/units/ContributionsTypes.php
Expand Up @@ -169,10 +169,6 @@ public function testContributionsTypes(): void
$ctype->delete(42)
);

/*$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage('You cannot delete default contribution type!');
$status->delete($status::DEFAULT_STATUS);*/

$this->assertTrue(
$ctype->delete($id)
);
Expand Down
32 changes: 25 additions & 7 deletions tests/Galette/Entity/tests/units/Group.php
Expand Up @@ -130,11 +130,11 @@ public function testSingleGroup(): void
}

/**
* Test group name unicity
* Test group name uniqueness when adding
*
* @return void
*/
public function testUnicity(): void
public function testAddUnicity(): void
{
global $zdb;
$zdb = $this->zdb;
Expand All @@ -144,11 +144,7 @@ public function testUnicity(): void

$group->setName('A group');
$this->assertTrue($group->store());
$group_id = $group->getId();

//update without changes should be ok
$group = new \Galette\Entity\Group($group_id);
$this->assertTrue($group->store());
$group->getId();

//Adding another group with same name throws an exception
$group = new \Galette\Entity\Group();
Expand All @@ -157,6 +153,28 @@ public function testUnicity(): void
$this->expectExceptionMessage('The group name you have requested already exists in the database.');
$group->setName('A group');
$this->assertFalse($group->store());
}

/**
* Test group name uniqueness when editing
*
* @return void
*/
public function testEditUnicity(): void
{
global $zdb;
$zdb = $this->zdb;

$group = new \Galette\Entity\Group();
$group->setLogin($this->login);

$group->setName('A group');
$this->assertTrue($group->store());
$group_id = $group->getId();

//update without changes should be ok
$group = new \Galette\Entity\Group($group_id);
$this->assertTrue($group->store());

//update with changes should be ok
$group = new \Galette\Entity\Group($group_id);
Expand Down
10 changes: 5 additions & 5 deletions tests/Galette/Entity/tests/units/PaymentType.php
Expand Up @@ -133,11 +133,6 @@ public function testPaymentType(): void
$results = $this->zdb->execute($select);
$this->assertSame(count($this->i18n->getArrayList()), count($results));

$type = new \Galette\Entity\PaymentType($this->zdb, \Galette\Entity\PaymentType::CASH);
$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage('You cannot delete system payment types!');
$type->remove();

$type = new \Galette\Entity\PaymentType($this->zdb, $id);
$this->assertTrue($type->remove());

Expand All @@ -149,5 +144,10 @@ public function testPaymentType(): void
);
$results = $this->zdb->execute($select);
$this->assertSame(0, $results->count());

$type = new \Galette\Entity\PaymentType($this->zdb, \Galette\Entity\PaymentType::CASH);
$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage('You cannot delete system payment types!');
$type->remove();
}
}
8 changes: 4 additions & 4 deletions tests/Galette/Entity/tests/units/Status.php
Expand Up @@ -152,10 +152,6 @@ public function testStatus(): void
$status->delete(42)
);

$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage('You cannot delete default status!');
$status->delete($status::DEFAULT_STATUS);

$this->assertTrue(
$status->delete($id)
);
Expand All @@ -168,6 +164,10 @@ public function testStatus(): void
);
$results = $this->zdb->execute($select);
$this->assertSame(0, $results->count());

$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage('You cannot delete default status!');
$status->delete($status::DEFAULT_STATUS);
}

/**
Expand Down