cakephp / cakephp/phinx

Add public getDsn method to adapters for unit testing constructed DSN strings

Open
#2,095 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

testing
Dominant language
PHP
Stars
4.5k
Forks
884
PR merge metrics
No merged PRs in 30d

Description

For each adapter, there's a handful of ways to customize the DSN string we generate that is passed to PDO. However, there's not currently a good way we have to test that said behavior is correct / good and so we have some nonsensical tests like:

https://github.com/cakephp/phinx/blob/48d8e1f686852882a319e6deff1ae6748d04ab5c/tests/Phinx/Db/Adapter/SqlServerAdapterTest.php#L62-L68

Was unsetting the port meaningful? Who knows!

While we could maybe get what we want out of some level of reflection / mocking around the protected createPdoConnection method, that should be avoided if possible.

Instead, having a getDsn method that returns a string instead would more easily solve the problem, and allow for best practices around unit testing these things, and ensure behavior is good and correct. It's possible that said method could also be useful to someone downstream, but I'm not overly concerned at the moment with that.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the protected createPdoConnection method in src/Phinx/Db/Adapter/PdoAdapter.php and the example in tests/Phinx/Db/Adapter/SqlServerAdapterTest.php. Review how each adapter constructs its DSN, then add focused tests that verify the returned strings for customization options. Done means the adapter behavior is testable without reflection or mocking the PDO connection.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
databases, testing
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.