Skip to content

Commit

Permalink
fix tests for php7
Browse files Browse the repository at this point in the history
  • Loading branch information
cornernote committed Mar 25, 2017
1 parent e390b0f commit 2d951ed
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 47 deletions.
32 changes: 16 additions & 16 deletions tests/codeception/unit/AuditErrorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ public function testGetEntry()

public function testAddManualError()
{
$oldId = $this->tester->fetchTheLastModelPk(AuditError::className());
$oldId = $this->getLastPk(AuditError::className());

$audit = $this->module();
$entry = $this->entry();
$errorPanel = $audit->getPanel($audit->findPanelIdentifier(ErrorPanel::className()));
$errorPanel->logMessage($entry->id, 'This is an unexpected error!', 1234, 'test.php', 50);

$newId = $this->tester->fetchTheLastModelPk(AuditError::className());
$newId = $this->getLastPk(AuditError::className());
$this->assertEquals($oldId + 1, $newId, 'Expected error entry to be created');

$this->assertInstanceOf(AuditError::className(), $error = AuditError::findOne($newId));
Expand All @@ -50,31 +50,31 @@ public function testAddManualError()

public function testException()
{
$oldId = $this->tester->fetchTheLastModelPk(AuditError::className());
$oldId = $this->getLastPk(AuditError::className());

$exception = new Exception('This is a test error!');
Yii::$app->errorHandler->logException($exception);

$newId = $this->tester->fetchTheLastModelPk(AuditError::className());
$newId = $this->getLastPk(AuditError::className());
$this->assertNotEquals($oldId, $newId, 'Expected error entry to be created');
}

public function testExceptionOutOfMemory()
{
$oldId = $this->tester->fetchTheLastModelPk(AuditError::className());
$oldId = $this->getLastPk(AuditError::className());

$exception = new Exception('Allowed memory size of ...');
Yii::$app->errorHandler->logException($exception);

$newId = $this->tester->fetchTheLastModelPk(AuditError::className());
$newId = $this->getLastPk(AuditError::className());
$this->assertEquals($oldId, $newId, 'Expected error entry to not be created');
}

public function testModuleCannotNotLoadDoesntLog()
{
$module = Audit::getInstance();

$oldId = $this->tester->fetchTheLastModelPk(AuditError::className());
$oldId = $this->getLastPk(AuditError::className());

Audit::setInstance(null);
Yii::$app->setModule('audit', null);
Expand All @@ -84,13 +84,13 @@ public function testModuleCannotNotLoadDoesntLog()

// Restore module so the getDb() calls work again
Audit::setInstance($module);
$newId = $this->tester->fetchTheLastModelPk(AuditError::className());
$newId = $this->getLastPk(AuditError::className());
$this->assertEquals($oldId, $newId, 'Expected error entry to not be created');
}

public function testModuleIsAutoloadedDuringException()
{
$oldId = $this->tester->fetchTheLastModelPk(AuditError::className());
$oldId = $this->getLastPk(AuditError::className());

Audit::setInstance(null);
// Back to configuaration array (default settings)
Expand All @@ -100,14 +100,14 @@ public function testModuleIsAutoloadedDuringException()
Yii::$app->errorHandler->logException($exception);

$this->assertInstanceOf(Audit::className(), Audit::getInstance());
$newId = $this->tester->fetchTheLastModelPk(AuditError::className());
$newId = $this->getLastPk(AuditError::className());
$this->assertEquals($oldId + 1, $newId, 'Expected error entry to be created');
}

public function testModuleIsNotAutoloadedDuringAMemoryExecption()
{
$module = Audit::getInstance();
$oldId = $this->tester->fetchTheLastModelPk(AuditError::className());
$oldId = $this->getLastPk(AuditError::className());

Audit::setInstance(null);
// Back to configuaration array (default settings)
Expand All @@ -120,27 +120,27 @@ public function testModuleIsNotAutoloadedDuringAMemoryExecption()

// Restore module for database
Audit::setInstance($module);
$newId = $this->tester->fetchTheLastModelPk(AuditError::className());
$newId = $this->getLastPk(AuditError::className());
$this->assertEquals($oldId, $newId, 'Expected error entry not to be created');
}

public function testThatExceptionUtilityFunctionIsAdded()
{
$module = $this->module();

$oldId = $this->tester->fetchTheLastModelPk(AuditError::className());
$oldId = $this->getLastPk(AuditError::className());
$module->exception(new \Exception('Testing utility functions'));
$newId = $this->tester->fetchTheLastModelPk(AuditError::className());
$newId = $this->getLastPk(AuditError::className());
$this->assertEquals($oldId + 1, $newId, 'Expected error entry to be created');
}

public function testThatErrorMessageUtilityFunctionIsAdded()
{
$module = $this->module();

$oldId = $this->tester->fetchTheLastModelPk(AuditError::className());
$oldId = $this->getLastPk(AuditError::className());
$module->errorMessage("testing the message function");
$newId = $this->tester->fetchTheLastModelPk(AuditError::className());
$newId = $this->getLastPk(AuditError::className());
$this->assertEquals($oldId + 1, $newId, 'Expected error entry to be created');
}

Expand Down
4 changes: 2 additions & 2 deletions tests/codeception/unit/AuditMailTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function testGetEntry()

public function testAddManualMail()
{
$oldId = $this->tester->fetchTheLastModelPk(AuditMail::className());
$oldId = $this->getLastPk(AuditMail::className());

$message = [
'subject' => 'test email subject',
Expand All @@ -49,7 +49,7 @@ public function testAddManualMail()
->setHtmlBody($message['html'])
->send();

$newId = $this->tester->fetchTheLastModelPk(AuditMail::className());
$newId = $this->getLastPk(AuditMail::className());
$this->assertNotEquals($oldId, $newId, 'Expected mail entry to be created');

$this->assertInstanceOf(AuditMail::className(), $mail = AuditMail::findOne($newId));
Expand Down
10 changes: 5 additions & 5 deletions tests/codeception/unit/AuditTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ public function testThatEntryIsCreated()
$this->entry();
$this->finalizeAudit(true);

$oldEntryId = $this->tester->fetchTheLastModelPk(AuditEntry::className());
$oldEntryId = $this->getLastPk(AuditEntry::className());
$this->entry();
$this->finalizeAudit(true);

$newEntryId = $this->tester->fetchTheLastModelPk(AuditEntry::className());
$newEntryId = $this->getLastPk(AuditEntry::className());
$this->entry();
$this->finalizeAudit(true);

Expand All @@ -45,17 +45,17 @@ public function testThatCustomDataCanBeAttachedToAnEntry()
$this->entry();
$this->finalizeAudit(true);

$entryId = $this->tester->fetchTheLastModelPk(AuditEntry::className());
$entryId = $this->getLastPk(AuditEntry::className());

$originalData = ['some', 'array', 'with', 'test', 'data'];

$oldDataId = $this->tester->fetchTheLastModelPk(AuditData::className());
$oldDataId = $this->getLastPk(AuditData::className());
$this->module()->data('extra-test', $originalData);

$this->entry();
$this->finalizeAudit(true);

$newDataId = $this->tester->fetchTheLastModelPk(AuditData::className());
$newDataId = $this->getLastPk(AuditData::className());
$this->assertNotEquals($oldDataId, $newDataId, 'I expected that a new data entry was added');

$this->tester->seeRecord(AuditData::className(), [
Expand Down
11 changes: 11 additions & 0 deletions tests/codeception/unit/AuditTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace tests\codeception\unit;

use yii\db\ActiveRecord;

class AuditTestCase extends \yii\codeception\TestCase
{
/**
Expand Down Expand Up @@ -49,4 +51,13 @@ public function assertEqualsIgnoreLineBreakType($actual, $expected)
$this->assertEquals($actual, $expected);
}


public function getLastPk($modelName)
{
/** @var ActiveRecord $model */
$model = new $modelName;
$model = $model->find()->orderBy([$model->primaryKey()[0] => SORT_DESC])->one();
return $model ? $model->primaryKey : 0;
}

}
12 changes: 6 additions & 6 deletions tests/codeception/unit/AuditTrailBehaviorIgnoredClassesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class AuditTrailBehaviorIgnoredClassesTest extends AuditTestCase
*/
public function testCreatePost()
{
$oldTrailId = $this->tester->fetchTheLastModelPk(AuditTrail::className());
$oldTrailId = $this->getLastPk(AuditTrail::className());
$post = new Post();

$post->getBehavior('audit')->ignoredClasses = [Post::className()];
Expand All @@ -33,7 +33,7 @@ public function testCreatePost()
$post->body = 'New post body';
$this->assertTrue($post->save());

$newTrailId = $this->tester->fetchTheLastModelPk(AuditTrail::className());
$newTrailId = $this->getLastPk(AuditTrail::className());
$this->assertEquals($oldTrailId, $newTrailId, 'I expected that a new trail was not added');
}

Expand All @@ -42,7 +42,7 @@ public function testCreatePost()
*/
public function testUpdatePost()
{
$oldTrailId = $this->tester->fetchTheLastModelPk(AuditTrail::className());
$oldTrailId = $this->getLastPk(AuditTrail::className());

$post = Post::findOne(1);

Expand All @@ -52,7 +52,7 @@ public function testUpdatePost()
$post->body = 'Updated post body';
$this->assertTrue($post->save());

$newTrailId = $this->tester->fetchTheLastModelPk(AuditTrail::className());
$newTrailId = $this->getLastPk(AuditTrail::className());
$this->assertEquals($oldTrailId, $newTrailId, 'I expected that a new trail was not added');
}

Expand All @@ -61,15 +61,15 @@ public function testUpdatePost()
*/
public function testDeletePost()
{
$oldTrailId = $this->tester->fetchTheLastModelPk(AuditTrail::className());
$oldTrailId = $this->getLastPk(AuditTrail::className());

$post = Post::findOne(1);

$post->getBehavior('audit')->ignoredClasses = [Post::className()];

$this->assertSame($post->delete(), 1);

$newTrailId = $this->tester->fetchTheLastModelPk(AuditTrail::className());
$newTrailId = $this->getLastPk(AuditTrail::className());
$this->assertEquals($oldTrailId, $newTrailId, 'I expected that a new trail was not added');
}

Expand Down
12 changes: 6 additions & 6 deletions tests/codeception/unit/AuditTrailBehaviorNotActiveTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class AuditTrailBehaviorNotActiveTest extends AuditTestCase
*/
public function testCreatePost()
{
$oldTrailId = $this->tester->fetchTheLastModelPk(AuditTrail::className());
$oldTrailId = $this->getLastPk(AuditTrail::className());
$post = new Post();

$post->getBehavior('audit')->active = 0;
Expand All @@ -33,7 +33,7 @@ public function testCreatePost()
$post->body = 'New post body';
$this->assertTrue($post->save());

$newTrailId = $this->tester->fetchTheLastModelPk(AuditTrail::className());
$newTrailId = $this->getLastPk(AuditTrail::className());
$this->assertEquals($oldTrailId, $newTrailId, 'I expected that a new trail was not added');
}

Expand All @@ -42,7 +42,7 @@ public function testCreatePost()
*/
public function testUpdatePost()
{
$oldTrailId = $this->tester->fetchTheLastModelPk(AuditTrail::className());
$oldTrailId = $this->getLastPk(AuditTrail::className());

$post = Post::findOne(1);

Expand All @@ -52,7 +52,7 @@ public function testUpdatePost()
$post->body = 'Updated post body';
$this->assertTrue($post->save());

$newTrailId = $this->tester->fetchTheLastModelPk(AuditTrail::className());
$newTrailId = $this->getLastPk(AuditTrail::className());
$this->assertEquals($oldTrailId, $newTrailId, 'I expected that a new trail was not added');
}

Expand All @@ -61,15 +61,15 @@ public function testUpdatePost()
*/
public function testDeletePost()
{
$oldTrailId = $this->tester->fetchTheLastModelPk(AuditTrail::className());
$oldTrailId = $this->getLastPk(AuditTrail::className());

$post = Post::findOne(1);

$post->getBehavior('audit')->active = 0;

$this->assertSame($post->delete(), 1);

$newTrailId = $this->tester->fetchTheLastModelPk(AuditTrail::className());
$newTrailId = $this->getLastPk(AuditTrail::className());
$this->assertEquals($oldTrailId, $newTrailId, 'I expected that a new trail was not added');
}

Expand Down
16 changes: 8 additions & 8 deletions tests/codeception/unit/AuditTrailBehaviorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ class AuditTrailBehaviorTest extends AuditTestCase
*/
public function testCreatePost()
{
$oldEntryId = $this->tester->fetchTheLastModelPk(AuditEntry::className());
$oldEntryId = $this->getLastPk(AuditEntry::className());
$post = new Post();
$post->title = 'New post title';
$post->body = 'New post body';
$this->assertTrue($post->save());

$newEntryId = $this->tester->fetchTheLastModelPk(AuditEntry::className());
$newEntryId = $this->getLastPk(AuditEntry::className());
$this->assertNotEquals($oldEntryId, $newEntryId, 'I expected that a new entry was added');
$this->tester->seeRecord(Post::className(), [
'id' => $post->id,
Expand Down Expand Up @@ -76,14 +76,14 @@ public function testCreatePost()
*/
public function testUpdatePost()
{
$oldEntryId = $this->tester->fetchTheLastModelPk(AuditEntry::className());
$oldEntryId = $this->getLastPk(AuditEntry::className());

$post = Post::findOne(1);
$post->title = 'Updated post title';
$post->body = 'Updated post body';
$this->assertTrue($post->save());

$newEntryId = $this->tester->fetchTheLastModelPk(AuditEntry::className());
$newEntryId = $this->getLastPk(AuditEntry::className());
$this->assertNotEquals($oldEntryId, $newEntryId, 'I expected that a new entry was added');

$this->tester->seeRecord(Post::className(), [
Expand Down Expand Up @@ -120,11 +120,11 @@ public function testUpdatePost()
*/
public function testDeletePost()
{
$oldEntryId = $this->tester->fetchTheLastModelPk(AuditEntry::className());
$oldEntryId = $this->getLastPk(AuditEntry::className());
$post = Post::findOne(1);
$this->assertSame($post->delete(), 1);

$newEntryId = $this->tester->fetchTheLastModelPk(AuditEntry::className());
$newEntryId = $this->getLastPk(AuditEntry::className());
$this->assertNotEquals($oldEntryId, $newEntryId, 'I expected that a new entry was added');

$this->tester->dontSeeRecord(Post::className(), [
Expand All @@ -147,12 +147,12 @@ public function testDeletePost()
*/
public function testUpdatePostNoChange()
{
$oldTrailId = $this->tester->fetchTheLastModelPk(AuditTrail::className());
$oldTrailId = $this->getLastPk(AuditTrail::className());

$post = Post::findOne(1);
$this->assertTrue($post->save());

$newTrailId = $this->tester->fetchTheLastModelPk(AuditTrail::className());
$newTrailId = $this->getLastPk(AuditTrail::className());
$this->assertEquals($oldTrailId, $newTrailId, 'I expected that a new trail was not added');
}

Expand Down

0 comments on commit 2d951ed

Please sign in to comment.