Cpanel-iContact-Provider-Local.t 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. use strict;
  2. use warnings;
  3. use Cwd qw{abs_path};
  4. use File::Basename qw{dirname};
  5. use lib abs_path( dirname(__FILE__) . "/../lib" );
  6. use Test::More;
  7. use Test::Fatal;
  8. use File::Temp ();
  9. use Cpanel::iContact::Provider::Local ();
  10. use Cpanel::iContact::Provider::Local::Getter ();
  11. plan tests => 1;
  12. # First, let's mock out the parent, and other stuff we wouldn't wanna do in a unit test
  13. subtest "Provider bits work as expected ('unit' test)" => sub {
  14. plan tests => 7;
  15. # Create tempdir for jamming stuff into
  16. my $tmp_obj = File::Temp->newdir();
  17. my $tmp_dir = $tmp_obj->dirname;
  18. $Cpanel::iContact::Provider::Local::DIR = "$tmp_dir/iContact_notices";
  19. # Make the notice send
  20. isa_ok( my $spammer = Cpanel::iContact::Provider::Local->new(), "Cpanel::iContact::Provider::Local" );
  21. my $ex = exception { $spammer->send() };
  22. is( $ex, undef, "send doesn't throw on GreatSuccess" ) || diag explain $ex;
  23. my $user = getpwuid($<);
  24. my @files = glob( "$tmp_dir/iContact_notices/$user/*.json" );
  25. ok( scalar(@files), "Looks like a file was written..." ) || diag explain \@files;
  26. like( $files[0], qr/\d\.json/, "..and it looks like we'd expect it to" ) || diag explain \@files;
  27. # Now let's check on them another way
  28. my %notifications = Cpanel::iContact::Provider::Local::Getter::get_all_notices( 'user' => $user );
  29. ok( scalar(keys(%notifications)), "Got the expected notifications from hash..." ) || diag explain \%notifications;
  30. like( (keys(%notifications))[0], qr/\d+/, "..and the hash key looks like we'd expect it to" ) || diag explain \%notifications;
  31. is_deeply( [ sort keys(%{ $notifications{(keys(%notifications))[0]} }) ], [ 'html', 'subject', 'text' ], "%notifications{time} hashref has has the right keys" );
  32. };