croak 'to many args to new'ifscalarkeys%{$args_for} > 1;
foreachmy$key(keys%{$args_for}) {
croak "Key not allowed: $key"
unless$ALLOWED_KEYS{$key};
}
return$self;
}
sub_write_to_file {
my$self= shift;
my$result= $self->{result};
my$override_flag= $self->{override_flag};
my$fixture_file= $self->{fixture_file};
returnunlessdefined$result;
returnunless$override_flag;
if($override_flag&& scalar@{$result}) {
my$json_data= $JSON_OBJ->encode($result);
my$fh= IO::File->new($fixture_file, 'w') or croak "cannot open file:$fixture_file $!\n";
say$fh$json_data;
$fh->closeor croak "cannot close file:$fixture_file $!\n";
undef$fh;
}
return$self;
}
sub_set_hashref_response {
my$self= shift;
my$sth= shift;
my$retval= shift;
my$result= [];
my$cols= $sth->{NAME};
foreachmy$col(@{$cols}) {
push@{$result}, $retval->{$col};
}
return$result;
}
subDESTROY {
my$self= shift;
my$result= delete$self->{result};
my$override_flag= delete$self->{override_flag};
$override= delete$self->{override};
my$dbh= delete$self->{dbh};
my$fixture_file= delete$self->{fixture_file};
return$self;
}
1;
=head1 NAME
DBD::Mock::Session::GenerateFixtures
=head1 SYNOPSIS
# Case 1: Providing a pre-existing DBI database handle for genereting a mocked data files
# with the test name
my $mock_dumper = DBD::Mock::Session::GenerateFixtures->new({ dbh => $dbh });
my $real_dbh = $mock_dumper->get_dbh();
# Case 2: Read data from the same file as current test
my $mock_dumper = DBD::Mock::Session::GenerateFixtures->new();
my $dbh = $mock_dumper->get_dbh();
# Your code using the mock DBD
# Case 3: Read data from a coustom file
my $mock_dumper = DBD::Mock::Session::GenerateFixtures->new({ file => 'path/to/fixture.json' });
my $dbh = $mock_dumper->get_dbh();
# Your code using the mock DBD
# Case 4: Providing an array reference containing mock data
my $mock_dumper = DBD::Mock::Session::GenerateFixtures->new({ data => \@mock_data });
my $dbh = $mock_dumper->get_dbh();
# Your code using the mock DBD
=head1 DESCRIPTION
When a real DBI database handle ($dbh) is provided, the module generates C<DBD::Mock::Session> data and stores it in a JSON file.
After the data is generated, remove the 'dbh' argument from the constructor, and it will use the previously generated data to create a 'DBD::Mock::Session' database handle.
Mocked data can also be loaded from a custom file or as a data structure.
This is not a part of the DBD::Mock::Session distribution; it's just a wrapper around it."
=head1 METHODS
=head2 new(\%args_for)
Constructor method to create a new C<DBD::Mock::Session::GenerateFixtures> object.
Accepts an optional hash reference C<\%args_for> with the following keys:
=over 4
=item * C<file>: File path to the fixture file containing mocked data.
=item * C<data>: Reference to an array containing mock data.
=item * C<dbh>: Database handle used for reading the data required to genereate a mocked dbh. This should used first time you are runnig the tests.
=back
=head2 get_dbh()
Returns the mocked database handle object.
=head2 get_override_object()
Returns the override object used for mocking DBI methods.
=head2 restore_all()
Restores all overridden DBI methods to their original implementations.
This method is used to revert all DBI method overrides set up for mocking database interactions back to their original implementations.
Returns the current object.
=head1 PRIVATE METHODS
These methods are not intended to be called directly from outside the module.
=head2 _initialize(\%args_for)
Initializes the C<DBD::Mock::Session::GenerateFixtures> object with the provided arguments.
=head2 _set_mock_dbh(\@data)
Sets up the mocked database handle based on the provided data.
=head2 _override_dbi_methods()
Overrides various DBI methods for mocking database interactions.
=head2 _override_dbi_execute($dbi_execute)
Overrides the C<execute> method of C<DBI::st> in order to capture the sql statement, bound_params and column names.
=head2 _override_dbi_bind_param($bind_param)
Overrides the C<bind_param> method of C<DBI::st> in order to capture the bound params.