What are the differences between integration tests and unit tests when working with temporary tables for testing?
When working with temporary tables for testing, the main difference between integration tests and unit tests is the scope of the tests. Integration tests typically involve testing the interaction between different components or systems, including the temporary tables, while unit tests focus on testing individual units of code in isolation, which may not involve the temporary tables. When writing integration tests for temporary tables, it is important to set up and tear down the temporary tables properly to ensure the tests are isolated and do not interfere with each other.
// Integration test example using temporary tables for testing
use PHPUnit\Framework\TestCase;
class IntegrationTest extends TestCase
{
protected $connection;
protected function setUp(): void
{
$this->connection = new PDO('mysql:host=localhost;dbname=test', 'username', 'password');
$this->connection->exec('CREATE TEMPORARY TABLE temp_table (id INT, name VARCHAR(255))');
}
protected function tearDown(): void
{
$this->connection->exec('DROP TEMPORARY TABLE temp_table');
$this->connection = null;
}
public function testTemporaryTableInsert()
{
// Test inserting data into the temporary table
$this->connection->exec('INSERT INTO temp_table (id, name) VALUES (1, "John")');
$stmt = $this->connection->query('SELECT * FROM temp_table');
$result = $stmt->fetch(PDO::FETCH_ASSOC);
$this->assertEquals(['id' => 1, 'name' => 'John'], $result);
}
}