App-MaMGal

 view release on metacpan or  search on metacpan

t/050_unit_entry.t  view on Meta::CPAN

#!/usr/bin/perl
# mamgal - a program for creating static image galleries
# Copyright 2007, 2008 Marcin Owsiany <marcin@owsiany.pl>
# See the README file for license information
package App::MaMGal::Unit::Entry;
use strict;
use warnings;
use Carp 'verbose';
use File::stat;
use Test::More;
use Test::Exception;
use base 'Test::Class';

use lib 'testlib';
use App::MaMGal::TestHelper;

sub dir_preparation : Test(startup) {
	prepare_test_data;
}

sub class_setting : Test(startup) {
	my $self = shift;
	$self->{class_name} = 'App::MaMGal::Entry';
	$self->{test_file_name} = [qw(td empty_file)];
}

sub file_name
{
	my $self = shift;
	return @{$self->{test_file_name}};
}

# This should be done in a BEGIN, but then planning the test count is difficult.
# However we are not using function prototypes, so it does not matter much.
sub class_usage : Test(startup => 1) {
	use_ok($_[0]->{class_name}) or $_[0]->BAILOUT("Class usage failed");
}

sub parameter_checks : Test(startup => 6) {
	my $self = shift;
	my $class_name = $self->{class_name};

	dies_ok(sub { $class_name->new },                             "$class_name dies on creation with no args");
	dies_ok(sub { $class_name->new('/') },                        "$class_name dies on creation with one arg");
	dies_ok(sub { $class_name->new($self->file_name, 1) },       "$class_name dies on creation with third argument not being a File::stat");
	dies_ok(sub { $class_name->new(qw(td/empty .)) },             "$class_name refuses to be created with '.' as the basename, when a name could have been provided");
	dies_ok(sub { $class_name->new(qw(. td/empty)) },             "$class_name refuses to be created with basename containing a slash");
	my $stat = stat('td/empty_file');
	dies_ok(sub { $class_name->new(qw(td empty_file), $stat, 3) },"$class_name dies on creation with more than 3 args");
}

sub _entry_creation : Test(setup => 4) {
	my $self = shift;
	my $class_name = $self->{class_name};

	my $fake_stat = Test::MockObject->new;
	$fake_stat->set_isa('File::stat');
	$fake_stat->mock('mtime', sub { '1229888888' });
	$self->{fake_stat} = $fake_stat;

	my $e;
	{
		$e = $class_name->new($self->file_name, $fake_stat);
		isa_ok($e, $class_name);
		isa_ok($e, 'App::MaMGal::Entry');
		$self->{entry} = $e;
	}
	{
		$e = $class_name->new($self->file_name);
		isa_ok($e, $class_name);
		isa_ok($e, 'App::MaMGal::Entry');
		$self->{entry_no_stat} = $e;
	}
}

sub _tools_methods : Test(setup => 4) {
	my $self = shift;
	my $dir = $self->{test_file_name}->[0];
	$self->{mock_container} = Test::MockObject->new
		->mock('ensure_subdir_exists', sub { mkdir $dir.'/'.$_[1] })
		->mock('_write_contents_to');
	my $mock_ef = Test::MockObject->new->mock('create_entry_for', sub { $self->{mock_container} });
	{
		my $e = $self->{entry};
		my $class_name = $self->{class_name};
		my $tools_hashref = { entry_factory => $mock_ef, logger => get_mock_logger };
		is_deeply($e->tools, {}, "newly created entry has an empty tools hash");
		$e->add_tools($tools_hashref);
		ok(exists($e->tools->{entry_factory}), "new tool is present");
	}
	{
		my $e = $self->{entry_no_stat};
		my $class_name = $self->{class_name};
		my $tools_hashref = { entry_factory => $mock_ef, logger => get_mock_logger };
		is_deeply($e->tools, {}, "newly created entry has an empty tools hash");
		$e->add_tools($tools_hashref);
		ok(exists($e->tools->{entry_factory}), "new tool is present");
	}
}

sub logger_method : Test(2) {
	my $self = shift;
	my $class_name = $self->{class_name};
	my @test_file_name = $self->file_name;
	{
		my $e = $self->{entry};
		ok($e->logger, "logger is set");
	}



( run in 1.735 second using v1.01-cache-2.11-cpan-0fb53d1c279 )